Developer knowledge network · moderated exchange

Zajednica UnreliableCode

Zajednica za istraživanje, obrnuti inženjering i programiranje programera

Knowledge indexŽivjeti
4Categories
919Threads
2.8KPostovi
Discussion

Understanding .NET Garbage Collection Generations (Gen 0, Gen 1, Gen 2) and the Large Object Heap (LOH) [StackOverflow Architecture Guide]

gc_internals_guy
.NET GC Specialist
MEMBER
Rep: 101
Datum pridruživanja: Nov 2020
Postovi: 1
Hvala: 48
prije 1 mjeseci · Jul 6, 2026 11:58 AM
#1

How the .NET generational Garbage Collector optimizes memory management:

  • Gen 0: Short-lived objects (temporary variables, loop buffers). Collected frequently in sub-millisecond pauses.
  • Gen 1: Buffer zone for objects that survived Gen 0.
  • Gen 2: Long-lived objects (static singletons, cache dictionaries). Full GC collections are expensive.
  • Large Object Heap (LOH): Objects $\ge 85,000$ bytes are allocated directly on LOH to avoid copying costs during compaction, but can cause address space fragmentation.
  • Pinned Object Heap (POH): Objects pinned for native interop to avoid GC compaction barriers.
dotnet_runtime_architect
.NET Core Specialist
MEMBER
Rep: 103
Datum pridruživanja: Apr 2018
Postovi: 40
Hvala: 24
prije 1 mjeseci · Jul 6, 2026 3:03 PM
#2

Allocating large buffers repeatedly on the LOH is the leading cause of OutOfMemoryException crashes in 32/64-bit .NET services.

profiler_pat
Performance Hunter
MEMBER
Rep: 146
Datum pridruživanja: Aug 2019
Postovi: 33
Hvala: 31
prije 1 mjeseci · Jul 7, 2026 3:15 AM
#3

Always use ArrayPool<byte>.Shared.Rent() for buffers $>85\text{KB}$ to keep them off the LOH free lists!