When a MAVEN project is large, or you run a command such as MVN site, the MAVEN runtime requires a lot of memory and, under the default configuration, it is possible to encounter a heap overflow in Java. Such as:
[INFO] Building jar:/home/dl9pf/svn/mindquarry/mindquarry-jcr/mindquarry-jcr-changes/target/ Mindquarry-migration-with-dependencies.jar
[INFO]------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO]------------------------------------------------------------------------
[INFO] Java Heap Space
[INFO]------------------------------------------------------------------------
[INFO] Trace
Java.lang.OutOfMemoryError:Java Heap Space
At Java.lang.AbstractStringBuilder.expandCapacity (abstractstringbuilder.java:99)
At Java.lang.AbstractStringBuilder.append (abstractstringbuilder.java:518)
...
At org.codehaus.classworlds.Launcher.launchEnhanced (launcher.java:315)
At Org.codehaus.classworlds.Launcher.launch (launcher.java:255)
At Org.codehaus.classworlds.Launcher.mainWithExitCode (launcher.java:430)
At Org.codehaus.classworlds.Launcher.main (launcher.java:375)
[INFO]------------------------------------------------------------------------
[INFO] Total Time:7 minutes seconds
[INFO] Finished at:wed Sep 07:44:55 CEST 2007
[INFO] Final memory:37m/63m
[INFO]------------------------------------------------------------------------
The workaround is to adjust the value of the Java heap size.
in a Windows environment
Locate the file %m2_home%\bin\mvn.bat , which is the script file that launches Maven, where you can see a line of comments:
@REM Set Maven_opts=-xdebug-xnoagent-djava.compiler=none ...
It means that you can set up some maven parameters, and we'll add a line below the comment:
Set maven_opts=-xms128m-xmx512m
After that, when you run the MAVEN command like Mvn-version, you will see the following output:
e:\test>mvn-version
E:\test>set maven_opts=-xms128m-xmx512m
Maven version:2.0.9
Java version:1.6.0_07
OS Name: "Windows 2003" version: "5.2" Arch: "x86" Family: "Windows"
We see that the configured MAVEN option is in effect and outofmemoryerror can be resolved accordingly.
in a Linux environment
You can also fix the problem by setting the environment variable, for example, edit the file /etc/profile as follows
maven_opts=-xmx512m
Export Java_home maven_home maven_opts java_bin PATH CLASSPATH
If you use Hudson
With Hudson + maven for continuous integration, and unfortunately also encountered similar errors, then both of these methods will no longer work, because Hudson use their own maven-agent to start Maven, not to invoke Maven's script, the natural corresponding configuration will be invalid.
Fortunately Hudson also provides us with a configuration point, in the Hudson Project configuration page, there is a build area, where we have set the root pom and goals. Note that there is an "advanced ..." button in the lower right corner of the area and you will see the maven_opts input box, where you can enter "-xmx512m" to OK.
In M2eclipse
Similar methods will fail, fortunately M2eclipse provides a configuration point. The steps are as follows:
Right-click on the project, run as, run configurations, Maven Build
At this point you will see a MAVEN Run Configuration dialog, which I do not explain more, in order to solve the memory overflow problem, we can choose the second Tab:jre, and then enter the configuration in VM arguments such as:-xms128m-xmx512m.
Configuring maven_opts To resolve OutOfMemory errors in Maven