垃圾收集 - 孤立的 LinkedList 链接 [英] Garbage collection - orphaned LinkedList links

查看:18
本文介绍了垃圾收集 - 孤立的 LinkedList 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有引用 A ->B->C->D.当你从 A 中删除对 B 的引用时,你会留下一个孤立的对象链 B ->C->D.

Suppose you have references A -> B -> C -> D. When you delete the reference to B from A, you're left with an orphaned chain of Objects B -> C -> D.

CD 是否会被垃圾收集,即使无法找到它们(因为没有对 B 的引用)?

Will C and D be garbage collected even though there's no way to get to them (since there's no reference to B)?

我想 GC 对此很聪明,并且会解决任何此类依赖项.

I imagine the GC is smart about this and will resolve any such dependencies.

但是,我查看了 源代码 用于 LinkedList 类,发现与此相反的东西.我注意到当一个列表被 clear() ed 时,对每个链接的所有引用都显式设置为 null,从而使其成为 O(n) 操作.这样做有什么理由/好处吗?

However, I took a look into the source code for the LinkedList class and found something contrary to this belief. I noticed that when a list is clear()ed, all of the references to each link are explicitly set to null, thus making it an O(n) operation. Is there any reason/benefit for doing so?

推荐答案

这看起来确实有点奇怪.也许它显式拆除列表的原因是为了清除现有迭代器和子列表以及父列表的列表.

That does look a bit peculiar. Maybe the reason that it is explicitly dismantling the list is so that the list is cleared for existing iterators and sublists as well as the parent list.

当然不是为了加快垃圾收集速度.垃圾收集器不会遍历无法访问的对象中的引用,因此将它们清空不会有任何区别.

It is certainly NOT done to make the garbage collection faster. A garbage collector doesn't traverse the references in an unreachable object, so nulling them won't make any difference.

更新

该方法的更新版本有以下评论:

A more recent version of the method has these comments:

// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
//   more than one generation
// - is sure to free memory even if there is a reachable Iterator

因此,至少在某些情况下,GC 似乎是有好处的.

So, it appears that there is an benefit for the GC, at least in some cases.

假设老一代中的 Node 包含对年轻一代中对象(例如 Node 或元素)的引用.该引用在收集年轻代时成为根",导致年轻代对象被保留,即使年老代Node不可达.这种状态一直持续到老一代被收集.很少收集老年代.

Suppose that a Node in an older generation contains a reference to an object (e.g. a Node or an element) in a younger generation. That reference becomes a "root" when collecting of the younger generation, causing the young generation object to be retained, even if the old generation Node is unreachable. This state continues until the older generation is collected. Old generations are collected infrequently.

如果你遍历列表并拆除它,包含旧 -> 新引用的变量被分配一个 null.该赋值的写屏障导致(立即或在 GC 时)原始引用不再是根".因此,现在可以收集年轻代中的对象,并且它最终不会永久"到老一代(这将需要收集该代的时间提前).

If you traverse the list and dismantle it, the variable containing the old -> new reference is assigned a null. The write-barrier for that assignment causes (immediately or at GC time) the original reference to no longer be a "root". Thus, the object in the younger generation can now be collected, and it doesn't end up "tenured" to an older generation (which brings forward the time when that generation needs to be collected).

据推测,GC 的好处超过取消选择列表的成本......无论是平均还是在成本灾难性的情况下.

Presumably, the GC benefits outweigh the cost of unpicking the list ... either on average, or in cases where the costs are disastrous.

有关详细信息,请参阅 Jones 和 Lins 的用于动态内存管理的垃圾收集算法".它在我(第一版)副本的第 7.5 章中.

For more information, refer to "Garbage Collection algorithms for dynamic memory management" by Jones and Lins. It is in chapter 7.5 in my (first edition) copy.

一般来说,将 Collection 对象扔掉并重新开始比清除它以供重用要好.

Generally speaking, it is better to throw a Collection object away and start again than it is to clear it for reuse.

这篇关于垃圾收集 - 孤立的 LinkedList 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆