This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Error in Java. Lang. OutOfMemoryError: beyond the GC overhead restrictions

I received the following error message while performing the JUnit test:

java.lang.OutOfMemoryError: GC overhead limit exceeded

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

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

This message indicates that, for some reason, the garbage collector is taking up too much time (98% of all CPU time for the process by default) and recovers too little memory each run (2% of the heap by default).

This essentially means that your program stops making any progress and is busy just running garbage collection.

To prevent your application from wasting CPU time without doing anything, the JVM throws this Error so that you have a chance to diagnose the problem.

The rare case I’ve seen this happen is when some code creates a lot of temporary objects and a lot of weak reference objects in an already very memory-constrained environment.

Check out the Java GC Tuning Guide, which is available for various Java versions and contains sections on this particular issue:

The Java 11 Tuning guide provides special sections for different garbage collectors for excessive GCS:

For the parallel collector there is no mention of this particular error case for the garbage-first (G1) collector for the concurrent mark scan (CMS) collector.Copy the code

The Java 8 Tuning Guide and its “Too Much GC” section

Answer:

If the current heap is insufficient, increase the heap size. If this error still occurs after increasing heap memory, use a memory analysis tool such as MAT (Memory analyzer tool), Visual VM, etc., and fix the memory leak. Upgrade the JDK version to the latest version (1.8.x) or at least 1.7.x and use the G1GC algorithm. The throughput target for the G1 GC is 90% application time and 10% garbage collection time

Try Xms1g -XMx2G instead of using -set heap memory

-XX:+UseG1GC -XX:G1HeapRegionSize=n -XX:MaxGCPauseMillis=m  
-XX:ParallelGCThreads=n -XX:ConcGCThreads=n
Copy the code

The article translated from am2dgbqfb6mk75jcyanzabc67y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 1…

Author suggestion: in addition to the above method, I suggest using JStack to view the stack information of the thread, view the status of each thread, for abnormal thread status investigation, such as: deadlock, second: JMAP view heap capacity sort third: JPS view Java process, top view system resources, fourth: use arthas investigation


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️