System.GC.Collect Method

Forces a garbage collection from generation 0 through a specified generation, at a time specified by a GCCollectionMode value.

Syntax

public static void Collect (int generation, GCCollectionMode mode)

Parameters

generation
The number of the oldest generation that garbage collection can be performed on.
mode
One of the enumeration values that specifies the behavior for the garbage collection.

Remarks

Use the mode parameter to specify whether garbage collection should occur immediately or only if the time is optimal to reclaim objects. Using this method does not guarantee that all inaccessible memory in the specified generation is reclaimed.

To adjust the intrusiveness of garbage collection during critical periods in your application, set the System.Runtime.GCSettings.LatencyMode property.

The garbage collector does not collect objects with a generation number higher than specified by the generation parameter. Use the GC.MaxGeneration property to determine the maximum valid value of generation.

To have the garbage collector consider all objects regardless of their generation, use the version of this method that takes no parameters.

To have the garbage collector reclaim objects up to a specified generation of objects, use the GC.Collect(int) method overload. When you specify the maximum generation, all objects are collected.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0