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.