aspose file tools*
The moose likes Beginning Java and the fly likes Eligible for Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Eligible for Garbage Collection" Watch "Eligible for Garbage Collection" New topic
Author

Eligible for Garbage Collection

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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

Joined: Aug 05, 2008
Posts: 378
I tend to think the same. b Should be "eligible" for GC soon after b = null;

Anybody want to dispute?
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
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

Joined: Oct 13, 2005
Posts: 40061
    
  28
How on earth can other objects get at those references?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
What if B looked like this


Joanne
sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

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
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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

Joined: Jun 28, 2008
Posts: 99
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

Joined: Jun 09, 2008
Posts: 117
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 ]

Java hobbyist.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Eligible for Garbage Collection