When implementing an algorithm, it is possible to know beforehand that the algorithm requires a lot of memory and consumes a lot of memory. It might just start executing the algorithm, and if the memory runs out, the CLR throws a OutOfMemoryException. In this case, much of the work done in the past was wasted. In addition, this anomaly needs to be captured to allow the program to recover gracefully.
The System.Runtime namespace provides a Memoryfailpoint class that allows you to check for ample memory before starting a large memory-consuming algorithm. You can view the class on MSDN.
The method of using this class is very simple. First, an instance of the class is constructed. At construction time, you pass the number of megabytes that your algorithm may require. In-house constructs perform the following checks, triggering corresponding actions in order.
- Does the paging file of the system have enough free space, and is there enough contiguous virtual address space in the process to meet the requirements? Note that memory that has been reserved logically by another memoryfailpoint construction call is deducted.
- If there is not enough space, force a garbage collection and try to free up some space.
- If there is still not enough paging file space, try extending the paging file if the paging file cannot be extended enough to throw a insufficientmemoryexception
- If there is still no contiguous virtual address space, throw a insufficientmemoryexception
- If a large enough paging file space and virtual address space are found, the megabytes are added to a private static variable defined by the Memoryfailpoint class, preserving the requested megabyte, which is done in a thread-safe manner so that multiple threads can construct instances of the class at the same time. and ensure that the memory they request is logically preserved as long as the constructor does not throw an exception.
If the constructor of the Memoryfailpoint class throws a insufficientmemoryexception, your application can release some of the resources it is using, or reduce its performance to reduce the probability that the CLR will run out of insufficientmemoryexception in the future. The insufficientmemoryexception is derived from OutOfMemoryException.
Note: If Memoryfailpoint does not logically throw an exception, it indicates that the requested memory has been logically persisted. It is now possible to turn on an algorithm that consumes large memory. Note, however, that the requested memory is not physically allocated. This means that the algorithm is only more likely to get the required memory and run successfully. Even if the constructor does not throw an exception, Memoryfailpoint does not guarantee that the algorithm obtains the memory he needs. The purpose of this class is to help you write more robust applications.
After the algorithm is executed, the Dispose method is called on the constructed Memoryfailpoint object. The use of the Memoryfailpoint class is demonstrated in internal dispose by subtracting the number of reserved bytes from the static field of the Memoryfailpoint class in a thread-safe manner:
static void Main (string[] args)
{
Try
{
using (System.Runtime.MemoryFailPoint MFP = new System.Runtime.MemoryFailPoint (1500))
{
This is where the memory-intensive algorithm is executed.
}
}
catch (Insufficientmemoryexception e)
{
Console.WriteLine (e);
}
}
12th: Predicting the success of operations that require large amounts of memory