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 ...
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

Garbage Collector doubt

 
Ranch Hand
Posts: 47
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

i have little confusing understanding the concept of garbage collector. according to my understanding from k&B, object gets eligible for garbage collection when that object is no more reachable.
we can explicitly make objects eligible for GC either by nulling a reference, reassigning a reference variable or by isolating a reference variable.



could any one please help me understand the concept of GC .

i really appreciate your help guys.
thanks in advance
cheers...
 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think of objects as houses, and reference variables as rolodex cards, or entires in your address book.

Anytime you see "new something", a house is built.

Anytime you see "variableName =", you are writing an address on the card.

so...

GarbageCollection gc1 = new GarbageCollection();
GarbageCollection gc2 = new GarbageCollection();

builds a house, and writes the address of that house on gc1. Then it builds a second house, and writes that address on gc2.

GarbageCollection gc3;

puts a blank address card in your rolodex, and says "this is where I will write down the address for gc3".


gc1=gc2;

This erases the old address on gc1, and replaces it with the address on gc2. Note that nothing about gc2 changes. You now have two variables with the same address. No card holds the address of the first thing you built. You have no way to find it - it is now eligible for garbage collection.


your doStuff() method does indeed return the address of a newly created object. You can call doStuff and ignore what it returns, which means you could create an object that is immediately eligible for gc, but this case, you do save it in your gc3 variable.

 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a 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