Sobes.tech
Middle+

Why is the separation of the Large Object Heap and Small Object Heap in .NET necessary?

sobes.tech AI

Answer from AI

Separation of LOH and SOH heaps in .NET optimizes memory management for objects of different sizes.

Small Object Heap (SOH)

  • Stores objects up to 85 KB.
  • Uses a generational garbage collector (Generational Garbage Collection - GC).
  • Recently created objects are placed in generation 0. Surviving objects are moved to generation 1, then to generation 2.
  • Garbage collection occurs more frequently and efficiently for small objects.
// Example of creating an object on SOH
string smallString = "This is a small string"; // Size less than 85 KB
object smallObject = new object(); // Small object

Large Object Heap (LOH)

  • Stores objects of 85 KB and more.
  • Is not generational in the same sense as SOH. Usually cleared only during full garbage collection (generation 2).
  • Memory allocation on LOH can be slower due to searching for a contiguous block of memory.
  • Fragmentation of LOH can lead to problems allocating large objects, even if there is enough free memory overall.
  • Objects on LOH are not moved by GC by default (with .NET Framework 4.5.1, LOH compaction became possible, but it is disabled by default and has its costs). This prevents costly copying of large data volumes.
// Example of creating an object on LOH
byte[] largeArray = new byte[100 * 1024]; // Size 100 KB > 85 KB

Advantages of separation

  1. Garbage collection optimization: Generational GC on SOH is much more efficient for small, short-lived objects. Moving objects on SOH is relatively quick.
  2. Reducing SOH fragmentation: Moving large objects would be extremely costly and would lead to significant fragmentation of SOH. Placing them on a separate LOH heap solves this problem.
  3. Minimizing overhead: Moving large objects to LOH would incur significant overhead. Their non-movement by GC (by default) avoids this.
  4. Improving memory allocation performance: Allocation on SOH often occurs very quickly (using a pointer), while on LOH it may require searching for a suitable free block. Separation allows maintaining high allocation speed for most (small) objects.
Characteristic Small Object Heap (SOH) Large Object Heap (LOH)
Object size Up to 85 KB 85 KB and more
GC model Generational (generations 0, 1, 2) Non-generational (by default)
Object movement Yes (compaction) No (by default)
Cleanup frequency More often Less often (usually with generation 2)
Fragmentation risk Low (compacts) High