1

I have the below code:

public final class LinkedBag<T> implements BagInterface<T> {
    private Node firstNode;
    private int numberOfEntries;

    public LinkedBag() {
        firstNode = null;
        numberOfEntries = 0;
    }

    // Other methods defined
    public int getCurrentSize() { }
    public boolean isEmpty() { }
    public boolean add(T newEntry) { }
    public T remove() { }
    public boolean remove(T anEntry) { }
    public int getFrequencyOf(T anEntry) { } 
    public boolean contains(T anEntry) { }
    public T[] toArray() { }

    public void clear() { 
        while(!isEmpty())
            remove();
    } 
}

To remove all entries the above version of the clear() method deallocates each node in the chain, thereby making it empty.

However, to remove all entries would the below version of the clear() method deallocate all of the nodes in the chain, thereby making it empty?

    public void clear() { 
        firstNode = null;
    }
myverdict
  • 622
  • 8
  • 24
  • It will not make available rest of the nodes for garbage collection. So you have to do it for all. – royalghost Feb 12 '20 at 04:00
  • @royalghost That's not true. The garbage collector checks whether objects are reachable via live references. This is an easy case because there aren't even any reference cycles. – kaya3 Feb 12 '20 at 04:01
  • @kaya3 but second node is still pointing to third node and third to fourth ... – royalghost Feb 12 '20 at 04:03
  • Read this answer - https://stackoverflow.com/questions/6935579/garbage-collection-orphaned-linkedlist-links – royalghost Feb 12 '20 at 04:07
  • Does this answer your question? [Will link list be deleted if head is set to null in java?](https://stackoverflow.com/questions/37274950/will-link-list-be-deleted-if-head-is-set-to-null-in-java) – Thiyagu Feb 12 '20 at 04:08
  • @royalghost Nothing is pointing to the first node, so the first node is garbage; then the second node is only pointed to from garbage, so it's also garbage. And so on. Your linked answer doesn't contradict this, and in fact says the only difference is in performance. – kaya3 Feb 12 '20 at 04:10
  • I would suggest to write a JUnit Test before conforming it unless someone has read the GC whitepaper. – royalghost Feb 12 '20 at 04:14
  • 1
    @royalghost How can you test this with a unit test? – Thiyagu Feb 12 '20 at 04:16
  • @royalghost Since you are contradicting the answer you cited yourself, I suggest you try testing it. But really, how could the first node not be collected, when nothing points at it? And after the first node is collected, what do you imagine points at the second node? – kaya3 Feb 12 '20 at 04:22
  • I need to again read GC but I though it also looks at the outgoing edge, since the second one has the outgoing edge to third one ( although I agreed it does not have incoming edge ) so there is a possibility of a memory leak... – royalghost Feb 12 '20 at 04:25

3 Answers3

1

Yes, it would. Unlike C/C++ where we have the take care of deallocating the memory we have allocated earlier, Java takes care of collecting the garbage when there are no strong references to an object.

When you make the head of the linked list (firstNode) to null, there wouldn't be (and shouldn't be) any references to that node. Hence, it is eligible for garbage collection (GC). Looking at the second node in the list, there would no other links to it other than the head and hence it is also eligible to be collected.

Extending this, the entire list (all the nodes in the list) can be GCed.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
1

Yes; except you should also remember to set numberOfEntries = 0; too. Then your linked list is in the same state as if a new empty linked list was just created (note that your constructor does the same two things), so it correctly represents an empty list.

Regarding the garbage collector, this will deallocate the removed node objects behind the scenes, automatically, because they are no longer reachable by live references. To avoid memory leaks in garbage-collected languages like Java, the main issue is making sure you don't retain references to data you no longer need. In this case, after setting firstNode = null; your instance retains no references at all, so there is no memory leak.

kaya3
  • 47,440
  • 4
  • 68
  • 97
1

Yes making the first node null will work. As reference to all subsequent nodes will be lost and they will be deallocated automatically by a garbage collector.