I made a memory dump in my simplest console application and there are a bunch of exception instances around, what is going on? Let’s see in this blog post, why you see a few exception instances in your memory dump.
The simplest application
If you have an application like this:
Console.WriteLine("Hello, World!");
Build and attach the debugger and check the memory (and filter for the word ‘Exception’) you get the following picture:
Now why do we have a:
ExecutionEngineException
OutOfMemoryException
StackOverflowException
Is the Console.ReadLine
responsible here? Well no - but to rule it out just use the following code:
return;
So we are doing a NOOP and directly return, but still we are seeing those exceptions. Now why do we have them?
Unrecoverable
The simple reason is: In cases where you would throw such an exception, you can’t create the instance anymore. Think of it. If you are out of memory - how would you instantiate a new object in memory to throw it? Well, you can’t - therefore this instance has to be available.
Also StackOverflowException
- new
ing up an instance would lead to a new stackframe. As the stack is overflown already, you can’t put something on top.
Imagine your laundry room full to the top with, well, dirty laundry. And now you try to squeeze in another sock. What should happen here? Well it doesn’t work (maybe you create a black hole if you squeeze enough).
So yes - those exceptions have to be around because in the scenarios where you want to throw them, you can’t anymore!