Generations
The .NET garbage collector (GC) algorithm is based on several considerations:- It's faster to compact the memory for a portion of the managed heap than for the entire managed heap.
- Newer objects have shorter lifetimes and older objects have longer lifetimes.
- Newer objects tend to be related to each other and accessed by the application around the same time.
- Generation 0. This is the youngest generation
and contains short-lived objects. Newly allocated objects form a new generation of objects and are
implicitly generation 0 collections. However, if they are large objects,
they go on the large object heap in a generation 2 collection. Most objects are reclaimed for garbage collection in generation 0 and don't survive to the next generation. If an application attempts to create a new object when generation 0
is full, the garbage collector performs a collection in an attempt to
free address space for the object. The garbage collector starts by
examining the objects in generation 0 rather than all objects in the
managed heap. A collection of generation 0 alone often reclaims enough
memory to enable the application to continue creating new objects.
- Generation 1. This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
After the garbage collector performs a collection of generation 0, it compacts the memory for the reachable objects and promotes them to generation 1. If a collection of generation 0 does not reclaim enough memory for the application to create a new object, the garbage collector can perform a collection of generation 1, then generation 2. Objects in generation 1 that survive collections are promoted to generation 2. - Generation 2. This generation contains
long-lived objects. Objects in generation 2 that survive a collection remain in
generation 2 until they are determined to be unreachable in a future
collection.
The CLR continually balances two priorities: not letting an application's working set get too large by delaying garbage collection and not letting the garbage collection run too frequently.
source: Fundamentals of garbage collection
No comments:
Post a Comment