Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate OutOfMemory exception

I need to refactor my project in order to make it immune to OutOfMemory exception.

There are huge collections used in my project and by changing one parameter I can make my program to be more accurate or use less of the memory...

OK, that's the background. What I would like to do is to run the routines in a loop:

  1. Run the subroutines with the default parameter.
  2. Catch the OutOfMemory exception, change the parameter and try to run it again.
  3. Do the 2nd point until parameters allow to run the subroutines without throwing the exception (usually, there will be only one change needed).

Now, I would like to test it. I know, that I can throw the OutOfMemory exception on my own, but I would like to simulate some real situation.

So the main question is:
Is there a way of setting some kind of memory limit for my program, after reaching which the OutOfMemory exception will be thrown automatically?
For example, I would like to set a limit, let's say 400MB of memory for my whole program to simulate the situation when there is such an amount of memory available in the system.
Can it be done?

like image 559
Gacek Avatar asked May 06 '10 15:05

Gacek


People also ask

How can OutOfMemory exception be prevented?

Well, according to the topic of the question, best way to avoid out of memory exception would be not to create objects that fill in that memory. Then you can calculate the length of your queue based on estimate of one object memory capacity. Another way would be to check for memory size in each worker thread.

What causes OutOfMemoryException?

When data structures or data sets that reside in memory become so large that the common language runtime is unable to allocate enough contiguous memory for them, an OutOfMemoryException exception results.

What is OutOfMemoryError in exception handling?

OutOfMemoryError exception. Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap. In this case, The garbage collector cannot make space available to accommodate a new object, and the heap cannot be expanded further.

How do you simulate out of memory error?

To simulate OutOfMemoryError , we could simply keep adding objects to a Java Collection such as LinkedList in a loop, until the JVM runs out of the heap memory and throws this error. In the example below, the infinite loop will add a new object of size 10MB on each iteration until it runs of memory.


4 Answers

This looks like a job for...System.Runtime.MemoryFailPoint.

http://msdn.microsoft.com/en-us/library/system.runtime.memoryfailpoint.aspx

I think the example in the link fits your situation. Set the MemoryFailPoint to whatever level you need and then catch the InsufficientMemoryException and adjust your input parameters accordingly.

like image 76
AlfredBr Avatar answered Sep 27 '22 15:09

AlfredBr


public void EatMemory()
{
    List<byte[]> wastedMemory = new List<byte[]>();

    while(true)
    {
        byte[] buffer = new byte[4096]; // Allocate 4kb
        wastedMemory.Add(buffer);
    }
}

Shouldn't take long unless you've got 12gb of ram :)

like image 44
Aren Avatar answered Sep 27 '22 15:09

Aren


Just allocate a very large array. You'll most likely start getting out of memory exceptions once your C# application reaches 1.2-1.6GB of RAM usage (usually on the lower side of that range, provided its targetting x86).

like image 27
Reed Copsey Avatar answered Sep 27 '22 16:09

Reed Copsey


string value = new string('crazy', int.MaxValue);
like image 22
user2530499 Avatar answered Sep 27 '22 17:09

user2530499