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?
after line 5
after line 6
after line 7
There is no way to be absolutely certain.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
59 comments Page 2 of 6.

TheExodu5 said:   9 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.

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.

Yuvaraju said:   9 years ago
We can assign a null to the object, so option D is correct.

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.

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.

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.

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.

Pankaj said:   10 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.

Mostapha said:   10 years ago
Option D.

It's depends of what a.s (b) will do if it exists.

Erik said:   10 years ago
Option D is correct.

Assume a.s (b) adds a reference to the object that b references to a collection of some sort, then that object does not become eligible after Line 5.


Post your comments here:

Your comments will be displayed after verification.