MemoryFailPoint(Int32) 생성자

정의

성공적인 실행에 필요한 메모리 양을 지정하여 클래스의 MemoryFailPoint 새 인스턴스를 초기화합니다.

public:
 MemoryFailPoint(int sizeInMegabytes);
public MemoryFailPoint(int sizeInMegabytes);
[System.Security.SecurityCritical]
public MemoryFailPoint(int sizeInMegabytes);
new System.Runtime.MemoryFailPoint : int -> System.Runtime.MemoryFailPoint
[<System.Security.SecurityCritical>]
new System.Runtime.MemoryFailPoint : int -> System.Runtime.MemoryFailPoint
Public Sub New (sizeInMegabytes As Integer)

매개 변수

sizeInMegabytes
Int32

필요한 메모리 크기(메가바이트)입니다. 양수 값이어야 합니다.

특성

예외

지정된 메모리 크기가 음수 또는 0입니다.

게이트로 보호되는 코드 실행을 시작하기 위한 메모리가 부족합니다.

예제

다음 예제에서는 실행할 때 메서드에 필요한 메모리 양을 확인하는 방법을 보여 줍니다. 이 코드 예제는 클래스에 제공된 더 큰 예제의 MemoryFailPoint 일부입니다.

private static int EstimateMemoryUsageInMB()
{
    int memUsageInMB = 0;

    long memBefore = GC.GetTotalMemory(true);
    int numGen0Collections = GC.CollectionCount(0);
    // Execute a test version of the method to estimate memory requirements.
    // This test method only exists to determine the memory requirements.
    ThreadMethod();
    // Includes garbage generated by the worker function.
    long memAfter = GC.GetTotalMemory(false);
    // If a garbage collection occurs during the measuring, you might need a greater memory requirement.
    Console.WriteLine("Did a GC occur while measuring?  {0}", numGen0Collections == GC.CollectionCount(0));
    // Set the field used as the parameter for the MemoryFailPoint constructor.
    long memUsage = (memAfter - memBefore);
    if (memUsage < 0)
    {
        Console.WriteLine("GC's occurred while measuring memory usage.  Try measuring again.");
        memUsage = 1 << 20;
    }

    // Round up to the nearest MB.
    memUsageInMB = (int)(1 + (memUsage >> 20));
    Console.WriteLine("Memory usage estimate: {0} bytes, rounded to {1} MB", memUsage, memUsageInMB);
    return memUsageInMB;
}

설명

애플리케이션에서 작업 항목을 처리하는 데 사용하는 메모리 양을 경험적으로 확인할 수 있습니다. 애플리케이션에서 요청을 처리하는 데 필요한 메모리 양을 예측하려면 이 메서드를 GC.GetTotalMemory 사용하여 작업 항목을 처리하는 메서드를 호출하기 전과 후에 사용 가능한 메모리 양을 확인하는 것이 좋습니다. 매개 변수의 MemoryFailPoint 값을 동적으로 결정하는 코드 예제는 클래스를 sizeInMegabytes 참조하세요.

적용 대상