Friday, November 13, 2009

The Garbage Collector Approaches

A Garbage Collector must do two things:-

1.Detect garbage objects
2.Deallocate the memory of garbage objects and make it available for the program.

There are four approaches that a garbage collector may adopt to detect the garbage objects.

A) Reference -counting Collectors :-Reference counting garbage collectors keep a count of the references for the each live object.When an object is created ,the reference count of each object is set to one.When you reference the object ,the reference count is incremented by one.Similarly when a reference to an object is eliminated ,the reference count is decremented by one.An object which has reference count zero is a garbage object when the object is garbage collected ,the references of the object that it refers to are decremented.Therefore garbage collection of the one may lead to the creation of other garbage objects.This method can be executed in small parts with the program ,and the program need not to be interrupted for a long time .However ,there is an overhead of incrementing and decrementing the counter everytime something happens on the references side.

B) Tracing Collectors :-In this technique ,a set of root is defined from where objects are traced .An object is reachable if there are objects that referenced and cannot ,therefore be accessed in the program.Objects that are reachable are marked .At the end of the trace ,all marked objects can be garbage collected.
This is also known as the mark and sweep algorithm .The mark phase marks all the referenced objects .The sweep phase garbage collects the memory of unreachable and unreferenced objects.

C) Compacting Collectors :-These collectors reduce the degree of memory fragmentation by moving the all unused and free space on one side during garbage collection.The free memory is then available as one huge chunk.All references need to be shifted ,objects are the updated to refer to the new memory locations.

D)Adaptive Collectors :-This algorithm makes the use of the fact that different garbage collectors algorithm works better in different situations .The adaptive algorithm monitors the situation and uses the garbage technique that best suits the situation .It may switch from one technique to the other according to the need.

Suggested Reading:-
Java Sandbox
Java Garbage Collectors
Java Architectutre Tradeoffs
Java class loader architecture

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP