GC クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
システム ガベージ コレクター (未使用のメモリを自動的に回収するサービス) を制御します。
public ref class GC abstract sealed
public ref class GC sealed
public static class GC
public sealed class GC
type GC = class
Public Class GC
Public NotInheritable Class GC
- 継承
-
GC
例
次の例では、いくつかの GC メソッドを使用して、未使用のオブジェクトのブロックに関する生成情報とメモリ情報を取得し、コンソールに出力します。 その後、未使用のオブジェクトが収集され、結果のメモリ合計が表示されます。
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();
}
}
}
}
open System
let maxGarbage = 1000
type MyGCCollectClass() =
member _.MakeSomeGarbage() =
for _ = 1 to maxGarbage do
// Create objects and release them to fill up memory with unused objects.
Version() |> ignore
[<EntryPoint>]
let main _ =
let myGCCol = MyGCCollectClass()
// Determine the maximum number of generations the system
// garbage collector currently supports.
printfn $"The highest generation is {GC.MaxGeneration}"
myGCCol.MakeSomeGarbage()
// Determine which generation myGCCol object is stored in.
printfn $"Generation: {GC.GetGeneration myGCCol}"
// Determine the best available approximation of the number
// of bytes currently allocated in managed memory.
printfn $"Total Memory: {GC.GetTotalMemory false}"
// Perform a collection of generation 0 only.
GC.Collect 0
// Determine which generation myGCCol object is stored in.
printfn $"Generation: {GC.GetGeneration myGCCol}"
printfn $"Total Memory: {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.
printfn $"Generation: {GC.GetGeneration myGCCol}"
printfn $"Total Memory: {GC.GetTotalMemory false}"
0
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
注釈
この API の詳細については、GC の 補足 API 解説を参照してください。
プロパティ
| 名前 | 説明 |
|---|---|
| MaxGeneration |
システムが現在サポートしている世代の最大数を取得します。 |
メソッド
| 名前 | 説明 |
|---|---|
| AddMemoryPressure(Int64) |
ガベージ コレクションをスケジュールするときに考慮する必要があるアンマネージ メモリの大きな割り当てをランタイムに通知します。 |
| CancelFullGCNotification() |
ガベージ コレクション通知の登録を取り消します。 |
| Collect() |
すべての世代の即時ガベージ コレクションを強制します。 |
| Collect(Int32, GCCollectionMode, Boolean, Boolean) |
ジェネレーション 0 から指定した世代までのガベージ コレクションを、 GCCollectionMode 値で指定された時点で強制的に生成し、コレクションをブロックおよび圧縮するかどうかを指定する値を指定します。 |
| Collect(Int32, GCCollectionMode, Boolean) |
ジェネレーション 0 から指定したジェネレーションまでのガベージ コレクションを、 GCCollectionMode 値で指定された時点で強制的に実行し、コレクションをブロックするかどうかを指定する値を指定します。 |
| Collect(Int32, GCCollectionMode) |
GCCollectionMode値で指定された時点で、ジェネレーション 0 から指定した世代までのガベージ コレクションを強制的に実行します。 |
| Collect(Int32) |
ジェネレーション 0 から指定した世代までの即時ガベージ コレクションを強制します。 |
| CollectionCount(Int32) |
指定したオブジェクトの生成に対してガベージ コレクションが発生した回数を返します。 |
| EndNoGCRegion() |
GC リージョン待機時間なしモードを終了します。 |
| GetAllocatedBytesForCurrentThread() |
有効期間の開始以降に現在のスレッドに割り当てられた合計バイト数を取得します。 |
| GetGeneration(Object) |
指定したオブジェクトの現在の世代番号を返します。 |
| GetGeneration(WeakReference) |
指定した弱参照のターゲットの現在の生成番号を返します。 |
| GetTotalMemory(Boolean) |
断片化を除くヒープ サイズを取得します。 たとえば、合計 GC ヒープ サイズが 100 mb で、断片化 (つまり、空きオブジェクトによって占有される領域) が 40 mb を占める場合、この API は 60 mb を報告します。 パラメーターは、システムがガベージ コレクションを実行してオブジェクトを最終処理できるように、このメソッドが戻る前に短い間隔で待機できるかどうかを示します。 |
| KeepAlive(Object) |
指定したオブジェクトを参照します。これにより、現在のルーチンの先頭からこのメソッドが呼び出される時点までのガベージ コレクションに不適格になります。 |
| RegisterForFullGCNotification(Int32, Int32) |
条件が完全ガベージ コレクションを優先する場合、およびコレクションが完了したときに、ガベージ コレクション通知を発生させる必要があることを指定します。 |
| RemoveMemoryPressure(Int64) |
アンマネージ メモリが解放され、ガベージ コレクションをスケジュールするときに考慮する必要がなくなったことをランタイムに通知します。 |
| ReRegisterForFinalize(Object) |
SuppressFinalize(Object)が以前に呼び出された指定したオブジェクトのファイナライザーをシステムが呼び出す要求。 |
| SuppressFinalize(Object) |
共通言語ランタイムが、指定したオブジェクトのファイナライザーを呼び出さないことを要求します。 |
| TryStartNoGCRegion(Int64, Boolean) |
指定した量のメモリが使用可能な場合、クリティカル パスの実行中にガベージ コレクションを許可しないように試み、メモリが十分でない場合にガベージ コレクターがフル ブロッキング ガベージ コレクションを実行するかどうかを制御します。 |
| TryStartNoGCRegion(Int64, Int64, Boolean) |
大きなオブジェクト ヒープと小さなオブジェクト ヒープに対して指定した量のメモリが使用可能な場合に、クリティカル パスの実行中にガベージ コレクションを許可しないように試み、メモリが十分でない場合にガベージ コレクターがフル ブロッキング ガベージ コレクションを実行するかどうかを制御します。 |
| TryStartNoGCRegion(Int64, Int64) |
指定した量のメモリが大きなオブジェクト ヒープと小さなオブジェクト ヒープに対して使用できる場合、クリティカル パスの実行中にガベージ コレクションを禁止しようとします。 |
| TryStartNoGCRegion(Int64) |
指定した量のメモリが使用可能な場合、クリティカル パスの実行中にガベージ コレクションを禁止しようとします。 |
| WaitForFullGCApproach() |
共通言語ランタイムによる完全なブロッキング ガベージ コレクションが差し迫っているかどうかを判断するための登録済み通知の状態を返します。 |
| WaitForFullGCApproach(Int32) |
指定されたタイムアウト期間に、共通言語ランタイムによる完全なブロッキング ガベージ コレクションが差し迫っているかどうかを判断するための登録済み通知の状態を返します。 |
| WaitForFullGCComplete() |
共通言語ランタイムによる完全なブロッキング ガベージ コレクションが完了したかどうかを判断するための登録済み通知の状態を返します。 |
| WaitForFullGCComplete(Int32) |
指定したタイムアウト期間に、ランタイムが完了した共通言語による完全なブロッキング ガベージ コレクションかどうかを判断するための登録済み通知の状態を返します。 |
| WaitForPendingFinalizers() |
ファイナライザーのキューを処理しているスレッドがそのキューを空にするまで、現在のスレッドを中断します。 |