Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to get an OutOfMemoryException in C#?

Just curious as to how I can get this error the easiest way.

Once I was trying to create a file navigator and I was creating Image thumbnails; that turned out awful.

like image 922
Sergio Tapia Avatar asked Jul 10 '10 23:07

Sergio Tapia


People also ask

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.


2 Answers

void Foo() {    throw new OutOfMemoryException(); } 

:)))

like image 166
Nagg Avatar answered Oct 13 '22 20:10

Nagg


Create a very, very large string. Probably:

string s = new string('a', int.MaxValue); 

will be enough.

If not, you can concat it to build even bigger string.

string ss = string.Concat(s, s); 
like image 35
Arseni Mourzenko Avatar answered Oct 13 '22 18:10

Arseni Mourzenko