Share via


GC.MaxGeneration プロパティ

システムが現在サポートしている最大ジェネレーション番号を取得します。

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;

プロパティ値

0 からサポートされるジェネレーションの最大数までの値。

解説

オブジェクトのジェネレーション番号、つまり世代は、実装で定義されているオブジェクトの有効期間の相対値です。最も新しく作成されたオブジェクトはジェネレーション 0 に属し、最も古いオブジェクトは、 MaxGeneration プロパティが返すジェネレーション番号以下のジェネレーションに属します。

ガベージ コレクタは、古いメモリより新しいメモリの方を優先的にガベージ コレクションの対象として認識します。したがって、ガベージ コレクタは、メモリの収集ごとにジェネレーション番号を調整してパフォーマンスを向上させるため、 MaxGeneration プロパティの値は時間と共に増大することがあります。

オブジェクトの世代が実装されている場合、 MaxGeneration プロパティは、システムが使用する最大ジェネレーション番号を返します。それ以外の場合、このプロパティは 0 を返します。

実装時の注意: この実装の場合、 MaxGeneration プロパティが返す値は、実行中のアプリケーションの有効期間中は一定であることが保証されています。

MaxGeneration プロパティを使用して、ジェネレーション番号をパラメータとしてとる Collect メソッドを呼び出すときに指定できる最大値を確認できます。

使用例

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

GC クラス | GC メンバ | System 名前空間 | GetGeneration