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.
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)
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)
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)
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)
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)
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)
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.
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.
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.
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