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