Mairbek Khadikov
Mairbek Khadikov

Reputation: 8089

OutOfMemoryError with free memory available

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

Answers (2)

pap
pap

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

Peter Lawrey
Peter Lawrey

Reputation: 533442

You will get an OutOfMemoryError when memory fails to allocate.

This can happen when

  • you have run out of memory.
  • your GC turning parameters prevent it from using all the memory. e.g. setting minimum sizes for generations which are not full.
  • you allocate a large block of memory. Immediately after this fails you still have free memory, just not enough to allocate the large block.
  • you get an OutOfMemoryError which is not for the heap, e.g. PermGen or direct memory or virtual memory (for threads etc)

Upvotes: 5

Related Questions