【问题标题】:Running Cobertura with PowerMockito throwing java.lang.OutOfMemoryError: PermGen space使用 PowerMockito 运行 Cobertura,抛出 java.lang.OutOfMemoryError:PermGen 空间
【发布时间】:2016-07-26 10:26:12
【问题描述】:

如何解决使用 + 时出现的Out of memory / 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-mojo 中提到的以下 cobertura 属性来添加最大内存:

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

但它没有按预期工作, 我可以看到Issue-227Issue-520PowerMock 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>

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

【问题讨论】:

  • 你在使用surefire插件吗?
  • @Santosh,是的。通过添加或不添加surefire插件没有区别。遇到同样的问题。
  • 你能显示完整的堆栈跟踪吗..

标签: powermockito cobertura java maven powermock cobertura powermockito


【解决方案1】:

请在 pom.xml 中添加 &lt;version&gt;2.19.1&lt;/version&gt;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>

【讨论】:

  • 是的,你是对的@Praveen Kumar,当我将版本号添加到 maven-surefire-plugin 时,我能够毫无问题地运行所有测试。
【解决方案2】:

正如@Alexander 提到的,您遇到的问题是您的永久代空间(存储静态对象、类信息等的内存区域)用完了。和 -Xmx 设置处理堆空间。

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

-XX:MaxPermSize=1024m

另一个选择是升级到 JDK 8,它完全取消了永久代,转而支持 MetaSpace。

Memory Model Management in Java

Java PermGen Removed

【讨论】:

    【解决方案3】:

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

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

    根本原因分析:

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

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

      在哪里

      -XX:PermSize&lt;size&gt; - Set initial PermGen Size.

      -XX:MaxPermSize&lt;size&gt; - Set the maximum PermGen Size.

    资源链接:

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

    【讨论】:

      【解决方案4】:

      只需在您的 pom.xml 中取消注释带有 -XX:MaxPermSize=1024m 的行。

      【讨论】:

        【解决方案5】:

        我在使用 glassfish 服务器的 Web 项目中遇到了类似的问题。这是因为我正在读取包含大约 600000 行数据和大约 50 列的整个表。我最终通过批量读取的方式解决了这个问题,这不仅解决了java.lang.OutOfMemoryError: PermGen空间错误,而且大大提高了性能。

        可能您的情况相同,如果是这种情况,即使您将烫发尺寸增加到 4096,您将来某个时候仍然会遇到同样的问题。

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

        【讨论】:

          猜你喜欢
          • 2013-09-01
          • 2011-04-14
          • 2012-10-21
          • 2011-07-28
          • 2012-08-05
          • 1970-01-01
          • 1970-01-01
          • 2014-04-17
          • 2015-05-11
          相关资源
          最近更新 更多