C# & CLR Internals (50 Qs)•Part 3: CLR Internals, Native AOT & Production Diagnostics (25 - 35)
.NET Garbage Collection: Generational GC and the Large Object Heap (LOH).
senior LevelMemory ManagementData Structures
In Simple Words: The .NET CLR uses a generational tracing garbage collector:
- Gen 0: Freshly allocated short-lived objects (collected in sub-milliseconds).
- Gen 1: Buffer generation acting as a ramp between short-lived and long-lived data.
- Gen 2: Long-lived objects (static caches, database connection pools).
- Large Object Heap (LOH): Objects $\ge 85,000\text{ bytes}$ (such as large arrays). LOH objects bypass Gen 0/1 and are allocated directly in Gen 2; historically not compacted, risking memory fragmentation (use
GCSettings.LargeObjectHeapCompactionModeto compact). - Server GC: Spawns one GC thread and dedicated managed heap per logical CPU core for massive multi-threaded web servers.