.NET Memory Management


In this article, I am giving you a broad idea of how the garbage collector works in Microsoft's implementation of the .NET Framework. You must have a general idea of how .NET manages memory. For additional information, you may refer to MSDN.


Garbage consists of objects created during a program's execution on the managed heap that are no longer accessible by the program.

Garbage Collector (GC)

The Garbage Collector (GC) is the part of the .NET Framework that allocates and releases memory for your .NET applications. The Common Language Runtime (CLR) manages allocation and deallocation of a managed object in memory. C# programmers never do this directly, there is no delete keyword in the C# language. It relies on the garbage collector.

The .NET objects are allocated to a region of memory termed the managed heap. They will be automatically destroyed by the garbage collector. Heap allocation only occurs when you are creating instances of classes. It eliminates the need for the programmer to manually delete objects that are no longer required for program execution. This reuse of memory helps reduce the amount of total memory that a program needs to run. Objects are allocated in the heap continuously, one after another. It is a very fast process, since it is just adding a value to a pointer.

The process of releasing memory is called garbage collection. It releases only objects that are no longer being used in the application. A root is a storage location containing a reference to an object on the managed heap. The runtime will check objects on the managed heap to determine whether they are still reachable (in other words, rooted) by the application. The CLR builds an object graph, that represents each reachable object on the heap. Object graphs are used to document all reachable objects.


Assume the managed heap contains a set of objects named A, B, C, D, E, F and G. During a garbage collection, these objects are examined for active roots. After the graph has been constructed, unreachable objects (that we will assume are objects C and F) are marked as garbage in reddish color in following diagram.

                              manage heep

After the objects have been marked for termination, they are deleted from memory. At this point, the remaining space on the heap is compacted, that in turn causes the CLR to modify the set of active application roots to refer to the correct memory location. Last but not least, the next object pointer is readjusted to point to the next available slot.

                                 next object pointer

Object Generation

For better performance of memory releasing, the managed heap is divided into segments called “Generations”. There are only 3 generations: 0, 1 and 2. This idea of generations is simple: the longer an object has existed on the heap are reachable objects. Objects that have only recently been placed on the heap are unreachable.

  1. When objects are just created, they are placed to the Generation 0 (Gen 0).

  2. When Gen 0 is full, the GC performs a garbage collection. During the collection, the GC removes all unreachable objects from the heap. All reachable objects are promoted to the Generation 1 (Gen 1). The Gen 0 collection is a rather quick operation.

  3. When Gen 1 is full, the Gen 1 garbage collection is performed. All objects that survive the collection are promoted to Gen 2. The Gen 0 collection also takes place here.

  4. When Gen 2 is full, the GC performs full garbage collection. First, Gen 2 collection is performed, then the Gen 1 and Gen 0 collections take place. If there is still not enough memory for new allocations, the GC raises the OutOfMemory exception.

  5. During a full garbage collection, the GC must pass through all objects in the heap, so this process might have a great impact on system resources.

The following diagram is a short explanation of generations work:



I hope you now have a basic understanding of what goes on behind the scenes in your .NET programs. If you have any suggestion regarding this article then please contact me.

Similar Articles