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?
ベストアンサー1
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