Night0
Night0

Reputation: 347

Java Garbage Collection: Does setting 'top of stack' to null in pop() method only GC the reference (not object)?

I'm reading an older version of a Java book that suggests setting a reference to null in pop() helps with garbage collection, specifically for popping an object off a stack array.

The top object is returned from the pop() method via another reference, so I assume dereferencing it to null only garbage collect the array's reference (not the object)?

Here's the pop() method in the Stack Array:

public Object pop() throws EmptyStackException {
  Object elem;
  .
  .
  elem = S[top];
  S[top--] = null; // dereference S[top] for garbage collection
  return elem;
}

So, the object of S[top] would be referenced by both S[top] and elem. After dereferencing S[top], elem still points to this object. Then this object is returned, elem reference destroyed, but object will be assigned another reference in the caller method. So, this object on the heap is still there, just that S[top] no longer points to it.

This seems legit because S[top] shouldn't reference the object after it's been popped, but how does it help with garbage collection? only on the reference?

Do we need to do this? Does it help with efficiency?

Upvotes: 1

Views: 1206

Answers (3)

jaco0646
jaco0646

Reputation: 17104

This exact scenario is covered extensively in Effective Java (2nd edition) Item 6. The short answer is that yes, it is beneficial to null out the reference after popping it.

I think the following warning is more important, however.
"When programmers are first stung by this problem, they may overcompensate by nulling out every object reference as soon as the program is finished using it. This is neither necessary nor desirable... Nulling out object references should be the exception rather than the norm."

On a side note: dereferencing means reading the value of a variable. It has nothing to do with null or setting a value.

Upvotes: 2

Arbiter
Arbiter

Reputation: 433

Yes you do, otherwise the element in the array would turn into an obsolete object, where it is invisible from the garbage collector, therefore will never be collected.

Referencing it to null can cause the object to be eligible for collection (unless there are other active references anywhere else). Since the object reference is null, there is no actual object, just null which does not use memory. Therefore it is crucial to do this, otherwise you could run into a memory leak.

Upvotes: 0

rgettman
rgettman

Reputation: 178303

It is worth it to set S[top] to null. The reference to the popped object is returned to the caller who can do anything to that object. When the caller is done with that object, then it no longer needs to be referenced by either the caller or the Stack. As long as no other object references it, then it is eligible for garbage collection.

If the Stack didn't clear that entry, then nothing would make it eligible for garbage collection, because the Stack would continue holding a reference to that object even after it was popped.

In the Stack, the array and any references to objects in it isn't garbage collected. The array in the Stack still exists as long as the Stack does. The reference element is just set to null. The reference still exists, but it's now null and it no longer refers to the object.

Clearing the reference helps with memory efficiency, by allowing garbage collection to reclaim the memory on the popped object, assuming that nothing else refers to that object as well. Without clearing the reference, the object would never be garbage collected, unless the Stack itself (with its array referring to the object) was garbage collected.

Upvotes: 3

Related Questions