Garbage collection in the .NET Framework allows automatic memory management. When a class object is created at runtime, heap memory is allocated to it.
Generation 0 of the heap memory contains all the short-lived objects, such as temporary variables and all newly allocated objects. In general, generation 0 has the highest frequency of garbage collection.
This generation contains longer-lived objects that were promoted from generation 0. Objects that make it through generation 1 are promoted to generation 2. Essentially, this generation acts as a buffer between short-lived and long-lived objects.
If some generation 1 objects do not get released in the next garbage collection run, they are moved to generation 2. Objects in generation 2 are long-lived and are similar to static objects because they remain in heap memory throughout the process.
Garbage collection then proceeds through three phases: • A marking phase that creates a list of live objects. • A relocating phase that updates references to the list of live objects. • A compaction phase that releases dead objects and compacts and moves live objects.