• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Eligible for Garbage Collection

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 73981
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How on earth can other objects get at those references?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if B looked like this
 
Ranch Hand
Posts: 237
MyEclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Hot dog! An advertiser loves us THIS much:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic