Java Programming - Garbage Collections - Discussion
Discussion Forum : Garbage Collections - General Questions (Q.No. 6)
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?Answer: Option
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.
Discussion:
6 comments Page 1 of 1.
Nikhil said:
4 years ago
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.
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.
(1)
Sasikanta said:
8 years ago
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.
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.
Neha said:
1 decade ago
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.
(1)
Nadin said:
1 decade ago
Why after the line 6 oa[0] is still equal to 3.14? Is the value of an object is not passed by reference?
Sireesha said:
1 decade ago
Can you please give me the clear examples for type casting done in both primitive datatypes and non-primitive datatypes.
Jit said:
1 decade ago
Somewhere the answers are given to be option B i.e. after Line 6. Which one is correct?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers