简体   繁体   中英

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

How to solve the Out of memory / PermGen space issue while using + .

When I ran mvn cobertura:cobertura on test cases it's getting exception like below:

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

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

            <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.
  2. Secondly, increase your JVM options "-XX:PermSize" and "-XX:MaxPermSize" based on your project need.

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. 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. Good think is you can specify size of permanent generation using JVM options "-XX:PermSize" and "-XX:MaxPermSize" based on your project need.
  • 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.
  • Another reason of OutOfMemoryError in PermGen space is if any thread started by application doesn't exit when you undeploy your
    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
  2. Dealing with "java.lang.OutOfMemoryError: PermGen space" error

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. ). The and -Xmx settings deal with the Heap space.

You want to Set the MaxPermSize, like @Alexander mentioned:

-XX:MaxPermSize=1024m

Another option is to upgrade to JDK 8, which does away with the Permanent Generation all together, in favor of MetaSpace.

Memory Model Management in Java

Java PermGen Removed

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

I had a similar issue in a web project using glassfish server. This was because I was reading the whole table which contains about 600000 rows of data with about 50 columns at once. 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.

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.

You can check: JPA: what is the proper pattern for iterating over large result sets? to see how it is implemented in jpa.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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