MSDN Documentation

.NET Development Concepts

Understanding Garbage Collection in .NET

Garbage collection (GC) is an automatic memory management feature in the .NET Common Language Runtime (CLR). It reclaims memory occupied by objects that are no longer in use by the application, preventing memory leaks and simplifying development.

How Garbage Collection Works

The GC operates in cycles. When the CLR detects that the managed heap is running low on memory, it initiates a garbage collection process. This process involves the following steps:

  1. Marking: The GC starts from the "root set" (which includes static objects, thread stacks, and objects referenced by the CPU registers) and traverses the object graph. It marks all objects that are reachable from the root set.
  2. Sweeping: After marking, the GC iterates through the managed heap. Any object that was not marked is considered unreachable and its memory is reclaimed.
  3. Compacting (Optional): To combat fragmentation, the GC can move the surviving (marked) objects closer together. This creates larger contiguous blocks of free memory, improving allocation performance.

Generations

To optimize the collection process, .NET uses a generational garbage collector. This approach is based on the observation that most objects have a short lifespan. The managed heap is divided into three generations:

When a collection occurs, the GC focuses on the lowest-numbered generation that contains objects. If Generation 0 is collected and a significant amount of memory is reclaimed, the GC might stop. Otherwise, it will proceed to collect Generation 1, and so on.

Types of Garbage Collectors

.NET offers different GC modes to suit various application needs:

Configuration and Best Practices

While the GC is largely automatic, understanding its behavior can help you write more efficient code.

Performance Tip: The System.GC.Collect() method can be used to manually trigger a garbage collection. However, it's generally recommended to let the CLR manage this automatically unless you have a specific performance tuning scenario and a deep understanding of its implications.

GC in .NET Core and later

While the core concepts remain the same, .NET Core and subsequent versions have seen continuous improvements to the GC, including enhanced performance and scalability.