java.lang.OutOfMemoryError: GC Overhead limit exceeded occurs when garbage collector has reached its overhead limit. This means, it is running all the time, but is very slow in collecting objects.
java.lang.OutOfMemoryError: GC Overhead limit exceeded indicates that the garbage collector is running all the time and Java program is making very slow progress. After a GC (garbage collection), if the garbage collector is spending more than 98% of its time doing garbage collection and if it is recovering less than 2% of the heap and has been doing so far the last 5 (compile time constant) consecutive garbage collections, then a java.lang.OutOfMemoryError is thrown.
This error can be thrown from the Serial, Parallel or Concurrent, G1 collectors.
// GCOverheadLimitDemo.java
// JVM Parameters: -Xms10m -Xmx10m -XX:+UseParallelGC
import java.util.*;
public class GCOverheadLimitDemo {
public static void main(String[] args) {
int count = 0 ;
List <Employee> list = new ArrayList<> ();
while ( true ){
System.out.println( ++count);
list.add( new Employee(count, "Ranga "+count, count, count * 0.5f));
}
}
}
Compile and Run:
javac GCOverheadLimitDemo.java
java -Xms10m -Xmx10m -XX:+UseParallelGC GCOverheadLimitDemo
Output:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.lang.Integer.toString(Integer.java:401)
at java.lang.String.valueOf(String.java:3099)
at java.io.PrintStream.print(PrintStream.java:597)
at java.io.PrintStream.println(PrintStream.java:736)
at GCOverheadLimitDemo.gcOverheadLimit(GCOverheadLimitDemo.java:11)
at GCOverheadLimitDemo.main(GCOverheadLimitDemo.java:5)
Solution:
- Increase the Heap size using '-Xmx'.
java -Xmx1024m GCOverheadLimitDemo
- Fix the memory leak in the application.
- GC Overhead limit exceeded can be turned off with '-XX:-UseGCOverheadLimit'. In fact, this parameter can't solve the memory problem. It just delays the wrong information, and eventually java.lang.OutOfMemoryError: Java heap space appears.
java -Xmx1024m -XX:-UseGCOverheadLimit GCOverheadLimitDemo