Catfish
Catfish

Reputation: 709

SCJP Question to Figure When Object Gets Garbage Collected?

I can't figure out an SCJP questions even after getting the right answer:

From the following code(source: http://scjptest.com), we need to determine when is object referenced as myInt be eligible for garbage collection:

01.public void doStuff() {  
02.    Integer arr[] = new Integer[5];  
03.    for (int i = 0; i < arr.length; i++) {  
04.        Integer myInt = new Integer(i);  
05.        arr[i] = myInt;  
06.    }  
07.    System.out.println("end");  
08.}

The answer states that it is eligible for GC on line 6. But I think the object is simply not eligable for GC until after line 7. Because, the object being referenced as myInt is also referred to as arr[i] as well. So, don't you think, since, after myInt going out of scope, arr[] still has a reference to it till line 8?

Upvotes: 2

Views: 1141

Answers (5)

Stephen C
Stephen C

Reputation: 719709

The reasoning for the SCJP answer is that at line 6 there are no remaining statements in the scope of arr that refer to it. Under normal circumstances, this would make the array and its elements eligible for garbage collection.

(The Java Language Spec (12.6.1) says this:

"A reachable object is any object that can be accessed in any potential continuing computation from any live thread. Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner. "

As you can see, the real definition of reachability is not actually based on scoping.)


There is another twist to this question ...

If they had assigned i to myInt, autoboxing will use Integer.valueOf(i), and that method would have recorded the Integer object in a static cache. This cache would have caused the object to remain reachable ...

However, the Integer instance is created using new, so caching does not occur. And the object is unreachable at line 6.

Upvotes: 5

axtavt
axtavt

Reputation: 242786

From JLS §12.6.1:

A reachable object is any object that can be accessed in any potential continuing computation from any live thread. Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner.

So, under this definition array referenced by arr can be considered unreachable after line 6, therfore its elements are unreachable as well.

Upvotes: 0

Aravindan R
Aravindan R

Reputation: 3084

Contrary to popular belief, Java object variables contain references to objects and not the object itself. When one object variable is assigned to another, the reference gets copied instead of the object. AFAIK GC is for an object and not a reference. We all know the GC claims an object when no reference to it exists.

In my opinion, the object referenced by myInt wont be available for collection until the function doStuff returns (line 8). object referenced by myInt gets stored in arr which is in scope till the function returns.

Upvotes: 1

Steve Kuo
Steve Kuo

Reputation: 63134

arr and myInt are last referenced on line 5. Since it's not referenced in line 7 I can see why line 6 is the stated answer.

Upvotes: 0

Mike Yockey
Mike Yockey

Reputation: 4593

arr[i] = myInt creates a copy of the reference to new Integer(i), not a reference to myInt; therefore, myInt isn't strictly required to exist after that assignment.

Upvotes: 3

Related Questions