简体   繁体   中英

Android WeakReference not behaving as expected

As I know, from the spec (and from the standard non-android) java, If an object is only "Weak Referenced", that is, only reachable by weak references - it will be collected by the garbage collection the next time it runs.

In my android app, for some reason, This is not the case.

A quite heavy object (Activity) stays in the heap every time its created, but when I perform a heap dump and examine it in the Eclipse Memory Analyzer I see that the object remained in the heap, even though it's only weak referenced (as can be seen in merge paths to GC roots option)

When I explicitly clear the weak reference, the object gets cleared.

I run my app on Android 2.3.3. It looks like a some kind of platform bug.

Am I missing something? Has anyone else encountered this issue?

Thanks, Yoav

it will be collected by the garbage collection the next time it runs.

No, there are no such guarantees. It may be collected. (This holds for all objects, regardless of reference types, and it's the reason why you often read that an object becomes " eligible for collection".)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM