OutOfMemoryException in C# is an exception that is thrown by the .NET framework execution engine when the program does not have enough memory to continue its execution. As its name suggests this exception will occur in our program when the CLR i.e. Common Language Runtime will not be able to allocate enough memory which will be required to perform certain operations of our program.
This exception does not always mean that we do not have enough space available in memory but sometimes it means that we do not have enough contiguous memory which is required by our program for allocation.
Syntax
The syntax to catch an OutOfMemoryException in C# is as follows:
try { //user code which can throw OutOfMemoryException } catch(OutOfMemoryException <em>exception</em>) { //statements to handle the exception }
The syntax to throw an OutOfMemoryException in C# is as follows:
throw new OutOfMemoryException();
In the above statement ‘throw’ is the keyword which is used to throw exceptions in C#.
In C#, we get OutOfMemoryException when our program does not have enough space to continue its execution. There could be many reasons for getting this exception. We also encounter this exception when in total we have enough space for our program to execute but this space is not contiguous for the allocations required to be done by our program. The two major reasons for this exception are as follows:
Trying to increase the length of a StringBuilder object beyond the length which is specified by the MaxCapacity property of StringBuilder.
We will get the exception saying “Insufficient memory to continue the execution of the program.”
The other reasons which can become the cause of this exception include:
Here are the following examples mention below
Example showing OutOfMemoryException thrown by the program when we try to expand the StringBuilder object beyond its maximum capacity.
Code:
using System; using System.Text; public class Program { public static void Main() { StringBuilder stringBuilder = new StringBuilder(17, 17); stringBuilder.Append("Welcome to the "); try { stringBuilder.Insert(0, "world of C# programming", 1); Console.WriteLine(stringBuilder.ToString()); Console.ReadLine(); } catch (OutOfMemoryException exception) { Console.WriteLine(exception.Message); Console.ReadLine(); } } }
Output:
Example showing a program that encounters OutOfMemoryException while trying to add the element in the list where the number of elements to be added is more than the capacity of the list.
Code:
using System; using System.Text; using System.Collections.Generic; namespace ConsoleApp4 { public class Program { public static void Main() { try { string[] strArray = GetArray(); Console.WriteLine(strArray); Console.ReadLine(); } catch (OutOfMemoryException exception) { Console.WriteLine(exception); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } public static string[] GetArray() { List<string> strList = new List<string>(); for (int i = 0; i <= int.MaxValue; i++) { strList.Add("Hello"); } return strList.ToArray(); } } }
Output:
We can avoid OutOfMemoryException in C# by keeping in mind the following points:
If the 64-bit platform is not available in the list then:
The OutOfMemoryException is a runtime exception that tells the programmer that there is no enough memory or there is a lack of contiguous memory for the allocations required by the C# program.
To avoid this exception the user should always take necessary precautions and should handle this exception.
The above is the detailed content of C# OutOfMemoryException. For more information, please follow other related articles on the PHP Chinese website!