Garbage Collection ( Part II )
Garbage Collection process:
The garbage collector's job is
1)To identify objects that are no longer in use (An object is in use if it can be accessed or reached by the program in its current state)
2) Reclaim the memory.
How to identify the unreachable objects?
An executing Java program consists of a set of threads, each of which is actively executing a set of methods (one having called the next). Each of these methods can have arguments or local variables that are references to objects. These references are said to belong to a root set of references that are immediately accessible to the program. Other references in the root set include static reference variables defined in loaded classes, and references registered through the Java Native Interface (JNI) API.
- All objects referenced by this root set of references are said to be reachable by the program in its current state and must not be collected. Also, those objects might contain references to still other objects, which are also reachable, and so on.
- All other objects on the heap are considered unreachable, and all unreachable objects are eligible for garbage collection.
- If an unreachable object has a finalize () method (means Resurrectable state), arrangements are made for the object's finalizer to be called.
- Unreachable objects with no finalizers and objects whose finalizers have been called are simply reclaimed during garbage collection.
Garbage collection algorithms vary, but they all have in common the task of identifying the objects that are reachable from the root set and reclaiming the space occupied by any other objects.
In the diagram, which is a simplified view of the stack and heap for instructional purposes, objects inside the blue square are reachable from the thread root set, while objects outside the square in red) are not.
4 comments:
It's really informative!! For last few days I was working on memory leak, I know how important it is to have this understanding of garbage collection!!
It's very imporatant to know "root set of references". It has been explained well!!
The second last paragraph starts off with, "Garbage collection algorithms vary". Does Java follow more than one Garbage Collection algorithm, or does it mean that the parallel platforms (eg. .NET) follows a different algo?
JVM implementation varies with different platforms (OS) to leverage platform specific enhanced libraries for better performance and optimization. Also JVM vendors can optimize JVM implementations any way they need to. As far as JVM vendors are concerned Sun and IBM are the leaders.
Following provides a good comparison of leading JVMs.
http://www.cs.princeton.edu/~mjacob/papers/crossarch.pdf
It would be a good idea to have comparison benchmark of JVMs of two parallel technologies java and .Net. After googling, I found following link - not sure how authentic the information is.
http://benpryor.com/blog/index.php?/archives/15-JVM-vs-CLR-memory-allocation.html
Post a Comment