How to fix java.lang.OutOfMemoryError issue in Maven?

java.lang.OutOfMemoryError in maven

:-

Maven is automation build project management tool.
if the project has lot 10000 java files, whe you try to build the project using "maven clean install", build failed with error.


maven clean install will tries to clean/delete the project and complie the project, and test the project and build the applicaiton

OutOfMemoryError issues occurs either in 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 heap to increase the heap size in number of ways.
Actuall OutOfMemoryError error there is no enough space for your maven process to execute the java project or java class loading issue with your classes.

java.lang.OutOfMemoryError: Java heap space
or
java.lang.OutOfMemoryError: Perm Gen space

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


First you have to find out which phase this error is occurring
if the error is giving with compliation, the you have to increase heap size as per your system capactiy as with following lines of code in linux,windows.

Java heap space means there is no enough space to allocate your object execution in heap memory
Perm Gen space means classloader loads the classes, but when your application is still maintain the references of classes after execution is over, this we call it as memory leak, so you have to use the systemclassloader when this errors occurs in maven


Fix for OutOfMemoryError for maven in windows:-


change the environment variable MAVEN_OPTS in such way that increase the heap size with following piece of code

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

Fix for OutOfMemoryError for maven in Linux/Unix:-


Increase heapszie for evnironment variable MAVEN_OPTS.
the processing for linux version is different

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


Most of the times OutOfMemoryError solves withe above lines of code, if it does't solve, then follow the below approache

issue with maven-surefire-plugin in maven:-


As discussed , surefire plugin loads the classes using different mechanism, we will instructs this plugin to use the system classloader with the following approaces

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



the following commands more helpful in debugging your app for running your project
maven -x
mvn  -Dmaven.surefire.debug

Hope you have idea on solving OutOfMemoryError issues in maven with windows,linux and unix environment.

Feel free to comment if you have anything to share

No comments:

Post a Comment

Note: only a member of this blog may post a comment.