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?
After a OutOfMemoryError does JVM terminates itself? If not then why? Will it try
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.