From the MSDN Page for OutOfMemoryException here is the major cause of an OutOfMemoryException
:
The common language runtime cannot allocate enough contiguous memory to successfully perform an operation. This exception can be thrown by any property assignment or method call that requires a memory allocation. For more information on the cause of the OutOfMemoryException exception, see "Out of Memory" Does Not Refer to Physical Memory.
This type of OutOfMemoryException exception represents a catastrophic failure. If you choose to handle the exception, you should include a catch block that calls the Environment.FailFast method to terminate your app and add an entry to the system event log...
The key point is that it represents a catastrophic failure and you should exit your app.
Calling GC.Collect()
is no longer an option once this type of exception occurs.