I'm looking at a method (delMin()) for deleting the small element in a heap. It sets the variable min
to equal the min value and then sets the value of min to null
to "avoid loitering and help with garbage collection." Could someone please explain how the JVM deals with this and why this is a good idea?
public Key delMin() {
if (isEmpty()) throw new NoSuchElementException("Priority queue underflow");
exch(1, N);
Key min = pq[N--];
sink(1);
pq[N+1] = null; // avoid loitering and help with garbage collection
if ((N > 0) && (N == (pq.length - 1) / 4)) resize(pq.length / 2);
assert isMinHeap();
return min;
}