Saturday, June 8, 2013

Garbage Collection Generations

Garbage Collection involves monitoring the Managed Heap, clearing off objects which are not used and reclaiming the memory used by those objects to make it available for the objects which gets created during the program execution.

Garbage Collection doesn't remove unused objects from the heap randomly, doing so might cause an overhead of re-creating frequently used objects again and again, to overcome this Garbage Collection employs a logic called Generations.

The Managed Heap is organized into a set of Generations, allocation of objects into each of this generation depends on the type of the object and the life time of the object.

The Heap is organized into 3 Generations
Generation 0
Generation 1
Generation 2

All newly created objects, small objects  and short lived objects are allocated in Generation 0. Large objects are placed in Generation 2 Heap.

When Garbage Collection is triggered, it first scans the objects in Generation 0, identifies objects which do not have a reference and removes them from, objects which still have a reference are called Survivors, these objects which survive the collection in Generation 0 are promoted to Generation 1. Garbage Collection runs more frequently in Generation 0 when compared to Generation 1 & 2.
Similar to Generation 0, Garbage Collection runs in Generation 1 to identify objects which do not have a reference, they are removed and the surviving objects are promoted to Generation 2.

Objects which survive a Garbage Collection in Generation 2 are retained in Generation 2, Generation 2 contains objects which are long lived and static object which remain alive till the application process gets terminated.

Search Flipkart Products:
Flipkart.com

No comments: