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 For test case execution, maven uses surefire-plugin. in any case, 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
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
Increase heap size for environment variable MAVEN_OPTS. 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
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
the following commands more helpful in debugging your app for running your project
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