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?
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.
And
OutOfMemoryErroris handled like any other exception:However there are two factors that are not really there in other exceptions:
OutOfMemoryErroris anErrorand not anException. This means that it’s very unlikely to be caught anywhere: You should not try to catch anErrorgenerally (with very few exceptions) and it’s not usually done, so the chances of it being handled are rather low.OutOfMemoryErrorhappens 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
mainmethod), 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.