Sobes.tech
Middle+

What heaps do you know besides the main one (SOH)? Tell me about the Large Object Heap and Pinned Object Heap.

sobes.tech AI

Answer from AI

In .NET, besides the main heap (Small Object Heap, SOH), there are:

  • Large Object Heap (LOH) — used for objects larger than 85,000 bytes. Such objects are allocated separately to avoid fragmentation of the SOH. LOH is not compacted by default, which can lead to memory fragmentation.

  • Pinned Object Heap (POH) — introduced in newer versions of .NET for storing objects that cannot be moved by the garbage collector (e.g., when working with unsafe code or interop). POH helps reduce fragmentation and improve performance when working with pinned objects.

Example:

byte[] largeArray = new byte[100_000]; // goes to LOH
fixed (byte* p = largeArray) {
    // pinned pointer, object is pinned in POH
}