TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# MemoryFailPoint and InsufficientMemoryException

Use a MemoryFailPoint to ensure the system has enough memory to run code that requires that memory.
MemoryFailPoint. A complex algorithm requires considerable memory—and the system should ensure it has enough before even attempting to run the algorithm.
With MemoryFailPoint, an exception is thrown if there is not enough memory. We pass an int to MemoryFailPoint. This is the number of required megabytes of memory.
An example. This program shows the MemoryFailPoint in a situation where it will always throw an exception. It tries to ensure the system has 1 million megabytes of RAM.

And: The system does not have this amount of RAM. So an InsufficientMemoryException is thrown.

Exception
C# program that uses MemoryFailPoint using System.Runtime; class Program { static void Main() { // This program for some reason needs a huge amount of memory. using (MemoryFailPoint point = new MemoryFailPoint(1000000)) { // An exception is thrown at this point. // ... The computer does not have enough memory. // ... So we do not need to execute the inner code. } } } Output Unhandled Exception: System.InsufficientMemoryException: Insufficient memory to meet the expected demands of an operation, and this system is likely to never satisfy this request. If this is a 32 bit system, consider booting in 3 GB mode. at System.Runtime.MemoryFailPoint..ctor(Int32 sizeInMegabytes) at Program.Main()...
Some notes. In complex systems, MemoryFailPoint can help improve reliability. We can avoid starting a method if we estimate it will not be able to finish.
Notes, reliability. For methods that are critical and require large amounts of RAM, MemoryFailPoint is probably something that should always be used.
A summary. With MemoryFailPoint, we improve complex systems by causing "early failures" instead of possibly causing worse problems because a method starts (but does not finish) its work.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf