GCSettings.LargeObjectHeapCompactionMode Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

[Supported in the .NET Framework 4.5.1 and later versions]

Gets or sets a value that indicates whether a full blocking garbage collection compacts the large object heap (LOH).

Namespace:   System.Runtime
Assembly:  mscorlib (in mscorlib.dll)

public static GCLargeObjectHeapCompactionMode LargeObjectHeapCompactionMode {
	[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]

Property Value

Type: System.Runtime.GCLargeObjectHeapCompactionMode

One of the enumeration values that indicates whether a full blocking garbage collection compacts the LOH.

The LOH is used for allocating memory for large objects (such as arrays) that require more than 85,000 bytes. Because of the performance impact of copying large blocks of memory, the garbage collector ordinarily sweeps the LOH, which involves creating a list of the memory locations that were occupied by dead objects and that can be reused to satisfy future requests for allocation of memory for large objects. However, in apps that make extensive use of the LOH to store transient objects, memory fragmentation can also adversely impact performance. In this case, it is possible to use the LargeObjectHeapCompactionMode property to compact rather than simply sweep the LOH during a garbage collection.

The default value of the LargeObjectHeapCompactionMode property is GCLargeObjectHeapCompactionMode.Default, which indicates that the LOH is not compacted during garbage collections. If you assign the property a value of GCLargeObjectHeapCompactionMode.CompactOnce, the LOH is compacted during the next full blocking garbage collection, and the property value is reset to GCLargeObjectHeapCompactionMode.Default.


Background garbage collections are not blocking. This means that, if you set the LargeObjectHeapCompactionMode property to GCLargeObjectHeapCompactionMode.CompactOnce, any background generation 2 collections that occur subsequently do not compact the LOH. Only the first blocking generation 2 collection compacts the LOH.

After the LargeObjectHeapCompactionMode property is set to GCLargeObjectHeapCompactionMode.CompactOnce, the next full blocking garbage collection (and compaction of the LOH) occurs at an indeterminate future time. You can compact the LOH immediately by using code like the following:

GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;

Universal Windows Platform
Available since 8.1
.NET Framework
Available since 4.5.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top