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

必要なメモリ サイズ (単位は MB)。 必ず正の値を指定します。

属性

例外

指定したメモリ サイズが負の値です。

ゲートによって保護されているコードの実行を開始するためのメモリが不足しています。

次の例では、メソッドの実行時に必要なメモリ量を確認する方法を示します。 このコード例は、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 参照してください。

適用対象