Reputation: 31397
A method given to me and asked, when it will be eligible for Garbage Collection, i mean at which line. I believe both o
and oa
are eligible for garbage collection. Since, they are set to null
. Please correct me, if i'm wrong. But, the question was, when it will be eligible for gc
, i mean at which line. ?
public Object m() {
Object o = new Float(3.14F);
Object[] oa = new Object[1];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}
Please anyone explain. ?
Upvotes: 0
Views: 1141
Reputation: 48687
Please correct me, if i'm wrong. But, the question was, when it will be eligible for gc, i mean at which line. ?
The question is nonsensical or based upon an over-simplified model of garbage collection. Garbage collectors act at run-time on the available representation which means tracing reachability from global roots including the registers, stack and global variables. There is no correspondance with high-level source code so it does not make sense to ask about line numbers. In reality, the compiler will mangle your code beyond all recognition before the GC runs.
Upvotes: 0
Reputation: 24316
Let us walk through the code:
1) o = new Float();
2) oa = new Object[];
at this point we have 2 objects.
3) oa[0] = o;
at this point oa[0] holds the reference of o.
4) o = null;
o is still being referenced by oa[0]
5) oa[0] = null
o now has zero references.
6) return o;
o is null.
Line 7 is where the GC eligibility happens for o
. oa
is not eligible for GC until the function exits.
In general, an object is only eligible for GC when there are no references left to it. Be very careful when dealing with a String
as there is a special place called the String pool. So the following code:
void foo()
{
String s = "foo";
s=null;
return s;
}
At no point is s
guaranteed to be eligible in the function.
Question from comments
one question, you said..oa is not eligible for GC until the function exits. but, before the return o, oa set to be null and it nowhere referred too
Answer:
oa is not set to null. What gets set to null is the object at oa[0] (the first index of oa). If the line was oa = null that would be true, and irrespective of the only item in oa being null, does not in fact make the wrapper (in this case an array) null. Similar to having a List and nulling out all of its elements does not make the List null.
Upvotes: 4
Reputation: 25695
AFAIK, Java uses the Mark-And-Sweep algorithm. The GC runs in a separate thread over the heap repeatedly.
If a certain reference variable(or object) goes out of scope, its marked for deletion and sweeped off when appropriate(or overwritten).
Also whenever the variable is set to null, that is when the reference count object becomes unreachable by static references and other threads, that is when its reference no longer exists, its marked for deletion again.
Line7 in your code is where it will be marked for deletion.
Upvotes: -1
Reputation: 1506
Garbage collection is eligible when no reference to that object exists any longer, but it can be important to note that GC is not guaranteed to run at that time, so technically the memory can be allocated.
oa[0] refers to the object, so when you set it to null at line 7, no reference to that object exists any longer, so it is eligible for GC.
As pointed out on comments, oa, the array itself, is still around until the method is done executing, i.e. after line 8 executes. It is local to the lifetime of the method m(), so it will be eligible for GC when m() returns from execution.
Upvotes: 1
Reputation: 120486
Java is allowed to do certain optimizations, so an optimizing JIT that reasons only about local-effects could simplify this code to
public Object m() {
//Object o = // o does not participate in any externally visible side-effect
new Float(3.14F); // Available for collection as soon as ctor finishes.
//Object[] oa = new Object[1]; // Array ctors are known not to have side-effects, and oa does not participate in a side-effect or result that is visible outside the method.
//oa[0] = o; /* Line 5 */ // Side-effect not visible.
// o = null; /* Line 6 */ // Side-effect not visible.
//oa[0] = null; /* Line 7 */
//return o; /* Line 8 */ // Eliminated by inlining.
return null; // Type-analysis proves that this method can only return null.
}
Don't assume that assignments of local variables to null
actually happen in long-lived method calls.
Upvotes: 3