运行环境: STS(eclipse -Xms40m -Xmx768m -XX:MaxPermSize=1024m)+win7(8G内存)
运行单元测试,报错: java.lang.OutOfMemoryError: Java heap space
相信这个错误比较常见.我这边出现的问题是,即使设置了eclipse的参数还是不行.google之, 查出原因如下.
junit不使用eclipse自身的jvm参数. 囧...
知道原因就好改了:
1 单独运行junit,在jvm参数里填加:-Xms128m -Xmx1024m (注:具体参数根据自身机器和需求来确定)
2 ant中单元测试: <junit maxmemory="1024m" printsummary="yes"
haltonfailure="no" fork="yes" forkmode="perBatch">
...
</junit>
(关键就是增加红色字体部分)
可以使用以下代码来查看内存状况:
public void testMemoryUsage() { Runtime runtime = Runtime.getRuntime(); NumberFormat format = NumberFormat.getInstance(); long mb = 1024*1024; StringBuilder sb = new StringBuilder(); long maxMemory = runtime.maxMemory(); long allocatedMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); long usedMemory = allocatedMemory - freeMemory; sb.append("free memory: " + format.format(freeMemory / mb) + "mb,"); sb.append(" allocated memory: " + format.format(allocatedMemory / mb) +"mb,"); sb.append(" max memory: " + format.format(maxMemory / mb) + "mb,"); sb.append(" used memory: " + format.format(usedMemory / mb) +"mb,"); sb.append(" total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / mb) +" mb"); System.out.println("memory usage:-"+sb); System.gc(); }
相关推荐
搜集整理关于java错误处理:java.lang.OutOfMemoryError: Java heap space java.lang.OutOfMemoryError: Java heap space 资料整理
java.lang.OutOfMemoryError: Java heap space 解决方法
Caused by: java.lang.OutOfMemoryError: PermGen space解决方案, avax.servlet.ServletException: java.lang.OutOfMemoryError: PermGen space at org.apache.jasper.servlet.JspServlet.service(JspServlet.java...
java.lang.OutOfMemoryError: PermGen space 解决方案
报错 java.lang.OutOfMemoryError: PermGen space 报错 java.lang.OutOfMemoryError: Java heap 启动报错java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler JAVA_OPTS="-server -Xms800m ...
Myeclipse下java.lang.OutOfMemoryError Java heap space的解决
java解决nested exception is java.lang.OutOfMemoryError Java heap space 解决OOM
NULL 博文链接:https://geyubin.iteye.com/blog/779330
记录java.lang.OutOfMemoryError:Java heap space的情况 文章内容: 一.问题描述+原因分析+解决方案 二.JVM调优说明 三.Tomcat添加到jvisualvm监控
java.lang.OutOfMemoryError: PermGen space及其解决方法,解决项目太大tomcat内存溢出
编译时出现java.lang.OutOfMemoryError Java heap space异常.
java.lang.OutOfMemoryError处理错误
java.lang.OutOfMemoryError: Java heap space 解决方法
java[1].lang.OutOfMemoryError_Java_heap_space错误及处理办法java[1].lang.OutOfMemoryError_Java_heap_space错误及处理办法java[1].lang.OutOfMemoryError_Java_heap_space错误及处理办法
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
Tomcat给我的java.lang.OutOfMemoryError: PermGenTomcat给我的java.lang.OutOfMemoryError: PermGenTomcat给我的java.lang.OutOfMemoryError: PermGenTomcat给我的java.lang.OutOfMemoryError: PermGenTomcat给我的...
Eclipse中通过Tomcat运行J2EE项目java.lang.OutOfMemoryError PermGen space的解决方案
java.lang.OutOfMemoryError: PermGen space
内存不足OOM java.lang.OutOfMemoryError.