android.media.SoundPool 会导致内存泄漏吗?
我发现这些代码每次执行后都可能会在 android 2.1 上出现内存泄漏
SoundPool soundPool = new SoundPool(10, 7, 0);
...
...
soundPool = null;
,MAT 插入表明两个“android:unnamed_thread”的 String 对象被添加到进程的堆中。这是一个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试运行 soundPool.release() 而不是 soundPool = null ?
did you try to run soundPool.release() instead of soundPool = null?
我看到两种可能性(可能还有更多)。
第一个(最有可能)对于所有 Java 对象都是如此:仅仅因为将引用设置为 null 并不自动意味着它后面的对象将被垃圾收集。
如果
SoundPool
对象本身包含对两个线程对象的引用,则在需要空间之前,这三个对象都不一定会被 GC(当然,这取决于收集器的积极性) 。第二个(不太可能)是 Android 可能足够聪明,可以缓存线程(甚至 SoundPool)对象,以防需要再次使用它们。如果对象创建比对象回收更昂贵,他们可能会这样做作为性能优化。
在这种情况下,它们仍然会引用缓存中某处的对象,并且它们不会被视为有资格进行垃圾收集。
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.