GC 類別

定義

控制系統記憶體回收行程,它是自動回收未使用之記憶體的服務。

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 namespace System;
const long maxGarbage = 1000;
ref 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 = gcnew Version;

      }
   }

};

int main()
{
   MyGCCollectClass^ myGCCol = gcnew 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 ) );
}
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)

告知執行階段,在排定記憶體回收時應考慮的大量 Unmanaged 記憶體配置。

AllocateArray<T>(Int32, Boolean)

配置陣列。

AllocateUninitializedArray<T>(Int32, Boolean)

如果可能的話,會在略過零初始化的情況下配置陣列。

CancelFullGCNotification()

取消註冊記憶體回收通知。

Collect()

強制立即執行所有層代的記憶體回收。

Collect(Int32)

強制立即執行層代 0 至指定層代的記憶體回收。

Collect(Int32, GCCollectionMode)

GCCollectionMode 值所指定的時間,強制執行層代 0 至指定層代的記憶體回收。

Collect(Int32, GCCollectionMode, Boolean)

GCCollectionMode 值 (含有指定是否應該封鎖回收作業的值) 所指定的時間,強制執行從層代 0 到指定之層代的記憶體回收。

Collect(Int32, GCCollectionMode, Boolean, Boolean)

GCCollectionMode 值 (含有指定是否應該封鎖及壓縮回收作業的值) 所指定的時間,強制執行從層代 0 到指定之層代的記憶體回收。

CollectionCount(Int32)

傳回指定物件層代已發生記憶體回收的次數。

EndNoGCRegion()

結束無 GC 區域延遲模式。

GetAllocatedBytesForCurrentThread()

取得自目前執行緒的存留期開始,已配置給該執行緒的位元組總數。

GetConfigurationVariables()

取得垃圾收集行程所使用的組態。

GetGCMemoryInfo()

取得記憶體回收的記憶體資訊。

GetGCMemoryInfo(GCKind)

取得記憶體回收的記憶體資訊。

GetGeneration(Object)

傳回指定物件的目前層代編號。

GetGeneration(WeakReference)

傳回指定弱式參考之目標的目前層代編號。

GetTotalAllocatedBytes(Boolean)

取得處理序存留期間配置的位元組計數。 傳回的值不包含任何原生配置。

GetTotalMemory(Boolean)

擷取排除片段的堆積大小。 例如,如果 GC 堆積大小總計是 100 mb,而片段,也就是可用物件佔用的空間,則此 API 會報告 60mb。 參數會指出這個方法是否可以先等待短暫間隔後再傳回,以便讓系統回收記憶體和最終處理物件。

GetTotalPauseDuration()

取得從進程開始以來,GC 中暫停的總時間量。

KeepAlive(Object)

參考指定的物件,使它從目前常式的開始至呼叫這個方法時都不適合記憶體回收。

RefreshMemoryLimit()

指示垃圾收集行程偵測系統上的各種記憶體限制,以重新設定本身。

RegisterForFullGCNotification(Int32, Int32)

指定當情況需要完整記憶體回收時和回收完成時,應引發記憶體回收通知。

RegisterNoGCRegionCallback(Int64, Action)

在未配置任何 GC 區域中的特定記憶體數量時,註冊要叫用的回呼。

RemoveMemoryPressure(Int64)

告知執行階段,Unmanaged 記憶體已釋放,所以排定記憶體回收時不需要再考慮它。

ReRegisterForFinalize(Object)

要求系統為指定物件 (先前已經為其呼叫 SuppressFinalize(Object)) 呼叫完成項。

SuppressFinalize(Object)

要求 Common Language Runtime 不要為指定之物件呼叫完成項。

TryStartNoGCRegion(Int64)

嘗試在有指定數量記憶體可用時,於執行關鍵路徑期間,禁止記憶體回收。

TryStartNoGCRegion(Int64, Boolean)

嘗試在有指定數量記憶體可用時,於執行關鍵路徑期間,禁止記憶體回收,並控制在最初沒有足夠的記憶體可用時,記憶體回收行程是否執行完全封鎖記憶體回收。

TryStartNoGCRegion(Int64, Int64)

嘗試在大型物件堆積與小型物件堆積有指定數量記憶體可用時,於執行關鍵路徑期間,禁止記憶體回收。

TryStartNoGCRegion(Int64, Int64, Boolean)

嘗試在大型物件堆積與小型物件堆積有指定數量記憶體可用時,於執行關鍵路徑期間,禁止記憶體回收,並控制在最初沒有足夠的記憶體可用時,記憶體回收行程是否執行完全封鎖記憶體回收。

WaitForFullGCApproach()

傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

WaitForFullGCApproach(Int32)

在指定的逾時期間傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

WaitForFullGCApproach(TimeSpan)

在指定的逾時期間傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

WaitForFullGCComplete()

傳回註冊通知的狀態,以判斷 Common Language Runtime 是否已完成完整的封鎖記憶體回收。

WaitForFullGCComplete(Int32)

在指定的逾時期間傳回註冊通知的狀態,以判斷 Common Language Runtime 是否已完成完整的封鎖記憶體回收。

WaitForFullGCComplete(TimeSpan)

傳回有關封鎖垃圾收集是否已完成的已註冊通知狀態。 可能會無限期等候完整集合。

WaitForPendingFinalizers()

暫止目前的執行緒,直到處理完成項佇列的執行緒已經清空該佇列為止。

適用於

另請參閱