简体   繁体   English

Java 的 LinkedList 中的 clear() impl

[英]clear() impl in Java's LinkedList

I fear this is a really stupid question, but here goes:我担心这是一个非常愚蠢的问题,但这里是:

Why does the clear method in Java's default LinkedList implementation bother to walk the list and unhook all the nodes?为什么 Java 的默认 LinkedList 实现中的 clear 方法麻烦地遍历列表并解开所有节点? Why not just unhook the header and leave the rest of the list connected -- the GC will get it anyway, no?为什么不只是解开标题并保持列表的其余部分连接 - GC 无论如何都会得到它,不是吗?

Here's the method:这是方法:

/**
 * Removes all of the elements from this list.
 */
public void clear() {
    Entry<E> e = header.next;
    while (e != header) {
        Entry<E> next = e.next;
        e.next = e.previous = null;
        e.element = null;
        e = next;
    }
    header.next = header.previous = header;
    size = 0;
modCount++;
}

Why walk it?为什么要走呢? Why not just skip to header.next = header.previous = header;为什么不直接跳到header.next = header.previous = header; ? ?

Best I can figure is it helps the GC...?我能想到的最好的方法是它有助于 GC ......? This link http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997442 sort of suggests that.这个链接http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997442有点暗示。

TIA...蒂亚...

Their method ensures that even if other code still holds references to particular nodes, the other nodes will be GC'ed.他们的方法确保即使其他代码仍然持有对特定节点的引用,其他节点也将被 GC 处理。

Otherwise, even a single external reference to one of the nodes would prevent the entire chain from being collected.否则,即使是对其中一个节点的单个外部引用也会阻止收集整个链。

Also, other operations in the list might be going on simultaneously (eg views through subList() or Collections.unmodifiableList() , iterators), and this ensures that those things perceive the list as "empty" immediately.此外,列表中的其他操作可能会同时进行(例如,通过subList()Collections.unmodifiableList()进行subList() 、迭代器),这确保这些操作立即将列表视为“空”。

IIRC, this was a change made in JDK6 to assist performance of certain (generational) GC algorithms. IIRC,这是在 JDK6 中进行的一项更改,以帮助某些(分代)GC 算法的性能。 Often, the List itself and older nodes will be in an older generation than some of the other nodes.通常, List本身和较旧的节点将比其他一些节点位于较旧的一代中。 The younger generations will get collected more frequently, with the result that young nodes get copied about before it is discovered that all the nodes are garbage.年轻一代将更频繁地被收集,结果年轻节点在发现所有节点都是垃圾之前就被复制了。

So it's a minor performance optimisation.所以这是一个小的性能优化。 Memory performance optimisation is a little odd in that often it's not the code which is causing the problem that is taking the additional time to execute.内存性能优化有点奇怪,因为它通常不是导致问题的代码需要额外的时间来执行。

The source code of java.util.LinkedList at http://developer.classpath.org/doc/java/util/LinkedList-source.html suggests that you can simply set the first and last elements to null. http://developer.classpath.org/doc/java/util/LinkedList-source.html 上java.util.LinkedList的源代码建议您可以简单地将第一个和最后一个元素设置为 null。

Of course if you tend to be over protective, you can loop through the whole thing.当然,如果你倾向于过度保护,你可以遍历整个事情。 I personally think that this may be a very expensive task if your list hold several thousands elements.我个人认为,如果您的列表包含数千个元素,这可能是一项非常昂贵的任务。

I was just speculating on this very issue on my game development blog.我只是在我的游戏开发博客上推测这个问题。 Thanks for the answer.谢谢你的回答。 I'd argue that node exposure was a questionable design allowance.我认为节点暴露是一个有问题的设计余量。 It's also sketchy that alternate views on the list (iterators and such) would rely on node unlinking to fail-fast.列表上的替代视图(迭代器等)将依赖节点取消链接以快速失败,这也是粗略的。 Instead of relying on this side-effect behavior, sub-views on the list should be checking the modification count.列表上的子视图应该检查修改计数,而不是依赖这种副作用行为。 In any case, I see why they're stuck with it now.无论如何,我明白为什么他们现在坚持下去了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM