Export (0) Print
Expand All

GC.Collect Method (Int32, GCCollectionMode, Boolean)

.NET Framework 4.5

Forces a garbage collection from generation 0 through a specified generation, at a time specified by a GCCollectionMode value, with a value specifying whether the collection should be blocking.

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

static member Collect : 
        generation:int * 
        mode:GCCollectionMode * 
        blocking:bool -> unit

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.

ExceptionCondition
ArgumentOutOfRangeException

generation is not valid.

-or-

mode is not one of the GCCollectionMode values.

The following table summarizes the interaction of the mode and blocking parameters:

mode

blocking is true

blocking is false

Forced or Default

A blocking collection is performed as soon as possible. If a background collection is in progress and generation is 0 or 1, the Collect(Int32, GCCollectionMode, Boolean) method immediately triggers a blocking collection and returns when the collection is finished. If a background collection is in progress and generation is 2, the method waits until the background collection is finished, triggers a blocking generation 2 collection, and then returns.

A collection is performed as soon as possible. The Collect(Int32, GCCollectionMode, Boolean) method requests a background collection, but this is not guaranteed; depending on the circumstances, a blocking collection may still be performed. If a background collection is already in progress, the method returns immediately.

Optimized

A blocking collection may be performed, depending on the state of the garbage collector and the generation parameter. The garbage collector tries to provide optimal performance.

A collection may be performed, depending on the state of the garbage collector. The Collect(Int32, GCCollectionMode, Boolean) method requests a background collection, but this is not guaranteed; depending on the circumstances, a blocking collection may still be performed. The garbage collector tries to provide optimal performance. If a background collection is already in progress, the method returns immediately.

If a call to the Collect(Int32, GCCollectionMode, Boolean) method performs a full blocking garbage collection, you can also compact the large object heap by setting the GCSettings.LargeObjectHeapCompactionMode property to GCLargeObjectHeapCompactionMode.CompactOnce before calling the Collect method.

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft