Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JVM terminate itself after OutOfMemoryError [duplicate]

Tags:

java

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?

like image 263
vishal Kumar Avatar asked Apr 26 '12 06:04

vishal Kumar


People also ask

Can the JVM recover from an OutOfMemoryError without a restart?

In other words if an OOME is thrown in an application server (jboss/websphere/..) do I have to restart it? No you don't have to restart. But it is probably wise to, especially if you don't have a good / automated way of checking that the service is running correctly. The JVM will recover just fine.

What happens if JVM runs out of memory?

In a very extreme condition, you can run out of free memory while the JVM is running, and at the same time the JVM requires more memory for its internal operation (not the heap space) – then the JVM tells you it needed more memory, but could not get any, and terminates, or it will crash outright.

How do you deal with Outofmemory error in Java?

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.

Which exception is thrown when Java is out of memory?

One common indication of a memory leak is the java. lang. OutOfMemoryError exception. Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap.


2 Answers

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.

like image 128
Torben Avatar answered Nov 14 '22 13:11

Torben


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.

like image 30
Michael Borgwardt Avatar answered Nov 14 '22 15:11

Michael Borgwardt