Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force an OutOfMemoryException in .Net

Tags:

.net

Sometimes it's helpful to get an application into a bad situation to see how it responds. Things like unplugging the network cable or removing power will show me how resilient my application is and where I have work to do.

To this end, I'm trying to figure out what the fastest way to force an OutOfMemoryException in .Net. Doing it in a simple console application will allow me to inject this scenario into a running application. There are obviously other things that need to be considered when dealing with OutOfMemoryExceptions (e.g. memory fragmentation and how the garbage collector has allocated the different generations) but that's not important for the scope of this experiment.

Update
To clarify the purpose of the question, it's important to note that simply throwing an out of memory exception isn't helpful since I want to see how the program will react when the memory pressure is increased. In essence, I want to stimulate the GC into an aggressive collection mode and monitor how that impacts the performance until the process dies from an out of memory exception.

like image 835
Matt Ruwe Avatar asked Aug 19 '16 18:08

Matt Ruwe


People also ask

How do you throw OutOfMemoryException?

Throw New OutOfMemoryException() Catch e As ArgumentException Console. WriteLine("ArgumentException in String. Insert") End Try ' Execute program logic. Catch e As OutOfMemoryException Console.

How do you fix system OutOfMemoryException exception of type system OutOfMemoryException was thrown?

OutOfMemoryException Exception of type 'System. OutOfMemoryException' was thrown. To resolve this issue, I had to restart Visual Studio or go to the Windows Task Manager and terminate IIS Express process. This error could happen due to a variety of reasons related to memory consumption of the application.

What does system out of memory mean?

Out of memory (OOM) is an often undesired state of computer operation where no additional memory can be allocated for use by programs or the operating system.


2 Answers

One example from MSDN..

The following example illustrates the OutOfMemoryException exception thrown by a call to the StringBuilder.Insert(Int32, String, Int32) method when the example tries to insert a string that would cause the object's Length property to exceed its maximum capacity

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder(15, 15);
      sb.Append("Substring #1 ");
      try {
         sb.Insert(0, "Substring #2 ", 1);
      }
      catch (OutOfMemoryException e) {
         Console.WriteLine("Out of Memory: {0}", e.Message);
      }
   }
}
// The example displays the following output:
//    Out of Memory: Insufficient memory to continue the execution of the program.

Further ,it says how to rectify the error.

Replace the call to the StringBuilder.StringBuilder(Int32, Int32) constructor with a call any other StringBuilder constructor overload. The maximum capacity of your StringBuilder object will be set to its default value, which is Int32.MaxValue.

Call the StringBuilder.StringBuilder(Int32, Int32) constructor with a maxCapacity value that is large enough to accommodate any expansions to the StringBuilder object.

like image 195
TheGameiswar Avatar answered Oct 03 '22 22:10

TheGameiswar


According to MSDN:

An OutOfMemoryException exception has two major causes:

  • You are attempting to expand a StringBuilder object beyond the length defined by its StringBuilder.MaxCapacity property.

  • The common language runtime cannot allocate enough contiguous memory to successfully perform an operation. This exception can be thrown by any property assignment or method call that requires a memory allocation. For more information on the cause of the OutOfMemoryException exception, see "Out of Memory" Does Not Refer to Physical Memory.

    This type of OutOfMemoryException exception represents a catastrophic failure. If you choose to handle the exception, you should include a catch block that calls the Environment.FailFast method to terminate your app and add an entry to the system event log, as the following example does.

It seems to me the first one with the string builder would be easier.

This works:

var sb = new StringBuilder(5, 5);
sb.Insert(0, "hello", 2);
like image 22
rory.ap Avatar answered Oct 04 '22 00:10

rory.ap