2

I have a question. As far as I know in .net out of memory exception can be raised in the following 3 scenarios

  1. Boxing
  2. Creating an Array
  3. Creating an Object

Normally it's raised when the managed heap is fragmented or does not have enough space to hold the object. My question if there is insufficient managed heap then where is the OutOfMemoryException object is created? Isn't it created on the managed heap? How does .Net framework makes sure that there is always enough space on the heap to create that OutOfMemoryException object?

Please help. I might be misunderstanding something.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
Abhik
  • 21
  • 1

3 Answers3

3

Heap in .NET framework is managed by .NET framework itself, so it keep necessary space for raising such exceptions. The same is true also for StackOverflow, for example.

In fact those both exception can not be potentially handled by client, or it is dangerous to handle them when you app in such condition (in case of OutOfMemory exception). There are still apps that handle it and try to "safe" client in some way, but the outcome of that is not guaranteed, you just do your best.

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

When your application starts, the runtime pre-creates these exceptions and stores them in a static list. If the exception needs to be thrown, it's retrieved from this list and thrown. This happens for OutOfMemoryException and StackOverflowException, and potentially a few others that are critical exceptions that might have problems being created later.

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
0

.Net Runtime reserves some space for itself in every process. this will help the framework to throw out critical exceptions like OutOfMemory.

You may also want to go through .Net memory management articles like the ones below

http://www.codeproject.com/Articles/483475/Memory-Limits-in-a-NET-Process

Is there a memory limit for a single .NET process

they have a lot of details and more references for you to pursue

Community
  • 1
  • 1
Dhawalk
  • 1,257
  • 13
  • 28