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
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().
9 Comments
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
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
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.