Reputation: 13
I'm reading the Effective Java book and its saying that eliminating obsolete reference is one of best way to avoid memory leaks. according to the below program, by doing -> elements[size] = null; its eliminating obsolete references in that program. My problem here what is the advantage of doing elements[size] = null;. Any other program can use that freed memory location? Or is it garbage collected? According to my understanding the array is already allocated the memory for its size. Even we do elements[size] = null; anyone can't use that freed memory location until you do elements = null;. Please someone tell me what is advantage of doing elements[size] = null; here.
public Object pop() {
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
Upvotes: 1
Views: 783
Reputation: 140573
There are two cases we have to distinguish:
Upvotes: 1
Reputation: 7290
Let's imagine, elements
is a 20-elements Object
array (elements = new Object[20];
), and has been filled with 18 BigInteger
instances, the remaining two places being null.
So the heap now contains 18 BigInteger
instances and a 20-elements Object[]
array. The garbage collector won't reclaim any of these instances, and that's okay as you'll most probably use them later (via the pop()
method).
Now you call the pop()
method to get the BigInteger
most recently added to the array. Let's assume you just want to print it and then forget it, so in your overall application that number isn't needed any more, and you'd expect the garbage collector to reclaim it. But that won't happen unless you do the null assignment
elements[size] = null; // Eliminate obsolete reference
As long as you store the reference to an object in some accessible place, the garbage collector believes that you'll still need the object later.
As long as elements[17]
still refers to the BigInteger
, it can potentially be accessed by your program, so it can't be reclaimed. If elements[17]
points to null, the BigInteger
that used to be there isn't accessible via elements
any more and can be reclaimed by the garbage collector (if no other part of your code still uses it).
It's only worth thinking about "obsolete references" if you have a long-living storage structure that contains fat objects, and you can tell at some point in time that you won't need one of the stored objects any more. As you won't need this object any more, you can now re-assign the storage with null
, and then the GC no longer believes you still need the object and is able to reclaim the storage space.
Upvotes: 0
Reputation: 131486
My problem here what is the advantage of doing elements[size] = null;.
Here obsolete references refer to object references not required any longer for the program.
You want that unnecessary objects to be free to consume only memory that your program need. Generally it is done for the good working of the current application.
Any other program can use that freed memory location?
Theoretically yes but it also depends on the JVM memory options used. You don't generally focus on it.
elements[size] = null
and elements = null;
don't have at all the same intention and the same effects.
In the context of the book, elements
is a structural intern of a class.
The idea is that some elements of the array may be stale and not required any longer after some removal operations.
The first one (elements[size] = null
) will make the object of the array element located at the size
index to be eligible to be GC if no other objects reference .
But the second one (elements = null
) is much more. It will make all elements of the array to be eligible to be GC if no other objects reference it.
Upvotes: 2