7

After a OutOfMemoryError does JVM terminates itself? If not then why? Will it try to get the resources back? Or is there any other reason?

vishal Kumar
  • 95
  • 3
  • 8

5 Answers5

15

An OutOfMemoryError DOES NOT TERMINATE JVM.

If it is uncaught, it terminates the THREAD from which the error was initiated. Other threads keep running just fine, unless of course they cause OutOfMemoryErrors too.

Only after all threads have been terminated or all remaining threads are daemon threads, the JVM is terminated.

It does not terminate the JVM because it does not have to. Terminating the JVM is a very extreme operation and it is not performed lightly.

It will not try to get any resources back, because there is nothing to be retrieved. The reason OOME is thrown is just that: JVM can not acquire a resource because all resources are taken. It has already done everything else it can.

One must remember that OOME is not necessarily thrown in the thread that consumes the most memory. A thread can consume all memory and yield processing to another thread that tries to allocate "just one byte". Thas of course fails and the thread that tried to allocate the one byte gets interrupted by an OOME. That is the reason why recovering from an OOME is nearly impossible.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
Torben
  • 3,805
  • 26
  • 31
4

The JVM terminates iff (like with any other Exception or Error) it's not caught anywhere and there are no other threads that are daemon threads.

It does not terminate immediately because the OutOfMemoryError can be caught and the application can try to do some error handling, from just logging the error up to dismissing that computation and otherwise continuing normally.

The latter is considered risky when an Error occurs but often possible without any problems, since a lot of objects can go out of scope between the point where the OutOfMemoryError is thrown and where it's caught, which can then be freed up by garbage collection to give the program new memory to work with. If the OutOfMemoryError occurred because one particular computation required more memory than is available and the application does something else afterwards, that's fine.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

That depends on whether you handle that error or not. If you don't then the application and the current thread with it will be terminated. If this thread happens to be the last running thread (in most cases the main thread of the current application) the JVM will exit as well (although it might still do some logging, create a memory dump etc. before exiting).

If you handle the error you normally just try to do a bit of cleanup, before stopping the JVM. In most cases it's a bad idea to try and recover from an OutOfMemoryError. For more details, see here: Can the JVM recover from an OutOfMemoryError without a restart

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
0

It doesn't terminates itself, it just throws an error saying that it cannot create any more objects are the memory is not available, though you can continue the code by handling the error, but you will have to survive with the currently created objects only, or may be release or nullify the ones that you don't need any more .

PS: but its not a recommended way to handle such errors and proceed

Akash Yadav
  • 2,411
  • 20
  • 32
0

In general a JVM only terminates when the last non-daemon thread exits. Throwing an OutOfMemoryError or any other kind of Error or Exception can cause the last non-daemon thread to exist if it is not caught.

However, if the error is caught and not rethrown or there is another non-daemon thread running, the JVM does not exit. i.e. there is nothing special about any particular error in causing a program to exit.

There are two qualifications

  • System.exit() will cause a JVM to start shutting down and cannot be caught. Having multiple threads makes no difference.
  • ThreadDeath error is special in that it doesn't get printed by default. Otherwise it behaves like any other error.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130