Labels

Tuesday, April 3, 2007

How Garbage Collector works.

Below is the explanation of GC Algrothim in breif:
  1. GC stores new objects in generation 0 until there is no more space.
  2. The GC performs a collection when generation 0 is full.
  3. This collection of generation 0 compacts the memory for the reachable objects.
  4. The garbage collector then promotes these objects from generation 0 to 1 and it considers the remainder of the managed heap generation 0.
  5. GC considers this new portion of the managed heap generation 1.
  6. Again It continues to allocate memory for new objects in generation 0 until generation 0 is full and it is necessary to perform another collection.
  7. Now in later stage of allocations, For example, if a collection of generation 0 does not reclaim enough memory for the application to successfully complete its attempt to create a new object, the GC can perform a collection of generation 1, then generation 0.
  8. If this does not reclaim enough memory, the GC can perform a collection of generations 2, 1, and 0.
  9. Because the garbage collector supports only three generations, objects in generation 2 that survives remain in generation 2 only, until they are determined to be unreachable in a future collection. i.e When the collection of generation 2 happens, the reachable objects in generation 2 remains there only, and not shifted to any other generations. And the Unreachable object are thrown away from the heap.

Note:

  • Because objects that survive collections tend to have longer lifetimes, it makes sense to promote them to a higher generation. As a result, the garbage collector does not have to reexamine the objects in generations 1 and 2 each time it performs a collection of generation 0.
  • This is the most efficient approach, because new objects tend to have short lifetimes, and it is expected that many of the objects in generation 0 will no longer be in use by the application when a collection is performed.


Hope it is clear now.

Regards,
Arun .....


No comments:

Post a Comment