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 4 of 6.
KotteMani said:
1 decade ago
Here b object is created and then called the parameter after that they assigned to null so there is a chance of garbage collection but the answer is D because already b object is created.
My answer is: D.
My answer is: D.
Raju Rathi said:
1 decade ago
@Yogesh,
What if A has instant member of type B and in method S, we are storing b into that instant member. In that case, even after line 5 object refered by b will be not eligible for garbage collection.
What if A has instant member of type B and in method S, we are storing b into that instant member. In that case, even after line 5 object refered by b will be not eligible for garbage collection.
Sushank Dahiwadkar said:
1 decade ago
According to me option D is correct because there is no definition for function s(). and in function s() another variable may be getting allocated to b's reference.
So D is correct.
So D is correct.
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.
Angelus said:
6 years ago
An object becomes eligible for garbage collection if there is no reference to it or if it has been assigned to null.
So, line 5, assign obj B to null, so I think A is the answer.
So, line 5, assign obj B to null, so I think A is the answer.
(1)
Erik said:
10 years ago
Option D is correct.
Assume a.s (b) adds a reference to the object that b references to a collection of some sort, then that object does not become eligible after Line 5.
Assume a.s (b) adds a reference to the object that b references to a collection of some sort, then that object does not become eligible after Line 5.
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".
Sojin said:
1 decade ago
Option D is correct.
We can't be sure until we know the behaviour of s(b) method. What if this reference is being assigned to a static variable of another class ?
We can't be sure until we know the behaviour of s(b) method. What if this reference is being assigned to a static variable of another class ?
Fayyam said:
1 decade ago
Can anybody (specially those who are saying that option D is correct ans) tell me after line 5th can we call any instance member of class B by using reference b.
Anchal Panda said:
10 years ago
Option A is correct.
As soon as reference b is set to null, b object becomes eligible for garbage collection. The link is removed as soon as it points to null.
As soon as reference b is set to null, b object becomes eligible for garbage collection. The link is removed as soon as it points to null.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers