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 1 of 6.
Shailesh suryawanshi said:
8 years ago
Here the object is created at line 3 by the new method. And this object created is referenced by b.
This b is then used by the method of a.
The object is eligible for garbage collection if and only if all the references to that object is set to null. That is De-referencing the object.
But since the object is passed in method and it may have its valid use that the object in the method is referenced by some other object reference say BRefernece=b;.
Since the object is on Hip memory and it is referenced by BReference. But though the other reference b is set to null the object remains on heap with a reference b.
The Analogy would be you have a remote control for tv and that tv you have given to your friend. Your tv had 'b' remote control at your home. Let's say you forgot to give remote to your friend. Mean while your friend bought new remote 'B_Reference'. Since your remote control can't operate the tv that means it is set to null. And the tv has got new remote control 'B_reference'. That means your tv is accessible. Implies that you will not give the tv to bhangarwala. That is garbage collection.
If the tv did not had remote control it was eligible for garbage collection, but you have the remote control that's why it is not eligible for garbage collection.
This b is then used by the method of a.
The object is eligible for garbage collection if and only if all the references to that object is set to null. That is De-referencing the object.
But since the object is passed in method and it may have its valid use that the object in the method is referenced by some other object reference say BRefernece=b;.
Since the object is on Hip memory and it is referenced by BReference. But though the other reference b is set to null the object remains on heap with a reference b.
The Analogy would be you have a remote control for tv and that tv you have given to your friend. Your tv had 'b' remote control at your home. Let's say you forgot to give remote to your friend. Mean while your friend bought new remote 'B_Reference'. Since your remote control can't operate the tv that means it is set to null. And the tv has got new remote control 'B_reference'. That means your tv is accessible. Implies that you will not give the tv to bhangarwala. That is garbage collection.
If the tv did not had remote control it was eligible for garbage collection, but you have the remote control that's why it is not eligible for garbage collection.
(1)
Vikesh said:
1 decade ago
Consider the following:
class A {
public void s(final B b) {
new Thread() {
public void run() {
b.doSomething();
//This is a long running thread. doing something on b
}
}.start();
}
}
class B {
public void doSomething() {
}
}
class MyAwesomeClass{
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 */
}
}
Now, I think things should get pretty clear. There is obviously no way it can be told. Even after line 7 has executed, B (created at line 3) might not be eligible for garbage collection. More so, it is possible that object isn't eligible even after start() has finished. We are talking about object not the reference to object. All that is happening above is reference to object is made null - the real object may still be there somewhere, which we don't know.
class A {
public void s(final B b) {
new Thread() {
public void run() {
b.doSomething();
//This is a long running thread. doing something on b
}
}.start();
}
}
class B {
public void doSomething() {
}
}
class MyAwesomeClass{
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 */
}
}
Now, I think things should get pretty clear. There is obviously no way it can be told. Even after line 7 has executed, B (created at line 3) might not be eligible for garbage collection. More so, it is possible that object isn't eligible even after start() has finished. We are talking about object not the reference to object. All that is happening above is reference to object is made null - the real object may still be there somewhere, which we don't know.
Ghaida Daya said:
7 months ago
After line 6.
Here's why:
Line 3: A B object is created, and the reference b points to it.
Line 4: The method a.s(b) establishes a connection between the A object and the B object (assuming the method s(B b) in class A holds a reference to the B object, e.g., by assigning it to an instance variable).
Line 5: The reference b is set to null. However, this does not make the B object eligible for garbage collection because the A object still holds a reference to it through its method s.
Line 6: The reference a is set to null. Now, there are no remaining references to the A object or the B object. Both objects are now eligible for garbage collection, as they are no longer accessible.
Line 7: This line simply prints to the console and does not affect the eligibility of the objects for garbage collection.
Final Answer:
The B object becomes eligible for garbage collection after line 6.
Here's why:
Line 3: A B object is created, and the reference b points to it.
Line 4: The method a.s(b) establishes a connection between the A object and the B object (assuming the method s(B b) in class A holds a reference to the B object, e.g., by assigning it to an instance variable).
Line 5: The reference b is set to null. However, this does not make the B object eligible for garbage collection because the A object still holds a reference to it through its method s.
Line 6: The reference a is set to null. Now, there are no remaining references to the A object or the B object. Both objects are now eligible for garbage collection, as they are no longer accessible.
Line 7: This line simply prints to the console and does not affect the eligibility of the objects for garbage collection.
Final Answer:
The B object becomes eligible for garbage collection after line 6.
(1)
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.
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...
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.
Mangesh Ashok Shingate said:
1 decade ago
We do not know about either object which assigned to object reference 'b' is assigned to another object reference of the class or not, if object is assigned to some other reference then answer is D, if object is not assigned to any other object reference then answer is A.
So until understanding execution of method s (i.e: a.s(b)), There is no way to be absolutely certain to say when object assigned to b is eligible for garbage collection.
So answer D is correct.
So until understanding execution of method s (i.e: a.s(b)), There is no way to be absolutely certain to say when object assigned to b is eligible for garbage collection.
So answer D is correct.
Aaron said:
1 decade ago
The answer is D because the question is asking when the OBJECT created for B is eligible for garbage collection. When a.s(b) is invoked, a copy of the reference 'b' is made and passed into a.s(...) (all Java is pass by value!).
This means that there is now a reference to the original OBJECT floating about somewhere, and that object could now be referenced in a list or map (or something) somewhere well after the method that constructed it is complete.
This means that there is now a reference to the original OBJECT floating about somewhere, and that object could now be referenced in a list or map (or something) somewhere well after the method that constructed it is complete.
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?
Anish Kumar said:
9 years ago
I think no any option is a right answer. Because when we are calling s method then there is a possibility of reassignment of the object in method but it is local, it will be executed by execution engine of java after completion of (s) method all object will be eligible for GC. So just before line number 5, all b object will be eligible for GC. Please, anyone explain to me if I am wrong.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers