• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

garbage collection

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what point only a single object is eligible for garbage collection?

class Eco
{
public static void main(String []args)
{
Eco e1=new Eco();
Eco e2=new Eco();
Eco e3=new Eco();
e3.e=e2;
e1.e=e3;
e2=null;
e3=null;
e1=null;
}

Eco e;

}


output is:Never in this program as until last refernce is null none of the objects is eligible and when last refernce is nulled all three are eleigible.

what is meant by last refernce.n xplain the output
 
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That exercise looks familiar; please quote Your Source. And please use the code button to preserve indentation and legibility.

When you get to the end of the method, work out which object the e1 reference points to, then work out what all its fields are, and whether they have any references. Write it on a sheet of paper and that ought to explain your problem.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vini,

IMHO, you will get prompt answers for such questions in SCJP forum

Anyways, let me try and explain it with a diagram:



Hope this helps clear the picture
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic