android垃圾收集器,释放对象内的对象
我很好奇这个东西在 Android 中是如何工作的。我有一堂课有两个 List<>在里面,在运行时实例化并加载了在读取一些数据时创建的对象,我想知道在这种情况下这些列表会发生什么:
类 A 具有列表 B 和列表 C 以及许多其他初始化的对象
另一个不同的类从 a 获取列表 C 的引用 A 类的方法,如
public List
.GetList() 在代码中的某个地方,A类不再被使用并且应用程序 向垃圾收集器发出信号,将对象设置为 null。
我的列表 C 被其他对象引用会发生什么情况?对象类别 A 会发生什么情况?
我尝试使用 Logcat 在调试器中逐步运行 apk 来跟踪垃圾收集器,但根本没有运气。有时它会释放内存,有时则不会,我无法确定任何特定的行为。
I'm very curious about how this thing works inside Android. I have a class with two List<> inside, instantiated at runtime and loaded with objects created while reading some data, I want to know what happens with those Lists in this situation:
Class A has List B and List C with many other initialized objects
inside.Another different class get a reference of List C from a
method of Class A, likepublic List<myObject> GetList()
.Somewhere in code, Class A is no longer used and application
signals that to garbage collector, setting object to null.
What happens with my List C that is referenced by other object? What happens with Object class A?
I've tried to track garbage collector with Logcat running apk in debugger step-by-step but no luck at all. Sometimes it frees memory, sometimes not, I couldn't pinpoint any specific behaviour.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果有任何对象仍然具有到根的路径,并且包含对另一个对象的引用,则引用的对象将不会被垃圾收集。因此,如果您创建对象 A,它就有一个子对象 A1。然后创建对象 B 并通过任何方式将对 A1 的引用传递给对象 B。即使对象 A 被 GC,A1 也不会被 GC,因为对象 B 仍然持有引用。 (在您的示例中,A 和 B 将被 gc'd...C 不会)
您可以通过使用
System.gc 显式告诉系统在特定时间执行收集来试验垃圾收集背后的机制();
调用。您还可以覆盖对象中的finalize,以准确查看给定对象何时被收集。:通过尝试对象、引用、finalize 和对 gc() 的显式调用,您可以学到很多东西。当您完成测试时,我会删除覆盖的最终调用。
If there is any object, that still has a path to root, containing a reference to another object, the referenced object will not be garbage collected. So if you create object A, that has a sub object A1. Then create object B and through what ever means pass a reference to A1 to object B. Even when object A is GC'd A1 will not be because object B still holds a reference. (In your example, A & B will be gc'd... C will not)
You can experiment with the mechanics behind garbage collection by telling the system explicitly to perform a collection at certain times with a
System.gc();
call. Also you can override the finalize in object to see exactly when the given object is collected.:You can learn a lot through experimenting with objects, references, finalize and explicit calls for gc(). I would remove the overridden finalize call when you done testing.
A 类对象将在下一个 GC 周期期间被垃圾回收。列表 B 也将被 GC 回收。并且列表 C 不会被 GC,因为它可以从 GC 根到达。
The object of class A will be garbage collected during the next GC-cycle. List B will be GC'ed too. And List C will not be GC'ed because it can be reached from GC roots.
默认情况下,java 通过引用而不是通过值创建对象。因此,在这种情况下,如果垃圾收集器拾取并清空所有 ABC,那么引用 C 的另一个类将为空,因为它没有创建该类的重复项,只是引用了它。
By default java creates an object by reference instead of by value. So, in this case if the garbage collector would pick up and empty all A B C and the then the other class that referenced C would be empty, since it didnt create a duplication of the class, it just referenced it.