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)

public static void Collect(
	int generation,
	GCCollectionMode mode,
	bool blocking
)

Parameters

generation
Type: System.Int32

The number of the oldest generation that garbage collection can be performed on.

mode
Type: System.GCCollectionMode

One of the enumeration values 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.5.2, 4.5.1, 4.5

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft