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 6 of 6.
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)
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] ).
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".
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.
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)
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)
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.
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)
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers