Developer knowledge network · moderated exchange

UnreliableCode қауымдастығы

Әзірлеушілерді зерттеу, кері инженерия және кодтау қауымдастығы

Knowledge indexТірі
4Categories
919Threads
2.8KЖазбалар
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
Өкіл: 101
Қосылу күні: Nov 2020
Хабарламалар: 1
Рахмет: 48
1 ай бұрын · 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
Өкіл: 103
Қосылу күні: Apr 2018
Хабарламалар: 40
Рахмет: 24
1 ай бұрын · 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
Өкіл: 146
Қосылу күні: Aug 2019
Хабарламалар: 33
Рахмет: 31
1 ай бұрын · 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!