如何在使用powermockito + cobertura时解决内存不足 / PermGen空间问题。
当我在测试用例上运行mvn cobertura:cobertura
时,会出现如下所示的异常:
Exception in thread "Thread-14" java.lang.OutOfMemoryError: PermGen space
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(Mock
ClassLoader.java:250)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(Mock
ClassLoader.java:194)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(D
eferSupportingClassLoader.java:71)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at net.sourceforge.cobertura.coveragedata.ProjectData.getOrCreateClassDa
ta(ProjectData.java:88)
at net.sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesOnP
rojectData(TouchCollector.java:109)
at net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectD
ata(ProjectData.java:272)
at net.sourceforge.cobertura.coveragedata.SaveTimer.run(SaveTimer.java:3
3)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-5" java.lang.OutOfMemoryError: PermGen space
at net.sourceforge.cobertura.coveragedata.ProjectData.addClassData(Proje
ctData.java:64)
at net.sourceforge.cobertura.coveragedata.ProjectData.getOrCreateClassDa
ta(ProjectData.java:89)
at net.sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesOnP
rojectData(TouchCollector.java:109)
at net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectD
ata(ProjectData.java:272)
at net.sourceforge.cobertura.coveragedata.SaveTimer.run(SaveTimer.java:3
3)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-22" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-15" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-6" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-11" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-10" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-7" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-13" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-3" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-12" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-1" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-2" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-24" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-0" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-4" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-8" java.lang.OutOfMemoryError: PermGen space
Exception in thread "Thread-25" java.lang.OutOfMemoryError: PermGen space
我尝试使用cobertura-mojo中提到的以下cobertura属性添加最大内存:
<cobertura.maxmem>1024m</cobertura.maxmem>
但它没有按预期工作,
我可以看到在Issue-227处有几个未解决的问题Issue-520,PowerMock repository,我尝试使用下面的注释添加忽略包但如果测试类的数量是更多。 @PowerMockIgnore({"com.package.*"})
的pom.xml
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx1624m</argLine>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<!--<argLine>-Xmx600m -XX:+HeapDumpOnOutOfMemoryError</argLine> -->
<forkCount>10</forkCount>
<reuseForks>true</reuseForks>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<quiet>true</quiet>
<cobertura.maxmem>1024m</cobertura.maxmem>
<instrumentation>
<maxmem>1024m</maxmem>
</instrumentation>
<!-- <argLine>-Xms512m -Xmx2048m -XX:MaxPermSize=1024m</argLine> -->
<!-- <argLine>-Xmx2048m</argLine> -->
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>instrument</goal>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
.......
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<!-- using mvn cobertura:cobertura to generate cobertura reports -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</reporting>
如果有任何解决方法,请与我们联系。
答案 0 :(得分:4)
请在pom.xml中将 <version>2.19.1</version>
添加到maven-surefire-plugin
,它应该可以正常工作。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>-Xmx1624m</argLine>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
答案 1 :(得分:2)
我想给你一些解决问题的建议。
"-XX:PermSize"
和"-XX:MaxPermSize"
。JVM default size of Perm Space is around "64MB"
你可以轻松跑
如果你有太多的类或大量的字符串,内存不足
在你的项目中。 要记住的重要一点是它没有
取决于-Xmx值,所以无论你的总堆大小有多大
可以在烫发空间运行OutOfMemory。好的想法是你可以指定
使用JVM选项"-XX:PermSize"
和{J}选项的永久代的大小
"-XX:MaxPermSize"
根据您的项目需求而定。"java.lang.OutOfMemoryError: PermGen"
的另一个原因是记忆
通过类加载器泄漏,它经常出现在WebServer中
和应用程序服务器,如tomcat,webshere,glassfish或weblogic。PermGen空间中OutOfMemoryError的另一个原因是任何线程 当您取消部署时,应用程序启动时不会退出 应用。
JVM_ARGS="-XX:PermSize=64M -XX:MaxPermSize=256m"
,其中
-XX:PermSize<size> - Set initial PermGen Size.
-XX:MaxPermSize<size> - Set the maximum PermGen Size.
答案 2 :(得分:2)
正如@Alexander所提到的,你遇到的问题是你的永久生成空间(存储静态对象,类信息等的内存区域)已经用完了。和-Xmx设置处理堆空间。
你想设置MaxPermSize,就像提到的@Alexander一样:
-XX:MaxPermSize=1024m
另一个选择是升级到JDK 8,它一起取消永久代,同时支持MetaSpace。
答案 3 :(得分:0)
只需在 pom.xml 中取消注释-XX:MaxPermSize=1024m
行。
答案 4 :(得分:0)
我在使用glassfish服务器的Web项目中遇到了类似的问题。这是因为我正在阅读整个表格,其中包含大约600000行数据,一次约有50列。我最后通过批量读取来解决问题,这不仅解决了java.lang.OutOfMemoryError: PermGen
空间错误,而且大大提高了性能。
可能是你穿着同样的鞋子,即使你将烫发尺寸增加到4096也是如此,你将在未来的某个时候仍然会遇到同样的问题。
您可以查看:JPA: what is the proper pattern for iterating over large result sets?,了解它是如何在jpa中实现的。