How to fix OutOfMemoryError issue in Maven| Perm Gen space |Java heap space

java.lang.OutOfMemoryError in maven

Maven is an automated build project management tool. if the project has 10000 or more java files and you try to build the project using “maven clean install”, the build failed with an error - java.lang.OutOfMemoryError.

maven clean install will try to clean/delete the project and compile the project, test the project, and build the application.

OutOfMemoryError issues occur either in the compile phase of your project or test phase of your project while building your maven project For test case execution, maven uses surefire-plugin. in both of these cases, you have a heap to increase the heap size in many ways.

The OutOfMemoryError error log gives additional information as seen below

java.lang.OutOfMemoryError: Java heap space

To fix this error, java.lang.OutOfMemoryError: Java heap space Java heap space means there is not enough space to allocate your object execution in the heap memory of the maven project

java.lang.OutOfMemoryError: Perm Gen space

Perm Gen space is related to an error on classload in java with your classes. This error occurs during classloader loads the classes when your application still maintains the references of classes after execution is over, we call it a memory leak, so you have to use the system classloader when this error occurs in maven.

For debugging the exact reason, use the maven -x clean install command, it gives the exact root cause with a detailed additional log stack trace.

First, you have to find out which phase this error is occurring if the error is given with compilation, you have to increase heap size as per your system capacity as with the following lines of code in Linux, windows.

Fix for OutOfMemoryError for maven in windows:-

change the environment variable MAVEN\_OPTS in such a way that increases the heap size by following a piece of code

set MAVEN_OPTS=-Xmx768M -XX:MaxPermSize=768M

Fix for OutOfMemoryError for maven in Linux/Unix:-

Increase heap size for environment variable MAVEN\_OPTS. the processing for the Linux version is different

export MAVEN_OPTS=-Xmx768M -XX:MaxPermSize=768M

Most of the times OutOfMemoryError solves with the above lines of code, if it doesn’t solve, then follow the below approaches.

Issue with maven-surefire-plugin in maven:-

As discussed, the surefire plugin loads the classes using a different mechanism, we will instruct this plugin to use the system classloader with the following approaches.

<configuration>
          <usesystemclassloader>false</usesystemclassloader>
    <forkmode>never</forkmode>
        </configuration>

And also you can increase plugin heap size with the configuration option argLine.

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
            <argLine>-Xmx1024m</argLine>
        </configuration>
      </plugin>
    </plugins>
  </build>

the following commands are more helpful in debugging your app for running your project

maven -x
mvn  -Dmaven.surefire.debug

Hope you have an idea on solving OutOfMemoryError issues in maven with windows, Linux, and Unix environments.

Feel free to comment if you have anything to share