Developer knowledge network ยท moderated exchange

Komunitas Kode Tidak Dapat Diandalkan

Riset Pengembang, Rekayasa Terbalik & Komunitas Pengkodean

Knowledge indexHidup
4Categories
919Threads
2.8KPostingan
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
Reputasi: 101
Tanggal Bergabung: Nov 2020
Postingan: 1
Terima kasih: 48
1 bulan yang lalu ยท 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
Reputasi: 103
Tanggal Bergabung: Apr 2018
Postingan: 40
Terima kasih: 24
1 bulan yang lalu ยท 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
Reputasi: 146
Tanggal Bergabung: Aug 2019
Postingan: 33
Terima kasih: 31
1 bulan yang lalu ยท 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!