Error java.lang.OutOfMemoryError: GC overhead limit exceeded

The issue

In our project when we were running a batch operation in polopoly CMS we ran into this issue. Following is the issue:

Error java.lang.OutOfMemoryError: GC overhead limit exceeded

What is this issue about?

The error “GC overhead limit exceeded” points that the garbage collector is running most of the time and Java program is making very slow progress.

After a garbage collection, if the Java process is spending more than approximately 98% of its time doing garbage collection. If java process is recovering less than 2% of the heap. If the same situation happens last 5 consecutive garbage collections then a java.lang.OutOfMemoryError is thrown.

The reason

The batch operation we were running was creating a lot of objects and the Java heap was getting filled up. The error thrown because the amount of live data barely fits into the Java heap having little free space for new allocations.

The solution

In our case we had to increase the Java heap size and that solved the issue with the batch operation. We can also turn off the overhead limit to the issue. So we have two approaches for the issue:

  • Increase the heap size.
  • The java.lang.OutOfMemoryError exception for GC Overhead limit exceeded can be turned off with the command line flag -XX:-UseGCOverheadLimit.

In both the solution appraoches the issue might not be sovled permanently if the code has memory leaks. It is always better to detect the code that is creating a lot of object references and eating up the Java heap.