// Only investigate generation 0 objects.
GC.Collect(0);
GC.WaitForPendingFinalizers();
...
}
As well, as of .NET 3.5, the Collect() method can also be passed in a value of the
GCCollectionMode enumeration as a second parameter, to fine-tune exactly how the runtime
should force the garbage collection. This enum defines the following values:
public enum GCCollectionMode
{
Default, // Forced is the current default.
Forced, // Tells the runtime to collect immediately!
Optimized // Allows the runtime to determine
// whether the current time is optimal to reclaim objects.
}
Like any garbage collection, calling GC.Collect() will promote surviving generations. To illus-
trate, assume that our Main() method has been updated as follows:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with System.GC *****");
Do'stlaringiz bilan baham: |