• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Garbage Collection

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have some confusions regarding this question.
class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}

The question is what are the objects eligible for GC once the m1() method finishes executing?
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Anil


I have some confusions regarding this question.


What is your confusion...???
If you want an answer to our question then i suggest you try find it yourself... the question is not very difficult
[ January 03, 2008: Message edited by: abhishek pendkay ]
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garbage collection questions are best answered with pictures. Your question demonstrates islands of isolation as shown below.
Well, i dont know how to attach images so i guess i will have to explain it
http://infernoupload.com/dl.php?file=131c9b933a5410257826
http://infernoupload.com/dl.php?file=131093943e5a1923732e
So after m1() executes all the local reference variables that are stored in m1 stack i1, i2, i3 and i4 are destroyed and now the any live thread which right now is main() has no way to access the four objects and hence these 4 objects are available for garbage collection. These are called islands of isolation, because they have valid references but there is no way for a live thread to access these objects.
Hence the answer is there are four objects that are available for garbage collection.
Thanks
Deepak

[ January 03, 2008: Message edited by: Deepak Jain ]
[ January 03, 2008: Message edited by: Deepak Jain ]
 
Anil Pradhan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton Deepak.
Abhishek - There are no dumb questions in this world(Java).
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,

And the object created by new J()? It is elegible for garbage collection, is not?
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carvalho,
I leave that for you to figure out where object J in main is eligible for garbage collection after main() finishes execution. Please go through the explanation and if you are not able to understand , i would explain it.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Remember that the big rule at JavaRanch is to be nice

Everyone should remember that we use the English language here at the ranch, and that for a LOT of ranchers English is not their first language. Because of that we should all be extra careful with our answers and give everyone the benefit of the doubt!

Thanks,

Bert
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops looks like i broke the rule un-intentionally. I am very sorry for that.
Coming to the answer to question.
The object new J() will be eligible for garbage collection after main() thread finishes because no live thread will be able to access the object.

I am sorry once again, I had no such intentions. My intention was to make Carvalho go through the explanation again and if it was not understandable then i or any rancher would be glad to explain it.

Sorry Carvalho.
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak.
Thank you for your explication. I understand that!
Alexsandra
 
reply
    Bookmark Topic Watch Topic
  • New Topic