Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

+9 votes
1 view
by (3.5k points)

I get this error message as I execute my JUnit tests:

java.lang.OutOfMemoryError: GC overhead limit exceeded

I know what an OutOfMemoryError is, but what does GC overhead limit mean? How can I solve this?

2 Answers

+9 votes
by (46k points)

OutOfMemoryError is a subclass of java.lang.VirtualMachineError; it’s thrown by the JVM when it faces a problem linked to using resources. More particularly, the error occurs when the JVM consumed too much time performing Garbage Collection and was only able to recover very little heap space.

According to Java docs, by default, the JVM is configured to throw this error if the Java method spends more than 98% of its time doing GC and when only less than 2% of the heap is collected in each run. In other words, this means that our application has consumed nearly all the available memory and the Garbage Collector has consumed too much time trying to clean it and failed regularly.

In this situation, users experience excessive slowness of the application. Some operations, which usually finished in milliseconds, take more time to finish. This is because the CPU is using its complete capacity for Garbage Collection and hence cannot perform any other tasks.

To fix this error turn this off with the command line option  -XX:-UseGCOverheadLimit

To know more about it click here.

+7 votes
by (108k points)

Your error is happening because, for some reason, the garbage collector is consuming an excessive amount of time. For example, 98% of CPU time is used on GC and less than 2% of the heap is recovered. This technically means your program stops making any progress and just executes the garbage collector. Thus to let you diagnose this error and prevent the applications from running for an extended period, JVM throws this error.

You can prevent this error, by following any of the below:

  • Increasing the heap size, for e.g: -Xmx1g
  • Enabling the concurrent low pause collector -XX:+UseConcMarkSweepGC
  • Reusing the existing objects(if possible) to save some memory

Hope this helps!!

Browse Categories