Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java loitering and garbage collection

I have this bit of code for the Stack's pop method and i'm trying to figure out how it avoids loitering while still returning the element that our index is currently pointing to:

public String pop()
{ // Remove item from top of stack.
String item = a[--N];
a[N] = null; // Avoid loitering (see text).
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}

From what i can understand we are pointing the reference item of our String object to the indexed element(we start from the last element using it's current size N-1 hence the decrements) of our array a. Then if we are returning the reference why are we setting that indexed element that our reference is pointing to null before doing so? Doesnt that make the item point to nothing and return nothing?

like image 422
user2644819 Avatar asked Aug 07 '13 17:08

user2644819


People also ask

What is loitering in Java?

The Java garbage collector determines that memory is garbage if there are no accessible references to it. Loitering is a bug in which there is memory that will never be used, but also cannot be collected because there are still references to it.

How to prevent an object from getting garbage collected in Java?

We have three ways to achieve same - 1) Increasing the Heap -Eden space size . 2) Create Singleton class with Static reference . 3) Override finalize() method and never let that object dereference.

How does garbage collection works in Java stack overflow?

Garbage collection refers to the process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program. The heap is a memory which is referred to as the free store, represents a large pool of unused memory allocated to your Java application.


2 Answers

This copies the reference in the array. It doesn't reference the array member.

 String item = a[--N];

Now you have two references to the same object, one in the local variable, and one in the array. This removes the copy in the array:

 a[N] = null; // Avoid loitering (see text).

If it were not removed from the array, then an unnecessary reference would continue to exist, preventing garbage collection.

like image 122
Andy Thomas Avatar answered Sep 18 '22 14:09

Andy Thomas


An object can't be garbage collected as long as it is reachable. If you simply change your index with --N but do not nullify a[N], you will keep a reference to that object, preventing its garbage collection even if the client code does not reference that object any longer.

That is one of the only cases where you need to nullify a variable in Java.

You also seem to misunderstand what references are. a[N] contains a value that points to an object in memory. When your write String item = a[N], you copy that value to the variable item. Both variables (item and a[N]) now refer to the same object. When you then write a[N] = null, you remove that reference from the array but item still contains a value that points to the original object.

like image 43
assylias Avatar answered Sep 19 '22 14:09

assylias