java.lang.OutOfMemoryError in maven
Maven is an automation build project management tool. if the project has lot 10000 java files, when you try to build the project using “maven clean install”, build failed with an error.
maven clean install will tries to clean/delete the project and compile the project, and 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
test case execution, maven uses
surefire-plugin. in both of these cases, you have a heap to increase the heap size in a number of ways.
Actually OutOfMemoryError error there is not enough space for your maven process to execute the java project or java classloading issue with your classes.
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Perm Gen space
For debugging the exact reason, use the maven -x clean install, it gives exact root cause with a detailed print stack trace.
First you have to find out which phase this error is occurring
if the error is giving with compilation, you have to increase heap size as per your system capacity as with following lines of code in Linux, windows.
Java heap space means there is not enough space to allocate your object execution in heap memory
Perm Gen space means classloader loads the classes, but when your application still maintains the references of classes after execution is over, this we call it as a memory leak, so you have to use the system classloader when this error occurs in maven
Fix for OutOfMemoryError for maven in windows:-
change the environment variable
MAVEN\_OPTS in such way that increase 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
the processing for 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, 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
<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 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 environment.
Feel free to comment if you have anything to share