一個運作了很久的項目,最近忽然報錯: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:1305)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:979)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:184)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:184)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:184)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:105)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:361)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1080)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:75)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:757)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
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:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.OutOfMemoryError: Java heap space
程式運作時記憶體分為堆(Stack)記憶體和棧(Heap)記憶體
憑借着初略的c語言知識來分析這個問題,了解如下:
1、棧記憶體的特性是快用快銷,不需要手工去釋放、管理;(網絡問題、檔案系統、消息循環、不合理的遞歸等操作也會導緻棧開銷過大,甚至溢出StackOverFlowError)
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
優化後如下
參考:Tomcat啟動記憶體設定【轉載】
轉載于:https://www.cnblogs.com/passedbylove/p/11002389.html