11

What would be the behavior of a Java program on getting OutOfMemoryError. Is there any defined behavior? Will the process crash or would it go into wait/sleep state?

Update: if I am not handling it in my code?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Umer Hayat
  • 1,993
  • 5
  • 31
  • 58

3 Answers3

19

And OutOfMemoryError is handled like any other exception:

  • If it is caught, then nothing more happens.
  • If it is not caught, then either the threads or the threads groups uncaught exception handler handles it. This pretty much always leads to the thread being stopped.

However there are two factors that are not really there in other exceptions:

  • OutOfMemoryError is an Error and not an Exception. This means that it's very unlikely to be caught anywhere: You should not try to catch an Error generally (with very few exceptions) and it's not usually done, so the chances of it being handled are rather low.
  • When an OutOfMemoryError happens and no object become eligible for GC because of that, then you'll still have little memory left and chances are that you'll run into the exact same problem again later on.

And if the thread this happens to is the only non-daemon thread (often, but not necessarily, that's the main thread, that executes the main method), then that thread getting killed results in the whole JVM shutting down (which is often perceived as "a crash").

So the tl;dr is: It will probably kill the thread, and if the memory-issue is not solved, then this can happen to more and more threads.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • +1 for "the only non-daemon thread", although I guess the main thread is likely to be doomed to an `OutOfMemoryError` shortly afterwards in any case. – Raedwald Oct 03 '11 at 13:30
  • 1
    @Raedwald: that can easily happen, but need not be the case: sometimes the unrolling of the stack makes enough object eligible for GC that the out of memory situation is solved by that alone. For example if a big import of data fails due to an OOM-Error, it can happen that the rest of the application continues unaffectedly (but you could have bad luck and get the OOM-Error in another part first). – Joachim Sauer Oct 03 '11 at 13:32
  • Good point. For a multi-threaded server, you actually want that behaviour, so a big request dooms itself rather than killing the server. – Raedwald Oct 03 '11 at 13:46
1

You can't determine the state of program when OutOfMemoryError occurs. If you are not catching Throwable then your program will terminate with the stacktrace. Even if you are catching Throwable, you should call System.exit since there is no point in recovering from it. "Error" is generally thrown by JVM, as oppose to Exception, which is application/programmer specific.

Usman Saleem
  • 1,665
  • 9
  • 18
1

OutOfMemoryError should be considered unrecoverable and the behavior of the JVM after such an error has been raised is undefined, so there is no point in expending effort to handle it. Any operations done after this exception is thrown by the JVM will have undefined behavior. They may execute, but more likely they will just cause another error to be thrown.

pap
  • 27,064
  • 6
  • 41
  • 46