tags:

views:

62

answers:

3

I have not understood the generations of Garbage Collection though I found some from google. Can some one of you please tell me in simple terms about that.

Or can reference to a link which will be easy to understand from !

Thanks in advance

A: 

There's a nice description in "Pro C# 2008":

  1. Generation 0 identifies a newly created object that has never been marked for collection
  2. Generation 1 identifies an object that has survived a GC (marked for collection but not removed because there was sufficient heap space)
  3. Generation 2 identifies an object that has survived more than one sweep of the GC.
duffymo
+1  A: 

From Understanding Garbage Collection in .NET

Generations

A generational garbage collector collects the short-lived objects more frequently than the longer lived ones. Short-lived objects are stored in the first generation, generation 0. The longer-lived objects are pushed into the higher generations, 1 or 2. The garbage collector works more frequently in the lower generations than in the higher ones.

When an object is first created, it is put into generation 0. When the generation 0 is filled up, the garbage collector is invoked. The objects that survive the garbage collection in the first generation are promoted onto the next higher generation, generation 1. The objects that survive garbage collection in generation 1 are promoted onto the next and the highest generation, generation 2. This algorithm works efficiently for garbage collection of objects, as it is fast. Note that generation 2 is the highest generation that is supported by the garbage collector.

Garbage Collection in .NET

Generations

While memory allocation on the managed heap is fast, GC itself may take some time. With this in mind several optimisations have been made to improve performance. The GC supports the concept of generations, based on the assumption that the longer an object has been on the heap, the longer it will probably stay there. When an object is allocated on the heap it belongs in generation 0. Each garbage collection that that object survives increases its generation by 1 (currently the highest supported generation is 2). Obviously it's faster to search through, and garbage collect a subset of all objects on the heap, so the GC has the option of collecting only generation 0, 1 or 2 objects (or whatever combination it chooses until it has sufficient memory). Even while collecting only younger objects the GC can also determine if old objects have references to new objects to ensure that it doesn't inadvertently ignore in-use objects.

astander
A: 

Hi All,

I have one query regarding the GC.

Is it possible to get the object generation details? and how?

Regards, Jay

Jay