Cu2S
Cu2S

Reputation: 686

What happens when there is no memory to construct an OutOfMemoryException in C#?

I found that System.Exception requires many memory allocation(many Strings, even IDictionary). What happens when there is no enough memory to construct an OutOfMemoryException?

After searching, I found a similar question: What happens when there's insufficient memory to throw an OutOfMemoryError? Does CLR behave like that?

Upvotes: 1

Views: 140

Answers (2)

Dudi Keleti
Dudi Keleti

Reputation: 2984

As you (Cu2s) mentioned, the framework pre allocate some objects. The OutOfMemory is not the only one. StackOverflow of course is also needed.

See source here

And also this blog post

You can see it yourself in WinDbg or some other tool.

For example when I'm running this code:

static void Main()
{
    Console.Read();
}

I see in the heap the following:

enter image description here

Upvotes: 3

Cu2S
Cu2S

Reputation: 686

I found the source code of coreclr. It seems that the CLR preallocates some exception classes.

Upvotes: 1

Related Questions