Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 932
Rob L Avatar asked Mar 15 '14 21:03

Rob L


People also ask

What's the best way to delete an element from a double linked list?

Deletion in doubly linked list at the beginning is the simplest operation. We just need to copy the head pointer to pointer ptr and shift the head pointer to its next. now make the prev of this new head node point to NULL.

Does doubly linked list have head tail?

A Doubly linked list is a bidirectional linked list; i.e., you can traverse it from head to tail node or tail to head node. Unlike singly-linked lists, its node has an extra pointer that points at the last node.

Do doubly linked lists need a tail?

No, it is not necessary. Below are the time complexities for a doubly-linked list that does not utilize tail pointers. TL;DR: No, it would function almost the same, except for less performant appending cases O(n).

What is head and tail in double linked list?

The head node is the node at the beginning of the list, and the tail node is the node at the end of the list. The head node's previous pointer is set to null and the tail node's next pointer is set to null . Think of your daily commute on the subway as a real-world example of a doubly linked list.


1 Answers

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.

like image 93
NPE Avatar answered Oct 27 '22 00:10

NPE