GC.MaxGeneration GC.MaxGeneration GC.MaxGeneration GC.MaxGeneration Property

Definition

Ruft die maximale Anzahl von Generationen ab, die das System gegenwärtig unterstützt.Gets the maximum number of generations that the system currently supports.

public:
 static property int MaxGeneration { int get(); };
public static int MaxGeneration { get; }
member this.MaxGeneration : int
Public Shared ReadOnly Property MaxGeneration As Integer

Eigenschaftswert

Ein Wert zwischen 0 (null) und der Höchstanzahl der unterstützten Generationen.A value that ranges from zero to the maximum number of supported generations.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie mit der maxgene-Eigenschaft die größte Generierungs Nummer angezeigt wird, die zurzeit verwendet wird.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 ) );
}

using System;

namespace GCCollectIntExample
{
    class MyGCCollectClass
    {
        private const long maxGarbage = 1000;
      
        static void Main()
        {
            MyGCCollectClass myGCCol = new 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));
            Console.Read();
        }

        void MakeSomeGarbage()
        {
            Version vt;

            for(int i = 0; i < maxGarbage; i++)
            {
                // Create objects and release them to fill up memory
        // with unused objects.
                vt = new Version();
            }
        }
    }
}
Namespace GCCollectInt_Example
    Class MyGCCollectClass
        Private maxGarbage As Long = 10000

        Public Shared Sub Main()
            Dim myGCCol As New 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))
            Console.Read()

        End Sub


        Sub MakeSomeGarbage()
            Dim vt As Version

            Dim i As Integer
            For i = 0 To maxGarbage - 1
                'Create objects and release them to fill up memory
                'with unused objects.
                vt = New Version
            Next i
        End Sub
    End Class
End Namespace

Hinweise

Die Generierungs Nummer (oder das Alter) eines Objekts ist ein durch die Implementierung definiertes relativer Measure der Lebensdauer eines Objekts.The generation number, or age, of an object is an implementation-defined relative measure of an object's lifespan. Die zuletzt erstellten Objekte befinden sich in der Generation 0 und die ältesten Objekte in einer Generation, die kleiner oder gleich der Generierung ist, die von der MaxGeneration-Eigenschaft zurückgegeben wird.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.

Der Garbage Collector geht davon aus, dass der neuere Arbeitsspeicher eher für Garbage Collection als älterer Arbeitsspeicher geeignet ist.The garbage collector assumes that newer memory is more likely to be eligible for garbage collection than older memory. Dadurch wird die Leistung des Garbage Collector verbessert, indem bei jeder Freigabe von Speicher die Generations Zahlen angepasst werden, und der Wert der MaxGeneration-Eigenschaft kann im Laufe der Zeit zunehmen.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.

Wenn die Objekt Alterung implementiert ist, gibt die MaxGeneration-Eigenschaft die maximale Generierungs Nummer zurück, die vom System verwendet wird. Andernfalls gibt diese Eigenschaft 0 (null) zurück.If object aging is implemented, the MaxGeneration property returns the maximum generation number used by the system; otherwise, this property returns zero.

Hinweise für Vererber

Bei dieser Implementierung bleibt der von der MaxGeneration-Eigenschaft zurückgegebene Wert für die Lebensdauer einer ausgeführten Anwendung garantiert konstant.For this implementation, the value returned by the MaxGeneration property is guaranteed to remain constant for the lifetime of an executing application.

Verwenden Sie die MaxGeneration-Eigenschaft, um den maximalen Wert zu bestimmen, den Sie beim Aufrufen der Collect(Int32)-Methode angeben können, die einen Generierungs Parameter annimmt.Use the MaxGeneration property to determine the maximum value you can specify when calling the Collect(Int32) method that takes a generation parameter.

Gilt für:

Siehe auch