错误解决java.lang.OutOfMemoryError: GC overhead limit exceeded

今天做一个秒杀项目的时候,遇到了下面的错误:

java.lang.OutOfMemoryError: GC overhead limit exceeded

三月 23, 2019 9:08:47 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [/YF_MS_WEB] threw exception [Handler processing failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause
java.lang.OutOfMemoryError: GC overhead limit exceeded

三月 23, 2019 9:08:53 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [/YF_MS_WEB] threw exception [Handler processing failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause
java.lang.OutOfMemoryError: GC overhead limit exceeded

三月 23, 2019 9:08:59 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [/YF_MS_WEB] threw exception [Handler processing failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause
java.lang.OutOfMemoryError: GC overhead limit exceeded

三月 23, 2019 9:09:08 上午 org.apache.tomcat.util.net.NioEndpoint$SocketProcessor doRun
严重: 
java.lang.OutOfMemoryError: GC overhead limit exceeded

三月 23, 2019 9:09:11 上午 org.apache.tomcat.util.net.NioEndpoint$Poller run
严重: 
java.lang.OutOfMemoryError: GC overhead limit exceeded

三月 23, 2019 9:09:43 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [/YF_MS_WEB] threw exception [Handler processing failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause
java.lang.OutOfMemoryError: GC overhead limit exceeded

三月 23, 2019 9:11:28 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [/YF_MS_WEB] threw exception [Handler processing failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause
java.lang.OutOfMemoryError: GC overhead limit exceeded

 GC overhead limt exceed检查是Hotspot VM 1.6定义的一个策略,通过统计GC时间来预测是否要OOM了,提前抛出异常,防止OOM发生。Sun 官方对此的定义是:“并行/并发回收器在GC回收时间过长时会抛出OutOfMemroyError。过长的定义是,超过98%的时间用来做GC并且回收了不到2%的堆内存。用来避免内存过小造成应用不能正常工作。“

一般是应用程序在有限的内存上创建了大量的临时对象或者弱引用对象,从而导致该异常。虽然加大内存可以暂时解决这个问题,但是还是强烈建议去优化代码,后者更加有效。

解决方法:

1.你可以关闭JVM这个默认的策略:

java -XX:-UseGCOverheadLimit JavaApp

2.加大Heap Size:

java -Xmx512m JavaApp

注意:在修改Tomcat的catalina.bat(*.sh)中的内容时,网上有很多都是说直接修改JAVA_OPTS,按照Apache官方的说法是:
Note: Do not use JAVA_OPTS to specify memory limits. You do not need much memory for a small process that is used to stop Tomcat. Those settings belong to CATALINA_OPTS.(注意:不要使用JAVA_OPTS指定内存限制。对于用于停止Tomcat的小进程,不需要太多内存。这些设置属于CATALINA_OPTS。)

原文地址:https://www.cnblogs.com/yangxianyang/p/13675611.html