Embedding Linked List 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 correctly collect garbage, or do I want to go through each node by setting the next Node to null?

None of the nodes can be directly linked to a link outside the linked list, so there is no case when there will be a link to any of them outside my class, but I'm not sure that Java correctly collects the remaining chains of nodes.

+3
source share
3 answers

This should be fine - Java handles circular references, etc. no problem.

+11

. firstNode null GC, .

+2

for your information, the LinkedList Sun implementation parses all list items and sets them to null

+1
source

Source: https://habr.com/ru/post/1735027/


All Articles