开发者

Linked List Implementation In Java and Garbage Collection

If I have a linked list structure, and 开发者_如何学编程I implement the clear() method as follows:

public void clear() {
    firstNode = null;
    size = 0;
}

will it still get correctly garbage collected, or would I want to walk through each node, setting nextNode to null?

None of the nodes can be directly referenced from outside the linked list, so there's no case where there would be a reference to any of them outside my class, but I'm unsure if Java would correctly garbage collect the remaining chained nodes.


That should be fine - Java handles cyclic references etc with no problems.


Since none of the nodes of the list have external references. Setting firstNode to null will make all the nodes eligible for GC as there would be no way to reach any of the nodes from an external reference.


for your information, the LinkedList implementation by Sun parse all elements of the list and set them to null

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜