GC::Collect Method (Int32, GCCollectionMode, Boolean, Boolean)
Forces a garbage collection from generation 0 through a specified generation, at a time specified by a GCCollectionMode value, with values that specify whether the collection should be blocking and compacting.
Assembly: mscorlib (in mscorlib.dll)
public: static void Collect( int generation, GCCollectionMode mode, bool blocking, bool compacting )
Parameters
- generation
-
Type:
System::Int32
The number of the oldest generation to be garbage collected.
- mode
-
Type:
System::GCCollectionMode
An enumeration value that specifies whether the garbage collection is forced (GCCollectionMode::Default or GCCollectionMode::Forced) or optimized (GCCollectionMode::Optimized).
- blocking
-
Type:
System::Boolean
true to perform a blocking garbage collection; false to perform a background garbage collection where possible. See the Remarks section for more information.
- compacting
-
Type:
System::Boolean
true to compact the small object heap; false to sweep only. See the Remarks section for more information.
If blocking is false, the GC decides whether to perform a background or a blocking garbage collection. If compacting is true, it performs a blocking garbage collection.
If compacting is true, the runtime compacts the small object heap (SOH). The large object heap (LOH) is not compacted unless the GCSettings::LargeObjectHeapCompactionMode property is set to GCLargeObjectHeapCompactionMode::CompactOnce. Note that this includes all blocking garbage collections, not just full blocking garbage collections.
You can call the Collect(Int32, GCCollectionMode, Boolean, Boolean) method to reduce the managed heap to the smallest size possible, as the following code fragment illustrates.
Specifying true for the compacting argument guarantees a compacting, full blocking garbage collection. Setting the GCSettings::LargeObjectHeapCompactionMode property to GCLargeObjectHeapCompactionMode::CompactOnce ensures that both the LOH and SOH are compacted.
Available since 4.6