Collect Method (Int32, GCCollectionMode, Boolean)
Collapse the table of content
Expand the table of content

GC.Collect Method (Int32, GCCollectionMode, Boolean)

.NET Framework (current version)
 

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 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.

Exception Condition
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.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft