Does setting head and tail references on a doubly linked list in Java REALLY clear it from memory?

StackOverflow https://stackoverflow.com/questions/22429716

  •  15-06-2023
  •  | 
  •  

Question

The other day my professor in my data structures course (Java) said, "okay guys, how can we clear this n element doubly linked list from memory? ". I uttered out loud "set the head and tail to null". He replied "Ok, but does that really clear it from memory?" I said "yeah I think so".

Anyhow, he went on to say that since the nodes in the list have references back and forth to one another, it does not really clear it from memory. I imagine we are looking at something like this:

        Head                                                                     Tail
null <-[null/data/node 2]-> <-[node1/data/node3]-> <-[node2/data/node4]-> <-[node3/data/null]->null

Lets assume this is the typical doubly linked list data structure in Java, where Node is a class (inner or separate class) and the only instance variables in the DoublyLinkedList class are

 Node head;

and

 Node tail;

He was saying that because in the inner portion of the list each node has a reference to a next node, and that node has a reference to the previous node. He said this wouldn't be the case in a singly linked list because the references are only "a one way street" and once you lose reference to the first one, they all end up getting garbage collected.

Of course, everyone in the class shared their opinion but no one in the class is anyone I would ever listen to or take programming advice from, and I seem to get conflicting information between when I ask a question on Stack Overflow versus what my professor tells me. I'll be honest, I don't have enough experience to chime in on the matter but simply put, I don't know if I always believe everything he says because he was wrong on one or two other things I asked him about.

So, does setting

head = null;

and

tail = null;

clear the entire list from memory? If so how can I verify this? If not, then what is the general idea of a solution to get rid of the list? Node by node deletion? Please leave your thoughts and comments.

Was it helpful?

Solution

You are correct: any modern JVM will reclaim memory even in the presence of circular references (as long as it's not reachable from a live reference). This means that your doubly-linked list will indeed be garbage collected, as long as there are no live references to any of its nodes.

I'd suggest reading up on mark-and-sweep garbage collectors.

The basic idea is that the garbage collector starts with a bunch of "roots" (i.e. known live objects) and follows all reference chains to the end. Any objects that have not been reached during this process are considered eligible for garbage collection, since there's no way they can be accessed by the program.

Actual garbage collectors employed in modern JVMs are considerably more sophisticated than this, but this should give you the basic idea.

Your professor is probably thinking of reference counting. While some languages (for example, CPython) do use reference counting for garbage collection, this is rarely the only mechanism precisely because it can't deal with circular references. FWIW, I have never come across a JVM that would use reference counting for garbage collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top