0

I always thought that an OutOfMemoryError restarts the JVM.
But I am seeing a behavior where an OutOfMemoryError occurs this is not caught from the code (actually I don't even know if this is possible)
and the JVM continues (a core dump is produced though).
Can anyone help me understand this behavior?

5 Answers 5

3

OutOfMemoryError is an exception just like any other. It certainly doesn't restart the JVM. It may possibly cause the thread in which it occurs to end, but that happens only if it is not caught anywhere. What you probably see is not a core dump, but merely the stack trace of the exception that was caught and its stack trace explicitly printed using e.printStackTrace().

Sign up to request clarification or add additional context in comments.

9 Comments

So when I occusionally see a JVM restart, what does that mean?
That means that a JVM launcher is in effect that explicitly takes care of the restarting. It has nothing to do with Java itself.
So you are saying that even though the dump says that the heap is exausted, the JVM still does not terminate by itself?What can it do after this condition?
The heap is not required for execution to continue. As long as you don't allocate anything, you can go on. In practice, as soon as the OOME happens, the call stack unwinds and many objects become unreachable. An ensuing GC run reclaims the memory. How much this helps is of course not answerable in general.
Then what does it mean when the dump reports that the heap is exausted?
|
2

An OutOfMemoryError reports to one thread that its memory allocation failed.

int count = 0;
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
    try {
        long[] longs = new long[Integer.MAX_VALUE];
    } catch (OutOfMemoryError e) {
        count++;
    }
}
long time = System.currentTimeMillis() - start;
System.out.printf("Got %,d OutOfMemoryErrors in %.1f seconds%n", count, time/1e3);

prints

Got 10,000,000 OutOfMemoryErrors in 3.9 seconds

Comments

1

OutOfMemory is just an Error that is thrown when an object could not be instanciated due to a lack of available memory. This does not stop the JVM, just like no other exception or error does.

Comments

1

No Throwable ever restarts a JVM. The OutOfMemoryError is like every other runtime exception or error: it propagates the call stack until it's caught. And if it isn't, then the thread's UncaughtExceptionHandler handles this exception, usually by terminating the thread and printing the stack trace of the exception.

6 Comments

So when I occusionally see a JVM restart, what does that mean?
Probably that you have some script or tool detecting that the JVM doesn't run anymore, and which restarts it. A JVM doesn't restart by itself. It could crash completely, but not restart.
So you are saying that even though the dump says that the heap is exausted, the JVM still does not terminate by itself?What can it do after this condition?
An OutOfMemoryError doesn't say that the heap is exhausted. It says that you asked for some memory, and that it wasn't possible to allocate it. If you have some thread asking for 1 GB of memory, and this thread dies because there is only 980 MBs available, other threads could continue running without any problem.
But the overall report of the dump says that there is no heap left
|
1

OutOfMemoryError or any exception never restart the JVM.

I am seeing a behavior where an OutOfMemoryError occurs this is not caught from the code

----> OutOfMemoryError inherits java.lang.Error. Errors describe environmental problems which may be rare or difficult to recover from such as running out of computer memory. You are not expected to handle Error class objects but you should handle them with Throwable. These should be regarded as environmental flaws.

Have a look on Exception Hierarchy.

1 Comment

A well-designed application is very much required to handle any Throwables, including OOME's. Would you like to see your server crash just because a single request OOME'd or SOE'd? I wouldn't, either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.