views:

86

answers:

2

hi, friends,

i found these code may case memory leak on android 2.1


    SoundPool soundPool = new SoundPool(10, 7, 0);
    ...
    ...
    soundPool = null;

every time after the execution, the MAT pluging tells that two String objects of "android:unnamed_thread" are added to the heap of the process. is that an issue?

A: 

I see two possibilities (there may well be more).

The first (most likely) is true of all Java objects: just because you set the reference to null doesn't automatically mean that the object behind it will be garbage-collected.

If a SoundPool object itself contains a reference to the two thread objects, none of the three will necessarily be GC'ed until space is required (although that depends, of course, on how aggressive your collector is).

The second (less likely) is that Android may be smart enough to cache thread (or even SoundPool) objects in case they need to be used again. They may nave done this as a performance optimisation if object creation is more expensive than object re-cycling.

In that case, they would still have a reference to the objects somewhere in a cache and they wouldn't be considered eligible for garbage collection.

paxdiablo
Thanks a lot. And I called System.gc() in my code, but it did not help to clean those objects. Does it mean the first posibility is not possible?
I'm pretty certain that `System.gc()` is a _suggestion_ to do garbage collection: "Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects ...". So I wouldn't rule it out as a possibility.
paxdiablo