簡體   English   中英

在PowerMockito中運行Cobertura並拋出java.lang.OutOfMemoryError:PermGen空間

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

如何使用 + 解決內存不足 / 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屬性添加最大內存,如cobertura-mojo中所述

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

但是它沒有按預期工作,我可以看到在PowerMock存儲庫中幾乎沒有未解決的問題Issue-227Issue-520
我嘗試通過使用以下注釋來添加忽略包,但是如果測試類的數量更多,它將無法正常工作。 @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的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>

我想給您一些解決問題的建議。

  1. 首先,從pom.xml中刪除所有內存空間。
  2. 其次,根據項目需要增加JVM選項"-XX:PermSize""-XX:MaxPermSize"

根本原因分析:

  • JVM default size of Perm Space is around "64MB" ,如果您的項目中有太多的類或大量的String,則很容易耗盡內存。 要記住的重要一點是,它不依賴於-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<size> - Set initial PermGen Size.

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

資源鏈接:

  1. Tomcat – java.lang.OutOfMemoryError:PermGen空間
  2. 處理“ java.lang.OutOfMemoryError:PermGen空間”錯誤

正如@Alexander提到的那樣,您遇到的問題是永久存儲空間(存儲靜態對象,類信息等的內存區域)用光了。 和-Xmx設置處理堆空間。

您要設置MaxPermSize,如@Alexander所述:

-XX:MaxPermSize=1024m

另一個選擇是升級到JDK 8,它將不再支持永久代,而支持MetaSpace。

Java中的內存模型管理

Java PermGen已刪除

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

在使用glassfish服務器的Web項目中,我遇到了類似的問題。 這是因為我正在讀取整個表,該表一次包含大約600000行數據和大約50列。 我終於通過批量讀取來解決此問題,這不僅解決了java.lang.OutOfMemoryError: PermGen空間錯誤,而且在性能上也有了很大的提高。

可能是您穿着同一雙鞋,即使是這種情況,即使將燙發尺寸增加到4096,在將來的某個時候仍然會遇到相同的問題。

您可以檢查: JPA:對大型結果集進行迭代的正確模式是什么? 看看如何在jpa中實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM