次の方法で共有


GC.GetGeneration メソッド

オブジェクトの現在のジェネレーション番号を返します。

オーバーロードの一覧

指定したオブジェクトの現在のジェネレーション番号を返します。

[Visual Basic] Overloads Public Shared Function GetGeneration(Object) As Integer

[C#] public static int GetGeneration(object);

[C++] public: static int GetGeneration(Object*);

[JScript] public static function GetGeneration(Object) : int;

指定した弱い参照の対象となる現在のジェネレーション番号を返します。

[Visual Basic] Overloads Public Shared Function GetGeneration(WeakReference) As Integer

[C#] public static int GetGeneration(WeakReference);

[C++] public: static int GetGeneration(WeakReference*);

[JScript] public static function GetGeneration(WeakReference) : int;

使用例

[Visual Basic, C#, C++] メモ   ここでは、GetGeneration のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System

Namespace GCGetGenerationWeakExample

   Class MyGCCollectClass
      Private maxGarbage As Long = 1000

      Public Shared Sub Main()
         ' Create a strong reference to an object.
         Dim myGCCol As New MyGCCollectClass

         ' Put some objects in memory.
         myGCCol.MakeSomeGarbage()

         ' Get the generation of managed memory where myGCCol is stored.
         Console.WriteLine("The object is in generation: {0}", _
                            GC.GetGeneration(myGCCol))

         ' Perform a full garbage collection.
         ' Because there is a strong reference to myGCCol, it will
         ' not be garbage collected.
         GC.Collect()

         ' Get the generation of managed memory where myGCCol is stored.
         Console.WriteLine("The object is in generation: {0}", _
                            GC.GetGeneration(myGCCol))

         ' Create a WeakReference to myGCCol.
         Dim wkref As New WeakReference(myGCCol)
         ' Remove the strong reference to myGCCol.
         myGCCol = Nothing

         ' Get the generation of managed memory where wkref is stored.
         Console.WriteLine("The WeakReference to the object is in generation: {0}", _
                           GC.GetGeneration(wkref))

         ' Perform another full garbage collection.
         ' A WeakReference will not survive a garbage collection.
         GC.Collect()

         ' Try to get the generation of managed memory where wkref is stored.
         ' Because it has been collected, an exception will be thrown.
         Try
            Console.WriteLine("The WeakReference to the object is in generation: {0}", _
                               GC.GetGeneration(wkref))
            Console.Read()
         Catch e As Exception
            Console.WriteLine("The WeakReference to the object " & _
                              "has been garbage collected: '{0}'", e)
            Console.Read()
         End Try
      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 GCGetGenerationWeakExample
{
    public class MyGCCollectClass
    {
        private const long maxGarbage = 1000;
      
        static void Main()
        {
            // Create a strong reference to an object.
            MyGCCollectClass myGCCol = new MyGCCollectClass();

            // Put some objects in memory.
            myGCCol.MakeSomeGarbage();
            
            // Get the generation of managed memory where myGCCol is stored.
            Console.WriteLine("The object is in generation: {0}", GC.GetGeneration(myGCCol));
                        
            // Perform a full garbage collection.
            // Because there is a strong reference to myGCCol, it will
            // not be garbage collected.
            GC.Collect();
            
            // Get the generation of managed memory where myGCCol is stored.
            Console.WriteLine("The object is in generation: {0}", GC.GetGeneration(myGCCol));
            
            // Create a WeakReference to myGCCol.
            WeakReference wkref = new WeakReference(myGCCol);
            // Remove the strong reference to myGCCol.
            myGCCol = null;
            
            // Get the generation of managed memory where wkref is stored.
            Console.WriteLine("The WeakReference to the object is in generation: {0}", GC.GetGeneration(wkref));
            
            // Perform another full garbage collection.
            // A WeakReference will not survive a garbage collection.
            GC.Collect();
        
            // Try to get the generation of managed memory where wkref is stored.
            // Because it has been collected, an exception will be thrown.
            try
            {
                Console.WriteLine("The WeakReference to the object is in generation: {0}", GC.GetGeneration(wkref));
                Console.Read();
            }
            catch(Exception e)
            {
                Console.WriteLine("The WeakReference to the object has been garbage collected: '{0}'", e);
                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;

__gc class MyGCCollectClass {
   static const long  maxGarbage = 1000;
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() {
   // Create a strong reference to an Object.
   MyGCCollectClass* myGCCol = new MyGCCollectClass();

   // Put some objects in memory.
   myGCCol->MakeSomeGarbage();

   // Get the generation of managed memory where myGCCol is stored.
   Console::WriteLine(S"The object is in generation: {0}",  __box(GC::GetGeneration(myGCCol)));

   // Perform a full garbage collection.
   // Because there is a strong reference to myGCCol, it will
   // not be garbage collected.
   GC::Collect();

   // Get the generation of managed memory where myGCCol is stored.
   Console::WriteLine(S"The object is in generation: {0}", __box( GC::GetGeneration(myGCCol)));

   // Create a WeakReference to myGCCol.
   WeakReference* wkref = new WeakReference(myGCCol);
   // Remove the strong reference to myGCCol.
   myGCCol = 0;

   // Get the generation of managed memory where wkref is stored.
   Console::WriteLine(S"The WeakReference to the object is in generation: {0}", __box( GC::GetGeneration(wkref)));

   // Perform another full garbage collection.
   // A WeakReference will not survive a garbage collection.
   GC::Collect();

   // Try to get the generation of managed memory where wkref is stored.
   // Because it has been collected, an exception will be thrown.
   try {
      Console::WriteLine(S"The WeakReference to the object is in generation: {0}", __box( GC::GetGeneration(wkref)));
      Console::Read();
   } catch (Exception* e) {
      Console::WriteLine(S"The WeakReference to the object has been garbage collected: ' {0}'", e);
   }
}

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

参照

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