Export (0) Print
Expand All

MemoryFailPoint Constructor

Initializes a new instance of the MemoryFailPoint class, specifying the amount of memory required for successful execution.

Namespace:  System.Runtime
Assembly:  mscorlib (in mscorlib.dll)

public MemoryFailPoint(
	int sizeInMegabytes
)

Parameters

sizeInMegabytes
Type: System.Int32

The required memory size, in megabytes. This must be a positive value.

ExceptionCondition
ArgumentOutOfRangeException

The specified memory size is negative.

InsufficientMemoryException

There is insufficient memory to begin execution of the code protected by the gate.

The amount of memory used by your application to process a work item can be determined empirically. To estimate the amount of memory your application needs to process a request, consider using the GC.GetTotalMemory method to determine the amount of memory available before and after calling the method that processes the work item. See the MemoryFailPoint class for a code example that dynamically determines the value for the sizeInMegabytes parameter.

The following example demonstrates how to determine the amount of memory a method requires when executing. This code example is part of a larger example provided for the MemoryFailPoint class.

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;
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • SecurityCriticalAttribute 

    requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft