Export (0) Print
Expand All

GC.MaxGeneration Property

Gets the maximum number of generations the system currently supports.

[Visual Basic]
Public Shared ReadOnly Property MaxGeneration As Integer
[C#]
public static int MaxGeneration {get;}
[C++]
public: __property static int get_MaxGeneration();
[JScript]
public static function get MaxGeneration() : int;

Property Value

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

Remarks

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 zero 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.

Example

[Visual Basic] 
Imports System

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 generation 2 only.
            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

[C#] 
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 generation 2 only.
            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();
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

const long  maxGarbage = 1000;

__gc 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 = new Version();
      }
   }
};

int main() {
   MyGCCollectClass* myGCCol = new MyGCCollectClass();

   // Determine the maximum number of generations the system
   // garbage collector currently supports.
   Console::WriteLine(S"The highest generation is {0}", __box(GC::MaxGeneration));

   myGCCol->MakeSomeGarbage();

   // Determine which generation myGCCol object is stored in.
   Console::WriteLine(S"Generation: {0}", __box(GC::GetGeneration(myGCCol)));

   // Determine the best available approximation of the number
   // of bytes currently allocated in managed memory.
   Console::WriteLine(S"Total Memory: {0}", __box(GC::GetTotalMemory(false)));

   // Perform a collection of generation 0 only.
   GC::Collect(0);

   // Determine which generation myGCCol object is stored in.
   Console::WriteLine(S"Generation: {0}", __box(GC::GetGeneration(myGCCol)));

   Console::WriteLine(S"Total Memory: {0}", __box(GC::GetTotalMemory(false)));

   // Perform a collection of generation 2 only.
   GC::Collect(2);

   // Determine which generation myGCCol object is stored in.
   Console::WriteLine(S"Generation: {0}", __box(GC::GetGeneration(myGCCol)));
   Console::WriteLine(S"Total Memory: {0}", __box(GC::GetTotalMemory(false)));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

GC Class | GC Members | System Namespace | GetGeneration

Show:
© 2014 Microsoft