GC.Collect Method (Int32, GCCollectionMode, Boolean)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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.
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Shared Sub Collect ( _ generation As Integer, _ mode As GCCollectionMode, _ blocking As Boolean _ )
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 or 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 |
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. | |
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. |