MemoryFailPoint(Int32) コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
正常に実行するために必要なメモリ量を指定して、 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 クラスを参照してください。