• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

gc

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am still confused with gc. when an object are legal to be gc? I went through lots of example, but I cannot do it correctly. Does anyone can give me some good examples and explain to me? thanks.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When nothing has a reference to them, then they are eligible for the gc. The trick is to recognize what can hold a reference to them.
You must also remember that more than one variable can hold a reference to the SAME object. Nulling one of them out does not free the object for the gc unless they are ALL nulled out or replaced with something else.

------------------
Cindy Glass
Sun Certified Programmer for the Java� 2 Platform
Co-author of Java 2 Certification Passport
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add my two cents...
In fact, it's trickier than that since an object may be referenced by another one but still be eligible for garbage collection. How's that ? In fact, all objects that are not reachable from any part of the program are eligible for garbage collection but those object may reference themselves anyway.
here is an example:

which means o2 is referencing o3 and o1 is referencing o2. When o1 reference is set to null, o2 still reference o3 but their are both unreachable. o2 and o3 are eligible for garbage collection but there is still a reference to o3...
Food for thoughts !
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
We begin by testing your absorbancy by exposing you to this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic