GC::Collect Method (Int32)
Forces an immediate garbage collection from generation zero through a specified generation.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- generation
- Type: System::Int32
The number of the oldest generation that garbage collection can be performed on.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | generation is not valid. |
Use this method to try to reclaim memory that is inaccessible. However, using this method does not guarantee that all inaccessible memory in the specified generation is reclaimed.
If object aging is implemented, the garbage collector does not collect objects with a generation number that is higher than the specified generation. If object aging is not implemented, the garbage collector considers all objects during the garbage collection.
Use the MaxGeneration property to determine the maximum valid value of the generation parameter.
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 based on a GCCollectionMode setting, use the GC::Collect(Int32, GCCollectionMode) method overload.
The following example demonstrates how to use the Collect method to perform a collection on individual layers of memory. The code generates a number of unused objects, and then calls the Collect method to clean them from memory.
using namespace System; const long maxGarbage = 1000; ref class MyGCCollectClass { public: void MakeSomeGarbage() { Version^ vt; for ( int i = 0; i < maxGarbage; i++ ) { // Create objects and release them to fill up memory // with unused objects. vt = gcnew Version; } } }; int main() { MyGCCollectClass^ myGCCol = gcnew MyGCCollectClass; // Determine the maximum number of generations the system // garbage collector currently supports. Console::WriteLine( "The highest generation is {0}", GC::MaxGeneration ); myGCCol->MakeSomeGarbage(); // Determine which generation myGCCol object is stored in. Console::WriteLine( "Generation: {0}", GC::GetGeneration( myGCCol ) ); // Determine the best available approximation of the number // of bytes currently allocated in managed memory. Console::WriteLine( "Total Memory: {0}", GC::GetTotalMemory( false ) ); // Perform a collection of generation 0 only. GC::Collect( 0 ); // Determine which generation myGCCol object is stored in. Console::WriteLine( "Generation: {0}", GC::GetGeneration( myGCCol ) ); Console::WriteLine( "Total Memory: {0}", GC::GetTotalMemory( false ) ); // Perform a collection of all generations up to and including 2. GC::Collect( 2 ); // Determine which generation myGCCol object is stored in. Console::WriteLine( "Generation: {0}", GC::GetGeneration( myGCCol ) ); Console::WriteLine( "Total Memory: {0}", GC::GetTotalMemory( false ) ); }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.