This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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: 18765
    
  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)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How Garbage Collector works?