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.
Bineeth said:
1 decade ago
Actually, Garbage Collection in Java is Unpredictable. So we can say both options A and D are correct.
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.
Prabavathi.V said:
1 decade ago
Actually, garbage collection object values are always unused and it will never be absolutely certain.
Ankur said:
1 decade ago
That is totally depend on the jvm implementation and algorithm that provide how and which time to rin gc() method we don't need to worry about it.
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?
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.
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();
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.
Mahi said:
1 decade ago
Can anyone tell(THOSE WHO SAY D I CORRECT) through which reference variable we can access object of B.. coz after 5th line b is pointed to null.. this implies there is no further reference to object.. 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 THE OBJECT b...
AND
@Ranjani: u r wrong.. whenever an object is passed it is done via call by reference.. yeah its true that in general we don't pass arguments via call by ref. in java but.. object is done via call by ref. only...
AND
@Ranjani: u r wrong.. whenever an object is passed it is done via call by reference.. yeah its true that in general we don't pass arguments via call by ref. in java but.. object is done via call by ref. only...
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers