开发者

java.lang.OutOfMemoryError: Java heap space on JSP Pages

开发者 https://www.devze.com 2023-04-01 07:59 出处:网络
I was getting out of memory error on a spring framework application. I disabled a few filters (like security filter). But now the error is coming on JSP Pages. My JSP pages are quite heavy and use Til

I was getting out of memory error on a spring framework application. I disabled a few filters (like security filter). But now the error is coming on JSP Pages. My JSP pages are quite heavy and use Tile Framework.

Unfortunately the hosting I have is providing only 32 MB of heap space and I could not increase that.

I have added a few lines in my layout page:

final long  MEGABYTE = 1024L * 1024L;

// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();
out.println("Total Memory (MB): "+(heapSize / MEGABYTE ));

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.
// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
out.println("<br/>Heap Max Size (MB): "+(heapMaxSize / MEGABYTE ));

// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();
out.println("<br/>Heap Free Size (MB): "+(heapFreeSize / MEGABYTE ));

The above prints following output at output (HTML Page)

Total Memory (MB): 29 
Heap Max Size (MB): 29 
Heap Free Size (MB): 3 

Here is the stack trace:

javax.servlet.jsp.JspException: ServletException in '/jsp/fragments/account/newAccount.jsp': java.lang.OutOfMemoryError: Java heap space
at org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(InsertTag.java:921)
at org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:460)
at org.apache.jsp.jsp.common.tiles.layout_jsp._jspx_meth_tiles_005finsert_005f3(layout_jsp.java:818)
at org.apache.jsp.jsp.common.tiles.layout_jsp._jspService(layout_jsp.java:428)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.ja开发者_如何转开发va:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)


I have is providing only 32 MB of heap space and I could not increase that..

Sorry, but then that's the end of story. You have 2 options:

  • Rewrite your views to be more memory efficient. Perhaps you need to fall back to plain vanilla JSP/Servlet without any additional frameworks which each of course needs their own memory share.

  • Give the server more memory. Upgrade your existing hosting account or migrate to a different host.


Profiling your application could be one option which will give you insight into what actually is causing an OutOfMemory error and if it is really possible to optimize the code without increasing the physical memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消