android垃圾收集器,释放对象内的对象

发布于 2024-11-30 09:24:06 字数 460 浏览 0 评论 0原文

我很好奇这个东西在 Android 中是如何工作的。我有一堂课有两个 List<>在里面,在运行时实例化并加载了在读取一些数据时创建的对象,我想知道在这种情况下这些列表会发生什么:

  1. 类 A 具有列表 B 和列表 C 以及许多其他初始化的对象

  2. 另一个不同的类从 a 获取列表 C 的引用 A 类的方法,如 public ListGetList().

  3. 在代码中的某个地方,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:

  1. Class A has List B and List C with many other initialized objects
    inside.

  2. Another different class get a reference of List C from a
    method of Class A, like public List<myObject> GetList().

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缪败 2024-12-07 09:24:06

如果有任何对象仍然具有到根的路径,并且包含对另一个对象的引用,则引用的对象将不会被垃圾收集。因此,如果您创建对象 A,它就有一个子对象 A1。然后创建对象 B 并通过任何方式将对 A1 的引用传递给对象 B。即使对象 A 被 GC,A1 也不会被 GC,因为对象 B 仍然持有引用。 (在您的示例中,A 和 B 将被 gc'd...C 不会)

您可以通过使用 System.gc 显式告诉系统在特定时间执行收集来试验垃圾收集背后的机制(); 调用。您还可以覆盖对象中的finalize,以准确查看给定对象何时被收集。:

   @Override
   protected void finalize() throws Throwable {
      try {
         Log.d("Learning about GC", "My Object has been GC'd." + this);         
      } finally {
         super.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.:

   @Override
   protected void finalize() throws Throwable {
      try {
         Log.d("Learning about GC", "My Object has been GC'd." + this);         
      } finally {
         super.finalize();
      }
   }

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.

星軌x 2024-12-07 09:24:06

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.

乱世争霸 2024-12-07 09:24:06

默认情况下,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文