• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

when the object is eligible for garbage collection

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 String s = "fasd";
2 s = null;
3 String b ="afdas";
Which is the earlist line where string s is eligible for being garbage collected?
Some books say line 2 where others say line 3. Which one is right?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say after line 2. That is when the object is not referenced anymore. Line three constructs a different string object.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects are marked for garbage as soon as they are inaccessable. Which translates that if Object o = someObject is nulled i.e. o = null; someObject will be marked for garbage if it is not referenced by any other reference(s).
1. null a reference
2. reference goes out of scope
3. reference is assigned to another object's instance
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well technically it will be marked after the execution of line 2. Not at line 2. So the wording of the question has to be accurate. Otherwise in this case it will be line 3.....
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s will never be eligible to the gc. Only objects created
with keyword 'new' will be eligible to gc if their ref is set to
null or there's no ref with them.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic