Saturday, March 5, 2011

Java garbage collection algorithms

There are two major types namely reference counting and tracing.

 Reference Counting:

Each object has a referenceCount that measures the number of reference objects that are pointing to it. i.e in other words if you create one more reference to that object, that count should increase by one. On garbage collection cycle, objects that have referenceCount as zero, will be removed from heap.

Tracing :

Starting from the root object, trace each object that are having references from the root object. Remove the objects that are not referenced from the root.

Tracing is achived through various techniques like Mark and Sweep, Copying,  Mark and Compact.

Mark and Sweep:

It has two steps. On first step live objects are marked through tracing. On second step all objects are visited and unmarked objects are removed from heap. Marked objects are unmarked for next tracing cycle.
Disadvantage of this approach is it has two steps and it could create holes in the heap. Garbage collection is done after stopping all the threads in the jvm.

Copying Collector:

Herealso all threads are stopped before staring the garbage collection. Heap is divided into active and inactive part. Once the active part is full, threads are stopped and tracing is started. During tracing, live objects are moved to the inactive part. While doing so, fragmentation is automatically done i.e objects are compacted in the other part. Once copying is completed active part is made as inactive part and vice versa. After copying inactive part is garbage collected. Garbage is collected through single pass, but twice the memory is needed as copying is performed from active part to inactive part.

Mark and Compact Collector:

It combines the advantages of both mark & sweep and copying collector. It has two phases as mark and compact phase.  During mark phase, objects are marked. In mark and sweep collection, boolean marked is stored in the object itself. Whereas here, it is stored in the handle. Handle is an object which has reference to the Class.object that it refers to and the pointer to the original object created in the heap. During mark phase, marked boolean is added here and not in the object. So when an object is changed, it is enough to update the handle and not reference objects that are pointing to it.  During compact phase ALL objects in the heap are examined. Objects that are marked are slided to the lower portion of the heap. First I took some time to understand this as I had questions like what will happen to the existing object? Then I realized that memory allocation is contigeous. Root element is starting with zero and then subsequent elements are sequentially stored in the heap.

No comments:

Post a Comment