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!