Java Programming - Garbage Collections - Discussion

6. 

public Object m() 
{  
    Object o = new Float(3.14F); 
    Object [] oa = new Object[l];
    oa[0] = o; /* Line 5 */
    o = null;  /* Line 6 */
    oa[0] = null; /* Line 7 */
    return o; /* Line 8 */
}
When is the Float object, created in line 3, eligible for garbage collection?

[A]. just after line 5
[B]. just after line 6
[C]. just after line 7
[D]. just after line 8

Answer: Option C

Explanation:

Option A is wrong. This simply copies the object reference into the array.

Option B is wrong. The reference o is set to null, but, oa[0] still maintains the reference to the Float object.

Option C is correct. The thread of execution will then not have access to the object.


Jit said: (Feb 23, 2011)  
Somewhere the answers are given to be option B i.e. after Line 6. Which one is correct?

Sireesha said: (Mar 25, 2011)  
Can you please give me the clear examples for type casting done in both primitive datatypes and non-primitive datatypes.

Nadin said: (Jul 18, 2014)  
Why after the line 6 oa[0] is still equal to 3.14? Is the value of an object is not passed by reference?

Neha said: (Nov 30, 2014)  
It should be after line 6 as per my knowledge since when oa[0] = o, both the handles are referencing to the location created in line 3 and since oa[0] has its individual existence now, when o=null then it becomes eligible for garbage collection.

Sasikanta said: (Jul 30, 2017)  
Garabage Collection rule says when, an object reference is nullyfy, then it is eligible for garbage collection.

Rule 1 : Nulling a Reference. Ex.

public static void main(String [] args) {
StringBuffer sb = new StringBuffer("hello");
System.out.println(sb);
// The StringBuffer object is not eligible for collection
sb = null;
// Now the StringBuffer object is eligible for collection
}
}

rule 2: Reassigning a Reference Variable

EX.

StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("goodbye");
System.out.println(s1);
// At this point the StringBuffer "hello" is not eligible
s1 = s2; // Redirects s1 to refer to the "goodbye" object
// Now the StringBuffer "hello" is eligible for collection

rule 3 : Isolating a Reference

ex:

Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
i2.i = i3; // i2 refers to i3
i3.i = i4; // i3 refers to i4
i4.i = i2; // i4 refers to i2
i2 = null;
i3 = null;
i4 = null;
// do complicated, memory intensive stuff
}
}
When the code reaches // do complicated, the three Island objects. (previously known as i2, i3, and i4) have instance variables so that they refer to each other, but their links to the outside world (i2, i3, and i4) have been nulled. These three objects are eligible for garbage collection.

So, ans should be option B.

Nikhil said: (Jul 11, 2021)  
Its a very simple thing;

A->X
B->A i.e B->X
Now A and B both belong to X.
Now A->NULL,
But still B-> X right,
But in line 7 B-> NULL.
So, X is free to remove and that is why it is eligible for garbage collection.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.