简体   繁体   English

在PowerMockito中运行Cobertura并抛出java.lang.OutOfMemoryError:PermGen空间

[英]Running Cobertura with PowerMockito throwing java.lang.OutOfMemoryError: PermGen space

How to solve the Out of memory / PermGen space issue while using + . 如何使用 + 解决内存不足 / PermGen空间问题。

When I ran mvn cobertura:cobertura on test cases it's getting exception like below: 当我在测试用例上运行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

I tried by adding max memory by using below cobertura property as mentioned in cobertura-mojo : 我尝试通过使用下面的cobertura属性添加最大内存,如cobertura-mojo中所述

<cobertura.maxmem>1024m</cobertura.maxmem>

but it's not working as expected, I can see there are few open issues Issue-227 , Issue-520 at PowerMock repository , 但是它没有按预期工作,我可以看到在PowerMock存储库中几乎没有未解决的问题Issue-227Issue-520
I try to add ignore packages by using below annotation but it's not working if number of test classes are more. 我尝试通过使用以下注释来添加忽略包,但是如果测试类的数量更多,它将无法正常工作。 @PowerMockIgnore({"com.package.*"})

pom.xml 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>

Please let me know if there is any workaround for this. 请让我知道是否有任何解决方法。

Please add <version>2.19.1</version> to maven-surefire-plugin in pom.xml, it should work fine. 请在pom.xml的maven-surefire-plugin中添加<version>2.19.1</version> ,它应该可以正常工作。

            <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>

I want to give you some suggestions to solve the issue. 我想给您一些解决问题的建议。

  1. First remove all memory space from your pom.xml. 首先,从pom.xml中删除所有内存空间。
  2. Secondly, increase your JVM options "-XX:PermSize" and "-XX:MaxPermSize" based on your project need. 其次,根据项目需要增加JVM选项"-XX:PermSize""-XX:MaxPermSize"

Root Cause Analysis: 根本原因分析:

  • JVM default size of Perm Space is around "64MB" you can easily ran out of memory if you have too many classes or huge number of Strings in your project. JVM default size of Perm Space is around "64MB" ,如果您的项目中有太多的类或大量的String,则很容易耗尽内存。 Important point to remember is that it doesn't depends on -Xmx value so no matter how big your total heap size you can ran OutOfMemory in perm space. 要记住的重要一点是,它不依赖于-Xmx值,因此,无论总堆大小有多大,都可以在烫发空间中运行OutOfMemory。 Good think is you can specify size of permanent generation using JVM options "-XX:PermSize" and "-XX:MaxPermSize" based on your project need. 不错的想法是,您可以根据项目需要使用JVM选项"-XX:PermSize""-XX:MaxPermSize"指定永久生成的大小。
  • Another reason of "java.lang.OutOfMemoryError: PermGen" is memory leak through Classloaders and it's very often surfaced in WebServer and application server like tomcat, webshere, glassfish or weblogic. "java.lang.OutOfMemoryError: PermGen"另一个原因是通过类加载器的内存泄漏,它经常出现在WebServer和应用服务器中,例如tomcat,webshere,glassfish或weblogic。
  • Another reason of OutOfMemoryError in PermGen space is if any thread started by application doesn't exit when you undeploy your PermGen空间中OutOfMemoryError的另一个原因是,当您取消部署应用程序时,是否没有退出由应用程序启动的任何线程
    application. 应用。

     JVM_ARGS="-XX:PermSize=64M -XX:MaxPermSize=256m" 

    where 哪里

    -XX:PermSize<size> - Set initial PermGen Size.

    -XX:MaxPermSize<size> - Set the maximum PermGen Size.

Resource Link: 资源链接:

  1. Tomcat – java.lang.OutOfMemoryError: PermGen space Tomcat – java.lang.OutOfMemoryError:PermGen空间
  2. Dealing with "java.lang.OutOfMemoryError: PermGen space" error 处理“ java.lang.OutOfMemoryError:PermGen空间”错误

As @Alexander mentioned, the problem you have is that you are running out of Permanent Generation space (Memory area that stores static objects, class info, etc. ). 正如@Alexander提到的那样,您遇到的问题是永久存储空间(存储静态对象,类信息等的内存区域)用光了。 The and -Xmx settings deal with the Heap space. 和-Xmx设置处理堆空间。

You want to Set the MaxPermSize, like @Alexander mentioned: 您要设置MaxPermSize,如@Alexander所述:

-XX:MaxPermSize=1024m

Another option is to upgrade to JDK 8, which does away with the Permanent Generation all together, in favor of MetaSpace. 另一个选择是升级到JDK 8,它将不再支持永久代,而支持MetaSpace。

Memory Model Management in Java Java中的内存模型管理

Java PermGen Removed Java PermGen已删除

只需在pom.xml中用-XX:MaxPermSize=1024m取消注释该行。

I had a similar issue in a web project using glassfish server. 在使用glassfish服务器的Web项目中,我遇到了类似的问题。 This was because I was reading the whole table which contains about 600000 rows of data with about 50 columns at once. 这是因为我正在读取整个表,该表一次包含大约600000行数据和大约50列。 I finally resolve the issue by reading by batch fetching, this did not only solve the java.lang.OutOfMemoryError: PermGen space error but also improved on the performance greatly. 我终于通过批量读取来解决此问题,这不仅解决了java.lang.OutOfMemoryError: PermGen空间错误,而且在性能上也有了很大的提高。

May be you are in the same shoes, and if that's the case even if you increase the perm size to 4096 you will still get the same issue sometime in the future. 可能是您穿着同一双鞋,即使是这种情况,即使将烫发尺寸增加到4096,在将来的某个时候仍然会遇到相同的问题。

You can check: JPA: what is the proper pattern for iterating over large result sets? 您可以检查: JPA:对大型结果集进行迭代的正确模式是什么? to see how it is implemented in jpa. 看看如何在jpa中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM