开发者

What are the Generations in Garbage Collection?

I don't understand what "generations" are in the context of Gar开发者_如何学Cbage Collection. Can someone explain in simple terms?


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.


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.


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:

What are the Generations in Garbage Collection?

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.

What are the Generations in Garbage Collection?

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.

What are the Generations in Garbage Collection?

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.

What are the Generations in Garbage Collection?

So heap would look like below with the object that survived in Generation 1 promoted to generation 2.

What are the Generations in Garbage Collection?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜