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 2 of 6.
Shankar Shingare said:
1 decade ago
Option A is correct.
1. It is ask for garbage collection, so when no possible reference to object, it is eligible for garbage collection i.e. after line 5.
2. It may b possible that it is being used in the method s. But still that method gets executed before the 5th line. And 5th line gets executed only after we are out of the method "s". So there is no possible reference to object b.
1. It is ask for garbage collection, so when no possible reference to object, it is eligible for garbage collection i.e. after line 5.
2. It may b possible that it is being used in the method s. But still that method gets executed before the 5th line. And 5th line gets executed only after we are out of the method "s". So there is no possible reference to object b.
Vikash Kumar said:
6 years ago
Yes D should be the best answer. Because the we need to understand b is not an object but a reference variable which holds the reference of the an object of type. Since it assigned to null in line no 5 it doesn't mean that it couldn't be assigned to other reference variable. It totally depends on the definition of the method s(). That is why in my view D is the best answer.
(2)
Sang Dang said:
1 decade ago
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
}
I also think that the answer should be D.
1. If the function a.s(b), keep the pointer to b object so then the b should eligible for GC after line 6.
2. If the function do nothing, so b should eligible for GC after line 5.
So my answer is D.
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
}
I also think that the answer should be D.
1. If the function a.s(b), keep the pointer to b object so then the b should eligible for GC after line 6.
2. If the function do nothing, so b should eligible for GC after line 5.
So my answer is D.
Yogesh Prajapati said:
1 decade ago
Option [A] is correct:
Above question asks for eligibility of object for garbage collection.So as soon as all references of the objects are removed that objects becomes eligible for garbage collection.
But it is totally specific to JVM implementation of garbage collection algorithm when the object will be actually collected by garbage collector.
Above question asks for eligibility of object for garbage collection.So as soon as all references of the objects are removed that objects becomes eligible for garbage collection.
But it is totally specific to JVM implementation of garbage collection algorithm when the object will be actually collected by garbage collector.
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] ).
Gayachand Sahoo said:
1 decade ago
Answer : A.
Because All method call will stored in the stack area. And object created in the heap area. So when method S of A class executed at.
That moment b object lost for that method only but b object will also.
Be alive in the Heap area.
Object b will be garbage collected when we explicitly mention that ref as null.
Because All method call will stored in the stack area. And object created in the heap area. So when method S of A class executed at.
That moment b object lost for that method only but b object will also.
Be alive in the Heap area.
Object b will be garbage collected when we explicitly mention that ref as null.
Anomi said:
8 years ago
Option [B] is most accurate.
We do not know what exactly method s does but let's suppose that it creates reference to b in a. Then gc cannot destroy b cause we still have object a that has reference to b. Therefore we need to get rid of object a, and then after line 6 we can be sure that object b is eligible for gc.
We do not know what exactly method s does but let's suppose that it creates reference to b in a. Then gc cannot destroy b cause we still have object a that has reference to b. Therefore we need to get rid of object a, and then after line 6 we can be sure that object b is eligible for gc.
(2)
Ratish Acharya said:
1 decade ago
Answer should be [A].
The truth is that,generally an object becomes eligible for garbage collection in Java on following cases:
1) All references of that object explicitly set to null e.g. object = null
2) Object is created inside a block and reference goes out scope once control exit that block
The truth is that,generally an object becomes eligible for garbage collection in Java on following cases:
1) All references of that object explicitly set to null e.g. object = null
2) Object is created inside a block and reference goes out scope once control exit that block
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.
Rajesh Kumar Nayak said:
1 decade ago
I think option D is correct because the definition of s(b) method is not given here. It might be the case that A has a class level variable and in s(b) the class A might be assigning the value to the class level variable.
So the reference of b would still exist even after we set a=null;
So the reference of b would still exist even after we set a=null;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers