OpenJDK's LinkedBlockingQueue implementation: Node class and GC [duplicate]

喜欢而已 提交于 2019-12-31 02:56:09

问题


I'm a little confused by the structure of the Node class in OpenJDK's implementation of LinkedBlockingQueue (in java.util.concurrent).

I've reproduced the description of the node class below:

static class Node<E> {
    E item;

    /**
     * One of:
     * - the real successor Node
     * - this Node, meaning the successor is head.next
     * - null, meaning there is no successor (this is the last node)
     */
    Node<E> next;

    Node(E x) { item = x; }
}

Specifically, I'm confused on the 2nd choice for next ("this Node, meaning successor is head.next").

This seems to be directly related to the dequeue method, which looks like:

private E dequeue() {
    // assert takeLock.isHeldByCurrentThread();
    // assert head.item == null;
    Node<E> h = head;
    Node<E> first = h.next;
    h.next = h; // help GC
    head = first;
    E x = first.item;
    first.item = null;
    return x;
}

So we've removed the current head, and we're setting its next to be itself to "help GC".

How does this help GC? How helpful is it to GC?


回答1:


I asked Professor Doug Lea, author of the code, and he said that it reduces the chances of GC leaving floating garbage.

Some useful resources on floating garbage:

http://www.memorymanagement.org/glossary/f.html#floating.garbage

http://java.sun.com/docs/hotspot/gc1.4.2/#4.4.4.%20Floating%20Garbage|outline

http://blog.johantibell.com/2010/04/generational-garbage-collection-and.html



来源:https://stackoverflow.com/questions/10106191/openjdks-linkedblockingqueue-implementation-node-class-and-gc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!