次の方法で共有


GC.GetTotalMemory メソッド

現在割り当てられていると思われるバイト数を取得します。このメソッドが、制御が返されるまでの間、システムがガベージ コレクションを行い、オブジェクトの終了操作を行うのを短い時間だけ待機するかどうかをパラメータで指定します。

Public Shared Function GetTotalMemory( _
   ByVal forceFullCollection As Boolean _) As Long
[C#]
public static long GetTotalMemory(boolforceFullCollection);
[C++]
public: static __int64 GetTotalMemory(boolforceFullCollection);
[JScript]
public static function GetTotalMemory(
   forceFullCollection : Boolean) : long;

パラメータ

  • forceFullCollection
    Boolean 値が true である場合、このメソッドは制御を戻す前にガベージ コレクションをしばらく待機させることができます。

戻り値

マネージ メモリに現在割り当てられているバイト数の近似値を表す数値。

解説

forceFullCollectiontrue である場合、システムがガベージ コレクションを行い、オブジェクトの終了操作を行うのを短い時間だけ待機します。この待機時間は、内部的に指定される制限時間で、完了したガベージ コレクションのサイクル数と、サイクル間で回復するメモリ量の変化によって決定されます。ガベージ コレクタは、アクセスできないすべてのメモリが収集されることは保証していません。

使用例

 
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 名前空間