我是 Android 的初学者,但我似乎遇到了一个问题,我的 Activity 永远不会被垃圾收集,因为在我的线程的非静态内部类中仍然有一个对 Activity 的引用,该变量被称为this$0 我是通过使用 Eclipse 中的 MAT 找到的。以下是我当前的设置。
public class MyActivity
-->class MySurfaceView
---->class MyThread
当尝试查找有关是否可以删除最后一个实例的信息时,在找到一篇非常有用的文章 here 后,我似乎无法删除这最后一个实例。 .
Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance
所以看来我不应该使用这个内部类系统或使用弱引用。
我的问题是,为什么示例项目使用没有弱引用的内部类系统?他们还在收集垃圾吗?或者它们的设计目的不是处理垃圾收集?
我的应用程序是否可能存在代码问题,这意味着我应该更正我的代码并仍然保留我的内部类?或者我应该分离这些类(或引入 WeakReferences)?
最佳答案
您可以使用内部类使其静态并具有弱引用父级。 还将长期存在的对象存储在应用程序上下文中。
关于java - Android 内部类由于 this$0 变量而未被垃圾回收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8926299/