What are the Generations in Garbage Collection?
Solution 1:
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.
Solution 2:
There's a nice description in "Pro C# 2008":
- Generation 0 identifies a newly created object that has never been marked for collection
- Generation 1 identifies an object that has survived a GC (marked for collection but not removed because there was sufficient heap space)
- Generation 2 identifies an object that has survived more than one sweep of the GC.
Solution 3:
My blog, Generations of Garbage Collection, answers your question:
The CLR’s Garbage collector (GC) is a generational garbage collector, also known as ephermal garbage collector.
It has three generations:
Generation 0 :
It contains all newly constructed object which are never examined by GC.
Generation 1:
The CLR, when initializes, selects a budget size in kb for generation 0 . If the creation of an object causes the generation 0 to surpass its budget, garbage collection is started. The objects which are not collected in Generation 0 are moved to Generation 1 and Generation 0 is emptied. Let’s say the budget of Generation 0 is equal to size of 5 objects. So generation 0 would look like below before creation of object 6:
After creation of object 6, garbage allocation gets started which deallocates the garbage objects 1, 3 and 5 and moves 2 and 4 adjacent to each other in Generation 1.
The budget size of generation 1 is also selected by CLR upon initialization. Creation of object 11 causes the GC to start again which may move some more objects to generation 1.
Generation 1 is ignored for Garbage Collection until it reaches its budget size for Garbage collection, which improves the performance of GC.
Generation 2:
Over the several generation 0 collection, generation 1 may surpass it’s budget limit which cause GC to collect the Garbage from both generations. In this case, generation 1 survivors are promoted to generation 2, generation 0 survivors are promoted to generation 1, and generation 0 is empty.
Let’s say allocation object 21 cause Garbage collection and generation 1 budget have been reached.
So heap would look like below with the object that survived in Generation 1 promoted to generation 2.
So basically Generation GC assumes that newer objects have more probability to collected.
We know that CLR selects budgets for all three generations but it can modify them as GC is a self-tuning collector. If GC sees that there are very few surviving objects after collecting generation 0, it might decide to reduce the budget of generation 0, so that lesser work is done. On the other hand, if GC collects generation 0 and sees that there are a lot of surviving objects, not a lot of memory was reclaimed in the garbage collection. In this case, the garbage collector will grow generation 0’s budget. The GC also modifies the budgets of generation 1 and generation 2 accordingly.