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

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

如何解决内存不足 / PermGen space问题,同时使用 +

当我在测试用例上 运行 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 属性 添加最大内存,如 cobertura-mojo 中所述:

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

但它没有按预期工作, 我可以看到有几个未解决的问题 Issue-227, Issue-520 at 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>

如果有任何解决方法,请告诉我。

只需在 pom.xml.

中取消注释带有 -XX:MaxPermSize=1024m 的行

我在使用 glassfish 服务器的 Web 项目中遇到了类似的问题。这是因为我正在阅读整个 table,其中一次包含大约 600000 行数据和大约 50 列。最后我通过批量读取读取解决了这个问题,这不仅解决了 java.lang.OutOfMemoryError: PermGen space 的错误,而且在性能上也有很大的提升。

可能是你的处境相同,如果是这种情况,即使你将烫发尺寸增加到 4096,你以后仍然会遇到同样的问题。

您可以查看:JPA: what is the proper pattern for iterating over large result sets? 了解它在 jpa 中是如何实现的。

我想给你一些解决问题的建议。

  1. 首先从你的 pom.xml.
  2. 中删除所有内存 space
  3. 其次,根据项目需要增加 JVM 选项 "-XX:PermSize""-XX:MaxPermSize"

根本原因分析:

  • JVM default size of Perm Space is around "64MB"你可以轻松运行 如果您有太多 类 或大量字符串,则内存不足 在你的项目中。 要记住的重要一点是它不 取决于 -Xmx 值,所以无论您的总堆大小有多大 可以 运行 OutOfMemory in perm space. 很好,你可以指定 使用 JVM 选项 "-XX:PermSize""-XX:MaxPermSize" 根据您的项目需要。
  • "java.lang.OutOfMemoryError: PermGen"的另一个原因是内存 通过类加载器泄漏,它经常出现在 WebServer 中 和应用程序服务器,如 tomcat、webshere、glassfish 或 weblogic。
  • PermGen 中 OutOfMemoryError 的另一个原因 space 是如果任何线程 当您取消部署
    时,应用程序启动不会退出 应用。

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

    哪里

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

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

资源Link:

  1. Tomcat – java.lang.OutOfMemoryError: PermGen space
  2. Dealing with "java.lang.OutOfMemoryError: PermGen space" error

正如@Alexander 所提到的,您遇到的问题是您 运行 超出了永久生成 space(存储静态对象的内存区域,class 信息等) .和 -Xmx 设置处理堆 space。

您想设置 MaxPermSize,就像@Alexander 提到的那样:

-XX:MaxPermSize=1024m

另一种选择是升级到 JDK 8,这将完全取消永久代,支持 MetaSpace。

Memory Model Management in Java

Java PermGen Removed

请将<version>2.19.1</version>添加到pom.xml中的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>