---
title: "How does garbage collection work in .NET CLR?"  
description: "How does garbage collection work in .NET CLR?"  
author: "ICSM Computer"  
published: 2025-06-16  
updated: 2025-06-19  
canonical: https://www.mindstick.com/forum/161714/how-does-garbage-collection-work-in-dot-net-clr  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# How does garbage collection work in .NET CLR?

How does [garbage collection](https://www.mindstick.com/articles/23/garbage-collection) work in .NET [CLR](https://www.mindstick.com/blog/11/clr-common-language-runtime) with example?

## Replies

### Reply by Anubhav Sharma

Garbage [Collection](https://www.mindstick.com/articles/1718/collections-in-java) (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.
- **Pinned memory**: Avoid moving interop objects (e.g., fixed buffers).
- **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.


---

Original Source: https://www.mindstick.com/forum/161714/how-does-garbage-collection-work-in-dot-net-clr

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
