英語で読む

次の方法で共有


MemoryFailPoint(Int32) コンストラクター

定義

正常に実行するために必要なメモリの量を指定して、MemoryFailPoint クラスの新しいインスタンスを初期化します。

C#
public MemoryFailPoint(int sizeInMegabytes);
C#
[System.Security.SecurityCritical]
public MemoryFailPoint(int sizeInMegabytes);

パラメーター

sizeInMegabytes
Int32

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

属性

例外

指定されたメモリ サイズは負または 0 です。

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

次の例では、メソッドの実行時に必要なメモリ量を確認する方法を示します。 このコード例は、MemoryFailPoint クラスのために提供されている大規模な例の一部です。

C#
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 参照してください。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1