Export (0) Print
Expand All

Induced Collections

In most cases, the garbage collector can determine the best time to perform a collection and you should let it run independently. There are rare situations when a forced collection might improve your application's performance. In these cases, you can induce garbage collection by using the Collect method to force a garbage collection.

Use the Collect method when there is a significant reduction in the amount of memory being used at a defined point in your application's code. For example, if your application uses a complex dialog box that has several controls, calling Collect when the dialog box is closed could improve performance by immediately reclaiming the memory. Be sure that the application is not inducing garbage collection too frequently because that can decrease the performance if the garbage collector is unproductively attempting to reclaim objects. The Optimized mode enables the garbage collector to determine whether to collect or not collect based on whether the collections would be productive.

You can use the GC.Collect method overload that takes a GCCollectionMode value to specify the behavior for a forced collection, as described in the following table.

Member

Description

Default

Uses the setting designated as the default garbage collection configuration of the running version of the .NET Framework.

Forced

Forces garbage collection to occur immediately. This is equivalent to calling GC.Collect().

Optimized

Enables the garbage collector to determine whether the current time is optimal to reclaim objects.

The garbage collector could determine that a collection would not be productive enough to be justified, in which case it will return without reclaiming objects.

Community Additions

ADD
Show:
© 2014 Microsoft