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 5 of 6.
Mostapha said:
9 years ago
Option D.
It's depends of what a.s (b) will do if it exists.
It's depends of what a.s (b) will do if it exists.
Pankaj said:
9 years ago
Option D is Correct.
Because Object A hold the Object B, and when B is null still Object A has a reference.
So there is no chance that Object B garbage Collected at any point.
But after execution of line, no 6 both Object is eligible for GarbageCollection.
Because Object A hold the Object B, and when B is null still Object A has a reference.
So there is no chance that Object B garbage Collected at any point.
But after execution of line, no 6 both Object is eligible for GarbageCollection.
Xinu said:
9 years ago
If the 'b' is assigned, by some different reference, in side the method s(b), then, 'b' will not come across 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.
Nikhil Nambiar said:
9 years ago
Option D is right because there is no assurance of garbage collector activity. And we cannot even predict the implicit call to finalize method.
Mukesh said:
9 years ago
Option D is correct Because JVM runs garbage collection without knowing of the user. So, even if we run the finally we just remove some resources, not the complete object is removed.
Yuvaraju said:
9 years ago
We can assign a null to the object, so option D is correct.
Mayank said:
9 years ago
We are passing value null for reference of Object not to object, so Object will be available for garbage after line 5, after line 5 no one access Object of B class OR we can say we will not be able to access instance member of B class we can only access Reference of B Class.
So, my answer is A.
So, my answer is A.
TheExodu5 said:
8 years ago
@Mayank.
The reference was passed to A.s. That reference may still exist, and the original object can still be accessed.
The answer is D.
The reference was passed to A.s. That reference may still exist, and the original object can still be accessed.
The answer is D.
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