Garbage Collection (GC) in the .NET CLR (Common Language Runtime) is an
automatic memory management system that reclaims memory occupied by
objects no longer in use, freeing developers from manually releasing memory (like in C/C++).
Key Concepts of .NET Garbage Collection
1. Managed Heap
All reference types are allocated on the managed heap.
The CLR tracks object references and cleans up memory when objects are no longer accessible.
2. Generational GC
The heap is divided into three generations:
Generation
Description
Purpose
Gen 0
Short-lived objects
Frequently collected
Gen 1
Buffer between Gen 0 and Gen 2
Less frequently collected
Gen 2
Long-lived objects (e.g., static data, caches)
Least frequently collected
Most new objects start in Gen 0. If they survive collections, they move up (get
promoted) to higher generations.
3. Collection Process
When memory runs low or thresholds are hit:
GC checks which objects are still reachable from:
Stack
Static fields
CPU registers
Unreachable objects are considered garbage.
GC:
Calls finalizers (if any).
Frees memory.
Compacts the heap to reduce fragmentation (except for large object heap in older .NET versions).
4. Large Object Heap (LOH)
Objects > 85 KB are stored in LOH.
Collected only during Gen 2 collections.
Was not compacted before .NET 4.5+ (now optional).
5. GC Modes
Mode
Description
Use Case
Workstation GC
Optimized for low-latency desktop apps
UI apps
Server GC
Optimized for throughput and scalability
ASP.NET, backend services
Background GC
Collects Gen 2 concurrently with app execution
Reduces pause times
6. Finalization and IDisposable
GC may call an object’s finalizer before reclaiming its memory.
Finalizers delay collection (object survives an extra cycle).
IDisposable and using are preferred for deterministic cleanup of unmanaged resources.
Performance Optimizations by GC
Object promotion: Older objects are collected less often.
Compaction: Reduces fragmentation by moving objects.
GC pressure: Monitored to tune allocations (e.g., GC.GetTotalMemory()).
Summary
Feature
Description
Automatic
No manual free() needed
Generational
Groups objects by lifetime
Compacting
Reorganizes memory to prevent fragmentation
Safe
Avoids dangling pointers and memory leaks (in managed code)
Modes
Workstation, Server, Background
Garbage collection in .NET CLR is safe, efficient, and optimized for a wide range of applications — from desktop to web servers — letting developers focus on logic rather than memory management.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Garbage Collection (GC) in the .NET CLR (Common Language Runtime) is an automatic memory management system that reclaims memory occupied by objects no longer in use, freeing developers from manually releasing memory (like in C/C++).
Key Concepts of .NET Garbage Collection
1. Managed Heap
2. Generational GC
The heap is divided into three generations:
3. Collection Process
When memory runs low or thresholds are hit:
4. Large Object Heap (LOH)
5. GC Modes
6. Finalization and IDisposable
IDisposableandusingare preferred for deterministic cleanup of unmanaged resources.Performance Optimizations by GC
GC.GetTotalMemory()).Summary
free()neededGarbage collection in .NET CLR is safe, efficient, and optimized for a wide range of applications — from desktop to web servers — letting developers focus on logic rather than memory management.