How to predict a lack of memory?

In my C # program, I perform various memory operations. Depending on the amount of available memory and various inconsistent circumstances, the program crashes with an OutOfMemoryException at different stages.

I would like to stop processing at some point when it is more or less obvious that the program will work with OOM in the near future.

However, there is no fixed threshold for this; Other users may have more (or less) memory, another OS with its memory specifics, and so on.

I do not want to simply verify that the software consumes more than, for example, 500 MB, as this may be too high or too low a limit.

Is there any reliable way to predict the upcoming OOM in .NET?

+7
source share
3 answers
+6
source

The answer to sorting is that this is probably not the best solution - instead of anticipating when you are going to run out of memory and try to do something, you probably should either try to reduce the memory consumption of your application, or just to until an OutOfMemoryException is thrown and then flushed .

An exception is excluded whenever the .Net runtime cannot allocate the requested memory - this may be because the computer has run out of physical memory and the page file has been disabled (or the machine has run out of disk space), or it may be due to the fact that the virtual memory space of the process is too fragmented to allocate the required block of memory (which can happen when working with large objects). Predicting when it will be quite difficult.

You can use the MemoryFailPoint class to check whether access to a specific amount of memory will be available before starting an operation that uses a large amount of memory, however this class does not guarantee that the memory will remain available for the entire duration of the operation, and therefore your application may still exit out of order with the exception of OOM. Although this class may be useful in some scenarios to try to reduce OOM exceptions (and, in turn, so as not to damage the state of the application), it will probably not be the magic bullet solution for your problem.

+2
source

Many of the OOM exceptions that I have seen so far have been caused by an incorrect written application with a memory leak or third-party components. Consider using a data structure optimized for your work.

OOM detection is quite difficult because they are accidentally thrown away. Perhaps you can use MemoryFailPoint and performance counters (available memory) to provide sufficient available memory.

0
source

All Articles