MemoryFailPoint クラス
アセンブリ: mscorlib (mscorlib.dll 内)

Public NotInheritable Class MemoryFailPoint Inherits CriticalFinalizerObject Implements IDisposable
public sealed class MemoryFailPoint : CriticalFinalizerObject, IDisposable
public final class MemoryFailPoint extends CriticalFinalizerObject implements IDisposable
public final class MemoryFailPoint extends CriticalFinalizerObject implements IDisposable

![]() |
---|
MemoryFailPoint クラスのインスタンスを作成すると、メモリ ゲートが作成されます。メモリ ゲートとは、大量のメモリを必要とするアクティビティを開始する前に、十分なリソースがあるかどうかをチェックすることです。チェックに合格しなかった場合、操作を開始できないようにする InsufficientMemoryException がスローされるため、リソース不足が原因となってアプリケーションが実行中に失敗する可能性が減少します。これにより、アプリケーションでは、パフォーマンスを低下させ、OutOfMemoryException や、コード内の任意の場所で OutOfMemoryException を誤って処理したことが原因で発生する状態の破損を避けることができます。
InsufficientMemoryException をスローすることにより、操作を完了できそうにないという回復可能である予測と、部分的に完了済みの操作で発生した、状態破損の可能性のある OutOfMemoryException とをアプリケーションで識別できます。これにより、アプリケーションは、現在の AppDomain のアンロードやプロセスのリサイクルが必要となる可能性のあるペシミスティック発生ポリシーの頻度を減らすことができます。
MemoryFailPoint は、すべてのガベージ コレクション ヒープで十分なメモリと連続した仮想アドレス空間が利用できるかどうかをチェックし、必要に応じてスワップ ファイルのサイズを大きくします。MemoryFailPoint は、ゲートの有効期間中にメモリが長期にわたって使用できるかどうかについては保証しませんが、呼び出し元は常に Dispose を呼び出して、MemoryFailPoint に関連付けられたリソースを解放する必要があります。
メモリ ゲートを使用するには、操作で使用が想定されるメモリのメガバイト数を指定して、MemoryFailPoint オブジェクトを作成する必要があります。十分なメモリを使用できない場合は、InsufficientMemoryException が発生します。
コンストラクタのパラメータは正の整数である必要があります。負の値であると、ArgumentOutOfRangeException が発生します。

MemoryFailPoint は、メモリの不足による破損を防ぐために、アプリケーションが自身の処理速度を低下させることができるようにデザインされています。これは、構文のスコープ内で使用する必要があります。作業キュー内の項目を処理するためにスレッドを開始するコード例を次に示します。各スレッドを開始する前に、MemoryFailPoint を使用して、利用できるメモリ リソースをチェックします。例外がスローされた場合、メイン メソッドはメモリが使用できるようになるのを待って、次のスレッドを開始します。
using System; using System.Runtime; using System.IO; using System.Threading; using System.Collections.Generic; using System.Collections; class MemoryFailPointExample { // Allocate in chunks of 64 megabytes. private const uint chunkSize = 64 << 20; // Use more than the total user-available address space (on 32 bit machines) // to drive towards getting an InsufficientMemoryException. private const uint numWorkItems = 1 + ((1U << 31) / chunkSize); static Queue workQueue = new Queue(50); // This value can be computed separately and hard-coded into the application. // The method is included to illustrate the technique. 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; } static void Main() { Console.WriteLine("Attempts to allocate more than 2 GB of memory across worker threads."); int memUsageInMB = EstimateMemoryUsageInMB(); // For a production application consider using the threadpool instead. Thread[] threads = new Thread[numWorkItems]; // Create a work queue to be processed by multiple threads. int n = 0; for (n = 0; n < numWorkItems; n++) workQueue.Enqueue(n); // Continue to launch threads until the work queue is empty. while (workQueue.Count > 0) { Console.WriteLine(" GC heap (live + garbage): {0} MB", GC.GetTotalMemory(false) >> 20); MemoryFailPoint memFailPoint = null; try { // Check for available memory. memFailPoint = new MemoryFailPoint(memUsageInMB); n = (int)workQueue.Dequeue(); threads[n] = new Thread(new ParameterizedThreadStart(ThreadMethod)); WorkerState state = new WorkerState(n, memFailPoint); threads[n].Start(state); Thread.Sleep(10); } catch (InsufficientMemoryException e) { // MemoryFailPoint threw an exception, handle by sleeping for a while, then // continue processing the queue. Console.WriteLine("Expected InsufficientMemoryException thrown. Message: " + e.Message); // We could optionally sleep until a running worker thread // has finished, like this: threads[joinCount++].Join(); Thread.Sleep(1000); } } Console.WriteLine("WorkQueue is empty - blocking to ensure all threads quit (each thread sleeps for 10 seconds)"); foreach (Thread t in threads) t.Join(); Console.WriteLine("All worker threads are finished - exiting application."); } // Test version of the working code to determine memory requirements. static void ThreadMethod() { byte[] bytes = new byte[chunkSize]; } internal class WorkerState { internal int _threadNumber; internal MemoryFailPoint _memFailPoint; internal WorkerState(int threadNumber, MemoryFailPoint memoryFailPoint) { _threadNumber = threadNumber; _memFailPoint = memoryFailPoint; } internal int ThreadNumber { get { return _threadNumber; } } internal MemoryFailPoint MemoryFailPoint { get { return _memFailPoint; } } } // The method that does the work. static void ThreadMethod(Object o) { WorkerState state = (WorkerState)o; Console.WriteLine("Executing ThreadMethod, " + "thread number {0}.", state.ThreadNumber); byte[] bytes = null; try { bytes = new byte[chunkSize]; // Allocated all the memory needed for this workitem. // Now dispose of the MemoryFailPoint, then process the workitem. state.MemoryFailPoint.Dispose(); } catch (OutOfMemoryException oom) { Console.Beep(); Console.WriteLine("Unexpected OutOfMemory exception thrown: " + oom); } // Do work here, possibly taking a lock if this app needs // synchronization between worker threads and/or the main thread. // Keep the thread alive for awhile to simulate a running thread. Thread.Sleep(10000); // A real thread would use the byte[], but to be an illustrative sample, // explicitly keep the byte[] alive to help exhaust the memory. GC.KeepAlive(bytes); Console.WriteLine("Thread {0} is finished.", state.ThreadNumber); } }


System.Runtime.ConstrainedExecution.CriticalFinalizerObject
System.Runtime.MemoryFailPoint


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


MemoryFailPoint コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)



作業項目を処理するためにアプリケーションが使用するメモリ量は、経験的に決定できます。アプリケーションが要求を処理するために必要とするメモリ量を推定するには、GC.GetTotalMemory メソッドを使用して、作業項目を処理するメソッドの呼び出しの前後に利用できるメモリ量を判断してください。sizeInMegabytes パラメータの値を動的に決定するコード例については、MemoryFailPoint クラスのトピックを参照してください。

実行時にメソッドが必要とするメモリ量を判断する方法を次のコード例に示します。このコード例は、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; }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


MemoryFailPoint メソッド

名前 | 説明 | |
---|---|---|
![]() | Dispose | MemoryFailPoint によって使用されているすべてのリソースを解放します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

MemoryFailPoint メンバ
実行前に十分なメモリ リソースがあるかどうかをチェックします。このクラスは継承できません。
MemoryFailPoint データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Dispose | MemoryFailPoint によって使用されているすべてのリソースを解放します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

- MemoryFailPointのページへのリンク