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.
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)
Chandra Mohan Yadav said:
3 years ago
public class Loan
{
public static void main(String[] args)
{
Loan loan1 = new Loan();
Loan loan2= new Loan();
Loan loan3 = new Loan();
Loan loan4 = new Loan();
loan1=loan3;
loan2=loan4;
loan3 = null
}
}
How many objects are eligible for garbage collection?
Can anyone explain this?
{
public static void main(String[] args)
{
Loan loan1 = new Loan();
Loan loan2= new Loan();
Loan loan3 = new Loan();
Loan loan4 = new Loan();
loan1=loan3;
loan2=loan4;
loan3 = null
}
}
How many objects are eligible for garbage collection?
Can anyone explain this?
(3)
Utkarsh said:
5 years ago
I believe answer should be B.
Clearly we still have a.s referencing B object (new B()) and only after a is set to null the whole chain would break.
Clearly we still have a.s referencing B object (new B()) and only after a is set to null the whole chain would break.
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)
Angelus said:
6 years ago
An object becomes eligible for garbage collection if there is no reference to it or if it has been assigned to null.
So, line 5, assign obj B to null, so I think A is the answer.
So, line 5, assign obj B to null, so I think A is the answer.
(1)
Zafar said:
6 years ago
A is correct , because while passings(b) we just pass the value of whatever in b which will further get collected in method S(B newXyz) so there is no b at all, after line 5, b is referred to null and will be GC'd.
Turgay Ekici said:
8 years ago
(A) Correct,
If the question was in which point that JVM will collect, the answer would be (D).
But the question is "when the object is eligible to be collected".
If the question was in which point that JVM will collect, the answer would be (D).
But the question is "when the object is eligible to be collected".
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] ).
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)
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers