GC::MaxGeneration Property

 

Gets the maximum number of generations that the system currently supports.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public:
property int MaxGeneration {
	static int get();
}

Property Value

Type: System::Int32

A value that ranges from zero to the maximum number of supported generations.

The generation number, or age, of an object is an implementation-defined relative measure of an object's lifespan. The most recently created objects are in generation 0 and the oldest objects are in a generation less than or equal to the generation returned by the MaxGeneration property.

The garbage collector assumes that newer memory is more likely to be eligible for garbage collection than older memory. Therefore, the garbage collector improves its performance by adjusting generation numbers each time it reclaims memory, and the MaxGeneration property value can grow over time.

If object aging is implemented, the MaxGeneration property returns the maximum generation number used by the system; otherwise, this property returns zero.

Notes to Implementers:

For this implementation, the value returned by the MaxGeneration property is guaranteed to remain constant for the lifetime of an executing application.

Use the MaxGeneration property to determine the maximum value you can specify when calling the Collect method that takes a generation parameter.

The following example demonstrates how to use the MaxGeneration property to display the largest generation number currently in use.

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 ) );
}

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: