[Solved-5 Solutions] Error java.lang.OutOfMemoryError: GC overhead limit exceeded
Error Description:
- We get this error message as we execute my JUnit tests:
java.lang.OutOfMemoryError:
GC overhead limit exceeded We know what anOutOfMemoryError
is, but what does GC overhead limit mean? How can we solve this?
Solution 1:
- This message means that for some reason the garbage collector is taking an excessive amount of time (by default 98% of all CPU time of the process) and recovers very little memory in each run (by default 2% of the heap).
- This effectively means that our program stops doing any progress and is busy running only the garbage collection at all time.
- To prevent our application from soaking up CPU time without getting anything done, the JVM throws this
Error
so that we have a chance of diagnosing the problem.
Solution 2:
- This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small.
- We can turn this off with the command line option
-XX:-UseGCOverheadLimit
Solution 3:
- If we are sure there are no
memory leaks
in our program, we can try to: - Increase the heap size, for example
-Xmx1g.
- Enable the concurrent low pause collector
-XX:+UseConcMarkSweepGC.
- Reuse existing objects when possible to save some memory.
Solution 4:
- We can increase the heap size a little by setting this option in
Run → Run Configurations → Arguments → VM arguments
- Xms - for minimum limit
- Xmx - for maximum limit
Solution 5:
- If we still get this error after increasing heap memory, we can use memory profiling tools like MAT ( Memory analyzer tool), Visual VM etc and fix memory leaks.
- We can upgrade JDK version to latest version ( 1.8.x) or at least 1.7.x and use G1GC algorithm.
- Apart from setting heap memory with
-Xms1g -Xmx2g
, try