Java Programming - Garbage Collections - Discussion
Discussion Forum : Garbage Collections - General Questions (Q.No. 1)
1.
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */
}
When is the B object, created in line 3, eligible for garbage collection?Discussion:
59 comments Page 3 of 6.
Anish Kumar said:
9 years ago
I think no any option is a right answer. Because when we are calling s method then there is a possibility of reassignment of the object in method but it is local, it will be executed by execution engine of java after completion of (s) method all object will be eligible for GC. So just before line number 5, all b object will be eligible for GC. Please, anyone explain to me if I am wrong.
Nikhil Nambiar said:
9 years ago
Option D is right because there is no assurance of garbage collector activity. And we cannot even predict the implicit call to finalize method.
Mukesh said:
9 years ago
Option D is correct Because JVM runs garbage collection without knowing of the user. So, even if we run the finally we just remove some resources, not the complete object is removed.
Yuvaraju said:
9 years ago
We can assign a null to the object, so option D is correct.
Mayank said:
9 years ago
We are passing value null for reference of Object not to object, so Object will be available for garbage after line 5, after line 5 no one access Object of B class OR we can say we will not be able to access instance member of B class we can only access Reference of B Class.
So, my answer is A.
So, my answer is A.
TheExodu5 said:
8 years ago
@Mayank.
The reference was passed to A.s. That reference may still exist, and the original object can still be accessed.
The answer is D.
The reference was passed to A.s. That reference may still exist, and the original object can still be accessed.
The answer is D.
Sasikanta said:
8 years ago
(A) is correct arguably. There is no reference running after this line.
Here the question is "when object b is eligible for garbage collection" not system.gc() will collect it or about its timing. So if the question is about eligible then no doubt it is after line five, which is a basic rule (by Effective Java [Joshua Bloch] ).
Here the question is "when object b is eligible for garbage collection" not system.gc() will collect it or about its timing. So if the question is about eligible then no doubt it is after line five, which is a basic rule (by Effective Java [Joshua Bloch] ).
Turgay Ekici said:
8 years ago
(A) Correct,
If the question was in which point that JVM will collect, the answer would be (D).
But the question is "when the object is eligible to be collected".
If the question was in which point that JVM will collect, the answer would be (D).
But the question is "when the object is eligible to be collected".
Zafar said:
6 years ago
A is correct , because while passings(b) we just pass the value of whatever in b which will further get collected in method S(B newXyz) so there is no b at all, after line 5, b is referred to null and will be GC'd.
Utkarsh said:
5 years ago
I believe answer should be B.
Clearly we still have a.s referencing B object (new B()) and only after a is set to null the whole chain would break.
Clearly we still have a.s referencing B object (new B()) and only after a is set to null the whole chain would break.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers