145

In C++ it's actually possible to throw an exception by value without allocating memory on a heap, so this situation makes sense. But in .NET framework OutOfMemoryException is a reference type, therefore it is allocated on a heap. How does .NET framework allocates memory for OutOfMemoryException when there is not enough memory to create a new object?

3
  • 6
    Excellent question. Maybe enough memory is reserved for just that situation. Commented Oct 30, 2015 at 15:49
  • 19
    Just to add to the other answers already here, bear in mind that OOM means that the block you requested can't be allocated. If you ask for 100Mb and the largest available block the runtime can find is only 99Mb, it will fail. But an OOM exception only needs a few bytes of memory. So just because your allocation failed it doesn't mean there is zero memory left. But of course it is likely that the runtime reserves some memory to cover itself in this situation Commented Oct 30, 2015 at 20:01
  • 4
    Your assumption about C++ is incorrect, by the way. Depending on the compiler, exceptions may well be allocated on the heap. The MS compiler does not, but in the Common C++ ABI, exceptions are allocated on the heap, except that there is a small preallocated emergency buffer that will be used instead if there's no space left on the heap. Commented Nov 1, 2015 at 0:45

2 Answers 2

164

It is preallocated by the runtime. If you explore the heap of any managed process you'll find an instance of that exception.

Here are the preallocated exceptions of a Hello World app:

0:003> !dumpheap -stat -type Exception
Statistics:
      MT    Count    TotalSize Class Name
735f2920        1           84 System.ExecutionEngineException
735f28dc        1           84 System.StackOverflowException
735f2898        1           84 System.OutOfMemoryException
735f2744        1           84 System.Exception
735f2964        2          168 System.Threading.ThreadAbortException
8
  • 4
    But the constructor of OutOfMemoryException is called. Commented Oct 30, 2015 at 15:52
  • 36
    The runtime doesn't have to play by the same rules as your code. Another example is that if you throw StackOverflowException you can catch it, but if the runtime throws that exception you can't catch it (by default). Commented Oct 30, 2015 at 15:59
  • 8
    Much of the underlying mechanisms of the CLR are actually written in "C" and "C++". So, it's entirely possible that the object is "new'd in place" or the memory is otherwise manipulated. Commented Oct 30, 2015 at 16:01
  • 2
    @hvd What's the side effect? Does OOM give a stack trace? I would have though the rest of the information is fairly static?
    – James
    Commented Oct 30, 2015 at 16:04
  • 7
    What if two exceptions of those with the same type are required because two threads throw them at the same time?
    – ASA
    Commented Oct 31, 2015 at 11:30
42

When an out-of-memory condition is encountered inside the runtime, it calls ThrowOutOfMemory. This calls Exception::GetOOMException, which constructs the object on the stack and then copies it to a statically-allocated global instance, which is then thrown.

This is not the managed Exception, though, it a C++ exception declared in ex.h. C++ Exceptions are converted to managed Exceptions in clrex.cpp, which contains code to specifically throw the preallocated managed OutOfMemoryException, which was originally allocated and constructed in appdomain.cpp.

Note: Some of these source files are large and may hang your browser for several seconds while it loads the syntax highlighting.

The call sites that Tim Schmelter linked in a comment on the other answer aren't related to the runtime running out of memory and being unable to construct an object.

Not the answer you're looking for? Browse other questions tagged or ask your own question.