|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
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.
Uses the setting designated as the default garbage collection configuration of the running version of the .NET Framework.
Forces garbage collection to occur immediately. This is equivalent to calling GC.Collect().
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.