🚀 1. Memory Model in CLR
When your .NET app runs, the CLR manages memory in a space called the Managed Heap.
This heap is divided into three main generations plus the Large Object Heap (LOH):
| Generation | Purpose | Typical Object Lifetime |
|---|---|---|
| Gen 0 | For new, short-lived objects | Temporary/local variables |
| Gen 1 | Acts as a buffer | Medium-lived objects |
| Gen 2 | For long-lived objects | Global/singleton objects |
| LOH | For large objects (>85 KB) | Usually Gen 2 behavior |
⚙️ 2. The Core Algorithm – Generational GC Concept
The core principle is:
"Most objects die young."
So .NET uses a generational approach to optimize performance:
Collects Gen 0 frequently
Collects Gen 1 occasionally
Collects Gen 2 rarely
This reduces pause times and CPU cost.
🔬 3. The Step-by-Step Algorithm
Let’s go behind the scenes of what actually happens in CLR.
🧩 Step 1: Object Allocation
Every time you create an object (
newkeyword), it is allocated sequentially in the Gen 0 segment.Allocation is extremely fast — just pointer bumping.
var user = new User(); // Allocated in Gen 0If Gen 0 segment is full → GC starts.
🧹 Step 2: GC Triggered for Gen 0
When Gen 0 is full, the GC runs:
Stop the world: Application threads pause.
Mark phase: GC marks all objects still reachable (rooted via stack, static variables, CPU registers).
Sweep phase: Unreachable objects are removed.
Compact phase: Survivors are moved down in memory to remove fragmentation.
Objects that survive are promoted to the next generation (Gen 1).
📦 Step 3: Object Promotion
After a GC run:
| Current Generation | Next Step |
|---|---|
| Survived Gen 0 | Move to Gen 1 |
| Survived Gen 1 | Move to Gen 2 |
| Survived Gen 2 | Stay in Gen 2 (until full GC) |
👉 Promotion is the “aging process” of an object.
Each time an object survives a collection, its generation count increases by 1 — up to a max of Gen 2.
⚙️ Step 4: Full GC (Gen 2 Collection)
A full GC is triggered when:
Memory pressure is high.
Gen 2 (or LOH) is full.
The app explicitly calls
GC.Collect().
Then:
GC scans all generations (0, 1, 2).
Frees memory for unreachable objects.
May compact the heap (in Compacting GC mode).
This is expensive — can cause noticeable application pause.
🧬 5. Internal Algorithm (Simplified Pseudocode)
Here’s a simplified model of how the CLR GC decides what to collect:
while (applicationRunning)
{
allocateObject();
if (Gen0.isFull)
CollectGeneration(0);
if (Gen1.isFull)
CollectGeneration(1);
if (Gen2.isFull || systemMemoryLow)
CollectGeneration(2);
}
void CollectGeneration(int gen)
{
MarkLiveObjects(gen);
SweepDeadObjects(gen);
CompactHeap(gen);
PromoteSurvivors(gen);
}
Promotion Logic
void PromoteSurvivors(int gen)
{
foreach (var obj in survivors)
{
obj.Generation = Math.Min(obj.Generation + 1, 2);
}
}
🧩 6. Example: Object Lifetime in Action
void Example()
{
// Gen 0 allocation
var temp = new StringBuilder("short-lived");
// Another allocation
var user = new UserProfile(); // long-lived
}

Join the conversation! Your thoughts help the community grow.