This is another GC question. It is a very, very basic one. The code looks like this:
Q: When is object B "eligible" for garbage collection?
My answer is: The line after b = null; because there is no 'live' reference to object B cause b has been set to null.
But the answer is: we cannot be certain.
Why? I understand that we cannot be absolutely certain 'when' it is gc'ed but the question is about "eligibility", isn't it?
Any idea or suggestions why? Is this mock question not valid?
Thanks for your replies.
Hi, I am not sure but behavior of GC is not certain means we can't say like this that after this line it will go for garbage collected. Because may be other objects are still using that object reference.
object-b is available for GarbageCollection after the line b=null; what is uncertain is you cant say when object-b is garbage collected.
Sudheer SCWCD, SCJP 5
Joined: Aug 05, 2005
Originally posted by sudheer kiran: object-b is available for GarbageCollection after the line b=null; what is uncertain is you cant say when object-b is garbage collected.
If class B was implemented as I showed above, then object b would NOT be eligible for garbage collection after the line b=null. Therefore, without knowing the implementation of class B, we cannot know when object b will be eligible for garbage collection.
Joined: Jun 28, 2008
I still think that GC should take place after the line b = null. If the code looks like this:
It'd probably be uncertain because the method doStuff(b) may have a live reference to object B.