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.
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.
Anuja said:
10 years ago
Please explain about option D.
Mit said:
1 decade ago
Option A is correct Even if we assign that object reference to some other B = null removes from B. Therefore b will be eligible after line 5.
Konapuram malli said:
1 decade ago
Option A is correct because of that object reference is null.
Fayyam said:
1 decade ago
Normal method call hold the control till then its execution not completed(unlike run() method in multithreading programming ) so what ever the implementation of a.
s() once that line will be encounter by JVM control will go to the class A and start the execution of s() method and after completing its execution it will again come back to line number 5 where object b assign with null. If you try to use object b after line no 5 you will get NullPointerException.
So Answer A is correct.
s() once that line will be encounter by JVM control will go to the class A and start the execution of s() method and after completing its execution it will again come back to line number 5 where object b assign with null. If you try to use object b after line no 5 you will get NullPointerException.
So Answer A is correct.
Dong said:
1 decade ago
D is correct, it depends on the implementation of A.s(). e.g. in the following case, b is not eligible for gc after line #5:
--------
class A {
B bInA;
public void s(B b) {
bInA = b;
}
}
--------
In this case, b is only eligible for gc after line #6, when A is also eligible for gc.
--------
class A {
B bInA;
public void s(B b) {
bInA = b;
}
}
--------
In this case, b is only eligible for gc after line #6, when A is also eligible for gc.
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.
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.
Ramaswamy said:
1 decade ago
I think D is correct.
1. In Java you always pass by value, which in this case means a copy of the reference to object 'b' is made and passed to the function s.
2. As many have pointed out above, in function 's', many things could be happening. You may be setting a static variable in another class (or even the enclosing class of the present start() function) to be equal to this reference. You could also be starting a new thread in function 's', which may be using the reference. That thread could continue to execute even after the program has left the current start() function.
Therefore, with the information given to us, you cannot say when the object pointed to by 'b' will be eligible for garbage collection.
1. In Java you always pass by value, which in this case means a copy of the reference to object 'b' is made and passed to the function s.
2. As many have pointed out above, in function 's', many things could be happening. You may be setting a static variable in another class (or even the enclosing class of the present start() function) to be equal to this reference. You could also be starting a new thread in function 's', which may be using the reference. That thread could continue to execute even after the program has left the current start() function.
Therefore, with the information given to us, you cannot say when the object pointed to by 'b' will be eligible for garbage collection.
Arnold villasanta said:
1 decade ago
Yup, D is correct.
No one knows how instance 'a' used 'b' in its method.
No one knows how instance 'a' used 'b' in its method.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers