I am a little bit confused about the fact that we can just catch an OutOfMemoryException
using a try/catch block.
Given the following code:
Your OutOfMemory()
method creates data structure (the List
) that is local to the method's scope. While your thread of execution is inside the OutOfMemory
method, the current stack frame is considered the GC root for the List. Once your thread ends up in the catch block, the stack frame has been popped and the list has effectively become unreachable. Therefore the garbage collector determines that it can safely collect the list (which it does as you have observed in your memory graph).