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)

가비지 수집을 예약할 때 고려해야 할 많은 양의 관리되지 않는 메모리 할당을 런타임에 알립니다.

AllocateArray<T>(Int32, Boolean)

배열을 할당합니다.

AllocateUninitializedArray<T>(Int32, Boolean)

가능하면 0 초기화를 건너뛰면서 배열을 할당합니다.

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 힙 크기가 100mb이고 조각화가 사용 가능한 개체에서 차지하는 공간이 40mb를 차지하면 이 API는 60mb를 보고합니다. 매개 변수는 시스템에서 가비지를 수집하고 개체를 종료할 수 있도록 이 메서드가 반환되기 전에 잠시 동안 대기할 수 있는지 여부를 나타냅니다.

GetTotalPauseDuration()

프로세스가 시작된 이후 GC에서 일시 중지된 총 시간을 가져옵니다.

KeepAlive(Object)

지정된 개체를 참조하여 현재 루틴이 시작된 지점에서 이 메서드가 호출된 지점까지 가비지 컬렉션이 불가능하도록 합니다.

RefreshMemoryLimit()

시스템의 다양한 메모리 제한을 검색하여 가비지 수집기에서 자신을 다시 구성하도록 지시합니다.

RegisterForFullGCNotification(Int32, Int32)

전체 가비지 수집에 유리한 조건인 경우와 수집이 완료된 경우에 가비지 수집 알림이 발생하도록 지정합니다.

RegisterNoGCRegionCallback(Int64, Action)

특정 양의 메모리가 GC 없음 지역에 할당될 때 호출할 콜백을 등록합니다.

RemoveMemoryPressure(Int64)

관리되는 메모리가 해제되었고 가비지 수집을 예약할 때 더 이상 고려할 필요가 없다고 런타임에 알립니다.

ReRegisterForFinalize(Object)

SuppressFinalize(Object)가 이전에 호출된 지정된 개체에 대해 시스템에서 종료자를 호출하도록 요청합니다.

SuppressFinalize(Object)

공용 언어 런타임에서 지정된 개체에 대해 종료자를 호출하지 않도록 요청합니다.

TryStartNoGCRegion(Int64)

지정된 양의 메모리를 사용할 수 있는 경우 중요한 경로를 실행하는 동안에는 가비지 수집이 허용되지 않습니다.

TryStartNoGCRegion(Int64, Boolean)

지정된 양의 메모리를 사용할 수 있는 경우 중요한 경로를 실행하는 동안 가비지 수집이 허용되지 않고, 초기에 사용할 수 있는 메모리가 충분하지 않은 경우 가비지 수집기가 전체 차단 가비지 수집 수행 여부를 제어합니다.

TryStartNoGCRegion(Int64, Int64)

큰 개체 힙 및 작은 개체 힙에 지정된 양의 메모리를 사용할 수 있는 경우 중요한 경로를 실행하는 동안에는 가비지 수집이 허용되지 않습니다.

TryStartNoGCRegion(Int64, Int64, Boolean)

큰 개체 힙 및 작은 개체 힙에 지정된 양의 메모리를 사용할 수 있는 경우 중요한 경로를 실행하는 동안 가비지 수집이 허용되지 않고, 초기에 사용할 수 있는 메모리가 충분하지 않은 경우 가비지 수집기가 전체 차단 가비지 수집 수행 여부를 제어합니다.

WaitForFullGCApproach()

공용 언어 런타임에 의한 전체 차단 가비지 컬렉션이 임박하고 있는지 여부를 확인하기 위한 등록된 알림의 상태를 반환합니다.

WaitForFullGCApproach(Int32)

공용 언어 런타임에 의한 전체 차단 가비지 컬렉션이 임박하고 있는지 여부를 확인하기 위한 등록된 알림의 상태를 지정된 제한 시간 내에 반환합니다.

WaitForFullGCApproach(TimeSpan)

공용 언어 런타임에 의한 전체 차단 가비지 컬렉션이 임박하고 있는지 여부를 확인하기 위한 등록된 알림의 상태를 지정된 제한 시간 내에 반환합니다.

WaitForFullGCComplete()

공용 언어 런타임에 의한 전체 차단 가비지 수집이 완료되었는지 여부를 확인하기 위한 등록된 알림의 상태를 반환합니다.

WaitForFullGCComplete(Int32)

공용 언어 런타임에 의한 전체 차단 가비지 컬렉션이 완료되었는지 여부를 확인하기 위한 등록된 알림의 상태를 지정된 제한 시간 내에 반환합니다.

WaitForFullGCComplete(TimeSpan)

차단 가비지 수집이 완료되었는지 여부에 대한 등록된 알림의 상태 반환합니다. 전체 컬렉션을 무기한 대기할 수 있습니다.

WaitForPendingFinalizers()

종료자의 큐를 처리하는 스레드에서 해당 큐를 비울 때까지 현재 스레드를 일시 중단합니다.

적용 대상

추가 정보