Handling large objects and arrays
This topic requires careful consideration due to the impact that collections of large objects can have on application performance and memory usage. Large objects are typically defined as 85,000 bytes or larger. To improve the efficiency of garbage collection, these objects are allocated in a particular area of the heap known as the Large Object Heap (LOH).
Objects on the LOH are collected during a Gen 2 collection. Because Gen 2 collections are less frequent, large objects can remain in memory longer, increasing memory usage. Since the LOH is not compacted, the heap can become fragmented over time, potentially leading to out-of-memory exceptions or increased memory usage because of the inability to utilize free spaces efficiently.
The garbage collector doesn’t compact the LOH because moving large objects around it is costly. Instead, it removes the memory occupied by large objects when they are no longer in use. As a result, over time...