Help my memory dump always shows me some exceptions!

5/23/2024

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:

Debugger.png

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 - newing 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!

Span / Memory / ReadOnlySequence in C#

There are many different memory types used in modern C# programs. The more common ones are Span<T> and Memory<T>. Occasionally there is also ReadOnlySequence<T>. What do these types do?

Throwing exceptions - Why is my stack trace lost?

You might have read, that re-throwing an exception like this: throw exc; is considered bad practice and you should just do this: throw; instead.

But why is it like that?

Memory is complicated

This is a small story about how memory operates in your .NET application. Well not only .NET but how memory does or does not get allocated.

We will see how a 1 Gigabyte big array is only a few megabytes big to some extend. Furthermore I will discuss working set and committed memory.

An error has occurred. This application may no longer respond until reloaded. Reload x