Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Garbage Collections

Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I am having a great deal of problems when I solve questions from garbage collections. In general the questions like, How many objects would be garbage collected. Is there any diagramatic way to solve such questions.

Posts: 17346
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, when you create a reference to a new object. Draw two boxes. One to for the stack, where the reference variable goes, and one for the heap where the Object instance goes. (Strings not included)

Then draw a line from the reference to the instance. This shows that the instance that is on the heap has a reference and is therefore not eligible for garbage collection yet.

If the instance creates new Objects then put that new object into the heap, but have the object atht is in the heap point to the new object in the heap.

So if you create another new object that the old reference variable will now point to, you will erase the previous line. When an object on the heap does not have a reference pointing to it from the stack, it is very possible that that is now eligible for garbage collection. The only time it won't be is if another object in the heap has a reference to that object, and some other reference from the stack points to that other object.

Islands are objects all on the heap that point to each other. As long as there is one reference, in the stack, that points to one of those objects in the heap, then all those heap objects that point to each other are still referenced and not eligible for garbage collection.

I hope that helps.

No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic