Reputation: 8089
I have an application that executes some heavy processing in background using java thread pools.
Thread pool are configured to log exceptions occurred during the execution
public enum ExceptionLogger implements Thread.UncaughtExceptionHandler {
INSTANCE;
private static final Logger log = LoggerFactory.getLogger(ExceptionLogger.class);
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("Exception occurred at thread " + t.getName() + "\n", e);
}
}
In the logs of my application I'm randomly getting java.lang.OutOfMemoryError: Java heap space
but if I log memory usage using Runtime.getRuntime().freeMemory()
I can see plenty of free heap space available.
In addition application continues working after I get this exception.
Is there any other reason for getting OutOfMemoryError
with this message?
Upvotes: 5
Views: 1392
Reputation: 27614
In addition to Peter Lawrey's excellent answer, you also have the "GC overhead limit reached" error.
This occurs when the JVM does not have sufficient processing time to garbage collect or GC takes too much of the total available processing power. Happens predominantly when you have huge amounts of very short-lived objects combined with CPU hungry application and not enough average free heap (max heap size - long lived objects).
Solutions to this vary, but often means that you have to restrict the CPU consumption (in your case, number of threads) and/or increase heap size.
Upvotes: 2
Reputation: 533442
You will get an OutOfMemoryError when memory fails to allocate.
This can happen when
Upvotes: 5