java.lang.OutOfMemoryError: Java heap space 堆空间溢出的错误解决 (临时)

#Tomcat报错日志信息:

Caused by: java.lang.OutOfMemoryError: Java heap space
javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
	at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:298)
	at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
	at apl.gsc.otleave.action.MyServlet.process(MyServlet.java:31)
	at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at apl.gsc.otleave.action.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:49)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at apl.gsc.otleave.util.CloseHibernateSession.doFilter(CloseHibernateSession.java:42)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	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:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:610)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:503)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.OutOfMemoryError: Java heap space
Exception in thread "http-8818-15" java.lang.OutOfMemoryError: Java heap space

#原因分析:

使用Java程序从数据库中查询大量的数据时出现异常:java.lang.OutOfMemoryError: Java heap space。在JVM中如果98%的时间是用于GC且可用的 Heap size 不足2%的时候将抛出此异常信息。JVM堆的设置是指java程序运行过程中JVM可以调配使用的内存空间的设置.

如果Heap Size设置偏小,除了这些异常信息外,还会发现程序的响应速度变慢了。GC占用了更多的时间,而应用分配到的执行时间较少。

Heap Size 最大不要超过可用物理内存的80%,一般的要将-Xms和-Xmx选项设置为相同,而-Xmn为1/4的-Xmx值。Heap size的 -Xms -Xmn 设置不要超出物理内存的大小。否则会提示“Error occurred during initialization of VM Could not reserve enough space for object heap”。

 

#解决方法

一.如果有,就配置 Tomcat 6.0\bin\catalina.bat 文件

set JAVA_OPTS=-Xms256m -Xmx1024m

二.tomcat6 可能修改Tomcat 6.0\bin\Tomcat6w.exe

java.lang.OutOfMemoryError: Java heap space 堆空间溢出的错误解决 (临时)_第1张图片

 

可以登录tomcat管理页面看到JVM的信息 http://IPaddress:port/manager/status

调整前:

java.lang.OutOfMemoryError: Java heap space 堆空间溢出的错误解决 (临时)_第2张图片

调整后:

 

#拓展知识

内存溢出java.lang.OutOfMemoryErrory后面一般会跟上内存溢出的区域
PermGen space(方法区), heap space(堆内存)
如果是PermGen space方法区内存溢出,可尝试加大MaxPermSize
如果是heap space 堆内存溢出,可尝试修改Xmx

-Xms
设置JVM初始化堆内存大小

-Xmx
设置JVM最大的堆内存大小

 

#这是临时解决办法,实际还是得找到根本原因 https://zazalu.space/2019/09/17/java-memory-error-solution-Theoretically/

你可能感兴趣的:(Java)