一个运行了很久的项目,最近忽然报错:OOM( java.lang.OutOfMemoryError: Java heap space),异常如下
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.OutOfMemoryError: Java heap space
at org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.OutOfMemoryError: Java heap space
程序运行时内存分为堆(Stack)内存和栈(Heap)内存
凭借着初略的c语言知识来分析这个问题,理解如下:
1、栈内存的特性是快用快销,不需要手工去释放、管理;(网络问题、文件系统、消息循环、不合理的递归等操作也会导致栈开销过大,甚至溢出*Error)
2、堆内存的特性是谁用谁销(c语言中说法:谁申请谁释放)
那么根据异常来甄别一下,一定是程序不合理的使用使用内存导致整个问题,而OOM异常一般是硬件级别的异常。
查了一下服务器,32G的内存,不应该出现这种问题的。
由于tomcat是通过服务方式运行的,more分配的内存如下
Initial memory pool(初始化堆内存) 128M 对应-Xms参数
Maximun memory pool(最大堆内存) 256M 对应-Xmx参数
由于端午节期间放假,增量数据累计到33万,没有人去处理,程序在自动运行的时候尝试申请堆内存,触顶了(256M),所以报这个错误。
解决方法:增大初始化堆内存,最大堆内存
Initial memory pool改为1024M
Maximun memory pool 10240M
另外在Java Option里面添加以下参数
-XX:PermSize=256M //初始化类加载内存池大小
-XX:MaxPermSize=256M //最大类加载内存池大小
-XX:MaxNewSize=256M
优化后如下