• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Eligible for Garbage Collection

 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

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.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to think the same. b Should be "eligible" for GC soon after b = null;

Anybody want to dispute?
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by LS chin:
Hi everyone,

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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How on earth can other objects get at those references?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if B looked like this
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
object-b is available for GarbageCollection after the line b=null; what is uncertain is you cant say when object-b is garbage collected.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks for all your replies.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
What if B looked like this


Wouldn't this create an island eligible for collection?

Edit: never mind, I looked over the static part.
[ September 16, 2008: Message edited by: Ronald Schild ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic