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 5 of 6.
Ravi Kapoor said:
1 decade ago
@Mahi.
I'm pretty much with you on that. The method taking reference variable 'b' must have been executed before referencing it to null. So b has to be eligible for garbage collection only after line 5. I too want answers those who say D.
I'm pretty much with you on that. The method taking reference variable 'b' must have been executed before referencing it to null. So b has to be eligible for garbage collection only after line 5. I too want answers those who say D.
Ashish Mishra said:
1 decade ago
option A
Just bcz of when object reference bcome null then it will eligible to call runtime.gc();
Just bcz of when object reference bcome null then it will eligible to call runtime.gc();
Chandan Kumar said:
1 decade ago
The garbage collector does not indicate when it is going to be called. Hence you can never be certain when GC will be called.
Ormek said:
1 decade ago
The Question is about being *eligible for garbage collection*. Thus it does not matter, when the object actually gets collected. And yes, there is no guarantee that the garbage collector will ever collect a certain object.
Oliver Meyer said:
1 decade ago
Make class A have a static member and make A.s(B) assign its parameter to that member. Then, after the end of method start you can access the object created in line 3 as A.storedB Thus, D is correct.
public class A {
public static B storedB;
public void s(B b) {
storedB = b;
}
}
BTW: The question is not "When will the object be garbage collected?" The Question is: "When is it eligible for garbage collection?
public class A {
public static B storedB;
public void s(B b) {
storedB = b;
}
}
BTW: The question is not "When will the object be garbage collected?" The Question is: "When is it eligible for garbage collection?
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
Prabavathi.V said:
1 decade ago
Actually, garbage collection object values are always unused and it will never be absolutely certain.
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.
Bineeth said:
1 decade ago
Actually, Garbage Collection in Java is Unpredictable. So we can say both options A and D are correct.
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