*
The moose likes Beginning Java and the fly likes How  Garbage Collector works? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How  Garbage Collector works?" Watch "How  Garbage Collector works?" New topic
Author

How Garbage Collector works?

Jalli Venkat
Greenhorn

Joined: Aug 10, 2006
Posts: 27
How can Garbage Collector identifies Objects which are not used by any other program? What is the internal machanism used by the Garbage Collector?
Is there any particular Condition for Garbage Collection?


venkat
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Originally posted by Jalli Venkat:
How can Garbage Collector identifies Objects which are not used by any other program? What is the internal machanism used by the Garbage Collector?
Is there any particular Condition for Garbage Collection?


There is no magic here. The GC must start from the roots (static variables, the stack for every thread, etc) and follow those references to instances. And then follow the variables in those instances to more instances. Until it has found everything that is reachable.

This is further complicated with weak references, and finalization. The condition for GC is that an instance is not reachable -- directly or indirectly -- from one of the roots.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: How Garbage Collector works?