Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it OK to catch an OutOfMemoryException and how to handle it?

Tags:

c#

.net

Yesterday I took part in a discussion on SO devoted to OutOfMemoryException and the pros and cons of handling it (C# try {} catch {}).

My pros for handling it were:

  • The fact that OutOfMemoryException was thrown doesn't generally mean that the state of a program was corrupted;
  • According to documentation "the following Microsoft intermediate (MSIL) instructions throw OutOfMemoryException: box, newarr, newobj" which just (usually) means that the CLR attempted to find a block of memory of a given size and was unable to do that; it does not mean that no single byte left at our disposition;

But not all people were agree with that and speculated about unknown program state after this exception and an inability to do something useful since it will require even more memory.

Therefore my question is: what are the serious reasons not to handle OutOfMemoryException and immediately give up when it occurs?

Edited: Do you think that OOME is as fatal as ExecutionEngineException?

like image 858
Igor Korkhov Avatar asked Jan 22 '10 12:01

Igor Korkhov


People also ask

How do you deal with OutOfMemoryException?

1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

What causes OutOfMemoryException?

When data structures or data sets that reside in memory become so large that the common language runtime is unable to allocate enough contiguous memory for them, an OutOfMemoryException exception results.

What is exception of type system OutOfMemoryException was thrown?

DeployAndExecuteCommand Exception of type 'System. OutOfMemoryException' was thrown. Cause: The reason this error is occurring is because the computer in use is running out of physical RAM memory and is unable to allocate the necessary RAM to complete the task.


1 Answers

IMO, since you can't predict what you can/can't do after an OOM (so you can't reliably process the error), or what else did/didn't happen when unrolling the stack to where you are (so the BCL hasn't reliably processed the error), your app must now be assumed to be in a corrupt state. If you "fix" your code by handling this exception you are burying your head in the sand.

I could be wrong here, but to me this message says BIG TROUBLE. The correct fix is to figure out why you have chomped though memory, and address that (for example, have you got a leak? could you switch to a streaming API?). Even switching to x64 isn't a magic bullet here; arrays (and hence lists) are still size limited; and the increased reference size means you can fix numerically fewer references in the 2GB object cap.

If you need to chance processing some data, and are happy for it to fail: launch a second process (an AppDomain isn't good enough). If it blows up, tear down the process. Problem solved, and your original process/AppDomain is safe.

like image 86
Marc Gravell Avatar answered Oct 18 '22 04:10

Marc Gravell