Maven Out of Memory Build Failure
Asked Answered
F

16

102

As of today, my maven compile fails.

[INFO] [ERROR] Unexpected
[INFO] java.lang.OutOfMemoryError: Java heap space
[INFO]  at java.util.Arrays.copyOfRange(Arrays.java:2694)
[INFO]  at java.lang.String.<init>(String.java:203)
[INFO]  at java.lang.String.substring(String.java:1877)

[ERROR] Out of memory; to increase the amount of memory, use the -Xmx flag at startup (java -Xmx128M ...)

As of yesterday I had successfully run a maven compile.

As of today, I just bumped up my heap to 3 GB. Also, I only changed 2-3 minor lines of code, so I don't understand this 'out of memory' error.

vagrant@dev:/vagrant/workspace$ echo $MAVEN_OPTS
-Xms1024m -Xmx3000m -Dmaven.surefire.debug=-Xmx3000m

EDIT: I tried the poster's comment by changing my failed module's pom.xml. But I got the same maven build error.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.5</source>
            <target>1.5</target>
            <fork>true</fork>
            <meminitial>1024m</meminitial>
            <maxmem>2024m</maxmem>
       </configuration>
    </plugin>
Foreknow answered 19/9, 2012 at 16:13 Comment(1)
Could you provide more of the stacktrace? I'm curious to see what might be causing a String initialization to run out of memory. Setting heap size in MAVEN_OPTS sounds like the way to go but my guess is that somewhere there is a ridiculously large String that you might just not be allocating enough for -Xmx.Hearts
G
152

What kind of 'web' module are you talking about? Is it a simple war and has packaging type war?

If you are not using Google's web toolkit (GWT) then you don't need to provide any gwt.extraJvmArgs

Forking the compile process might be not the best idea, because it starts a second process which ignores MAVEN_OPTS altogether, thus making analysis more difficult.

So I would try to increase the Xmx by setting the MAVEN_OPTS

export MAVEN_OPTS="-Xmx3000m"

And don't fork the compiler to a different process

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.5</source>
        <target>1.5</target>
   </configuration>
</plugin>

Increasing -XX:MaxPermSize=512m should not be required because if perm size is the reason of the problem, then I would expect the error java.lang.OutOfMemoryError: PermGen space

If that does not solve your problem, then you can create heap dumps for further analysis by adding -XX:+HeapDumpOnOutOfMemoryError. Additionally, you can use jconsole.exe in your java bin directory to connect to the jvm while the compilation is running and see what is going on inside the jvm's heap.

Another Idea (may be a stupid one) which came up to me, do you have enough RAM inside your machine? Defining the memory size is nice, but if your host has only 4GB and then you might have the problem that Java is not able to use the defined Memory because it is already used by the OS, Java, MS-Office...

Georama answered 22/9, 2012 at 12:56 Comment(11)
thanks for your reply. Does your suggestion to remove the forked JVM also apply for the 'maven-surefire-plugin?' I tried your suggestion to bump up my MAVEN_OPTS memory to 3000. My maven-compiler did not have a setting for a forked JVM, so I did not need to change anything there. And yes, my Guest VM has 4 GB of RAM. The host machine has 8 GB RAM.Foreknow
by the way, the mvn build failed again with your suggestions.Foreknow
Usually I try to avoid process forking as long as I don't get it running. If your system has only 4GB then ~1 GB is used by OS. So you have 3GB rest. If maven starts with Xms=1GB then rest of free Memory is 2GB. Next the compiler fork started with Xms=1GB .... that reduces the free memory to 1GB. Now you can substract PermGen Memory 128MB, the forked failsafe-plugin process, ... As you can see your Xmx setting most probably could never be used be the JVM since the memory is simple not free. Have you tried using JConsole? and HeapDumpOnOutOfMemoryError?Georama
I removed the Xms1024m from my MAVEN_OPTS, yet the mvn build still failed. I added the "HeapDump..." to my MAVEN_OPTS, but I'm not sure where the dump gets printed. Looking into JConsole now.Foreknow
Dumps are plces in the jvms directoryGeorama
No results for vagrant@dev:~/bin/jdk1.7.0_07$ find /home/vagrant/bin/jdk1.7.0_07 -name "jvms" -type d... Any idea where else it could live? ThanksForeknow
I mean jvm's directory. The installation directory of the java used to trigger the maven build.Georama
Would you happen to know the name of the file? Also, by installation directory, do you mean my JAVA_HOME?Foreknow
I cd'd to $JAVA_HOME, and then ran this command, vagrant@dev:~/bin/jdk1.7.0_07$ ls -Rl | grep "Sep". But I got no results, so I conclude that no log was created.Foreknow
Actually, I ran jconsole. Maven has a java process. Maven spawned another java process when compiling the maven module that's responsible for killing my build. However, when the build died, both java processes were using roughly 500 and 300 MB, respectively. That's well below the max mem values I configured.Foreknow
Then there must be another java process forked while the build is running.Georama
H
46

Answering late to mention yet another option rather than the common MAVEN_OPTS environment variable to pass to the Maven build the required JVM options.

Since Maven 3.3.1, you could have an .mvn folder as part of the concerned project and a jvm.config file as perfect place for such an option.

two new optional configuration files .mvn/jvm.config and .mvn/maven.config, located at the base directory of project source tree. If present, these files will provide default jvm and maven options. Because these files are part of the project source tree, they will be present in all project checkouts and will be automatically used every time the project is build.

As part of the official release notes

In Maven it is not simple to define JVM configuration on a per project base. The existing mechanism based on an environment variable MAVEN_OPTS and the usage of ${user.home}/.mavenrc is an other option with the drawback of not being part of the project.

Starting with this release you can define JVM configuration via ${maven.projectBasedir}/.mvn/jvm.config file which means you can define the options for your build on a per project base. This file will become part of your project and will be checked in along with your project. So no need anymore for MAVEN_OPTS, .mavenrc files. So for example if you put the following JVM options into the ${maven.projectBasedir}/.mvn/jvm.config file:

-Xmx2048m -Xms1024m -XX:MaxPermSize=512m -Djava.awt.headless=true

The main advantage of this approach is that the configuration is isolated to the concerned project and applied to the whole build as well, and less fragile than MAVEN_OPTS for other developers working on the same project (forgetting to setting it).
Moreover, the options will be applied to all modules in case of a multi-module project.

Hustings answered 14/3, 2016 at 21:50 Comment(1)
Note that MaxPermSize is ignored if you're using JDK 8.Masurium
T
18

I got same problem trying to compile "clean install" using a Lowend 512Mb ram VPS and good CPU. Run OutOfMemory and killed script repeatly.

I used export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=350m" and worked.

Still getting some other compiling failure because is the first time i need Maven, but OutOfMemory problem has gone.

Thorium answered 19/1, 2015 at 6:6 Comment(0)
J
15

Add option

-XX:MaxPermSize=512m

to MAVEN_OPTS

maven-compiler-plugin options

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
      <fork>true</fork>
      <meminitial>1024m</meminitial>
      <maxmem>2024m</maxmem>
    </configuration>
  </plugin>
Jacklyn answered 19/9, 2012 at 16:50 Comment(17)
I actually added the option, -XX:MaxPermSize=1024m, after making this post. But I still got an out of memory error. Another SO post mentioned that I need to add an option to maven-surefire-plugin's argLine to bump up the memory used by forked threads. I increased it to <argLine>-Xms256m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m</argLine>Foreknow
I should've mentioned that... No, the maven build still failed.Foreknow
Add all this properties to maven-compilier-plugin and increase -XX:MaxPermSize, Xmx should be = XX:MaxPermSizeJacklyn
Also use option <fork>true</true> in maven-compilier-pluginJacklyn
I tried that (please see original post), but my mvn build still failed.Foreknow
Run build without tests... mvn clean install -Dmaven.test.skip=trueJacklyn
Same error... [INFO] [ERROR] Unexpected [INFO] java.lang.OutOfMemoryError: Java heap space [INFO] at java.util.Arrays.copyOfRange(Arrays.java:2694)Foreknow
run with --debug option and get tail of log with StackTrace. OutOfMemory in copyOfRange it's interestingJacklyn
Actually, I saw this: cwiki.apache.org/confluence/display/MAVEN/… --> means that my POM file might be configured incorrectly. But why do I also see an Out of Memory problem?Foreknow
run maven with aditional opt -Dgwt.extraJvmArgs= and args like for JVM (Xmx, MaxPermSise, etc)Jacklyn
mvn clean install -Dgwt.extraJvmArgs=-Xms1024m -Xmx1024m or mvn clean install -Dgwt.extraJvmArgs="-Xms1024m -Xmx1024m"Jacklyn
I tried your options, but I still got out of memory errors. Also, the end of my mvn build says, "cwiki.apache.org/confluence/display/MAVEN/…." I grabbed the failed module's pom.xml from a co-worker. His pom.xml file should be good. Now I'm curious whether I have a bad pom.xml file or an out of memory problem...Foreknow
Try build each child module and then mvn package on parentJacklyn
You said ` I only changed 2-3 minor lines of code`. If you revert this lines, do you get outofmemory?Jacklyn
After I reverted all of my changes, I still got the out of memory and mojoExecutor errors. I'm trying to build each child module now.Foreknow
I successfully compiled all modules except for the 'web' module. This mvn build failed due to the same "out of memory" error as when I tried to build all packages together. Note: to build a module individually, I used >mvn install -pl <nmodule name> -am -DskipTests=true -Dmaven.test.skip=trueForeknow
MaxPermSize is deprecated, shold have no effect on newer JDKs. OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0Southport
D
7
_JAVA_OPTIONS="-Xmx3G" mvn clean install
Duodecimo answered 17/11, 2018 at 13:53 Comment(0)
T
6

I got same problem when compiling Druid.io, increasing the MaxDirectMemorySize finally worked.

export MAVEN_OPTS="-Xms8g -Xmx8g -XX:MaxDirectMemorySize=4096m"
Thunderhead answered 2/3, 2017 at 12:32 Comment(1)
Curious, MaxDirectMemorySize is ostensibly unbounded by default (i.e. you added a limit, not adjusted a preexisting one).Elver
A
5

This below configuration working in my case

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
        <verbose>true</verbose>
        <fork>true</fork>
        <argLine>-XX:MaxPermSize=500M</argLine>
    </configuration>
</plugin>

Try to use -XX:MaxPermSize instead of -XX:MaxPermGen

Atomic answered 11/2, 2018 at 20:40 Comment(0)
R
3

What type of OS are you running on?

In order to assign more than 2GB of ram it needs to be at least a 64bit OS.

Then there is another problem. Even if your OS has Unlimited RAM, but that is fragmented in a way that not a single free block of 2GB is available, you'll get out of memory exceptions too. And keep in mind that the normal Heap memory is only part of the memory the VM process is using. So on a 32bit machine you will probably never be able to set Xmx to 2048MB.

I would also suggest to set min an max memory to the same value, because in this case as soon as the VM runs out of memory the frist time 1GB is allocated from the start, the VM then allocates a new block (assuming it increases with 500MB blocks) of 1,5GB after that is allocated, it would copy all the stuff from block one to the new one and free Memory after that. If it runs out of Memory again the 2GB are allocated and the 1,5 GB are then copied, temporarily allocating 3,5GB of memory.

Rori answered 28/9, 2012 at 14:46 Comment(0)
I
2

While building the project on Unix/Linux platform, set Maven options syntax as below. Notice that single qoutation signs, not double qoutation.

export MAVEN_OPTS='-Xmx512m -XX:MaxPermSize=128m'
Inactivate answered 19/7, 2018 at 6:18 Comment(0)
H
1

Someone has already mentioned the problem with the 32 bit OS. In my case the problem was that I was compiling with 32 bit JDK.

Hafiz answered 22/8, 2019 at 9:22 Comment(0)
D
0

Using .mvn/jvm.config worked for me plus has the added benefit of being linked with the project.

Diley answered 23/3, 2016 at 18:39 Comment(0)
C
0

This happens in big projects on Windows when cygwin or other linux emulator is used(git bash). By some coincidence, both does not work on my project, what is an big open source project. In a sh script, a couple of mvn commands are called. The memory size grows to heap size bigger that specified in Xmx and most of the time in a case second windows process is started. This is making the memory consumption even higher.

The solution in this case is to use batch file and reduced Xmx size and then the maven operations are successful. If there is interest I can reveal more details.

Cassilda answered 7/1, 2019 at 20:48 Comment(0)
V
0

Increasing the memory size in the environment variable 'MAVEN_OPTS' will help resolve this issue. For me, increasing from -Xmx756M to -Xmx1024M worked.

Valdes answered 30/7, 2020 at 7:18 Comment(0)
C
0

Need to install jar files as well so that build can save memory in getting local jars.

Below command work for me after trying all those "MAVEN_OPTS" environment variable.

run "mvn clean install -U" on terminal from the root folder of the project.

Congo answered 22/2, 2021 at 10:15 Comment(0)
P
0

I would recommend that you also check if you are using the right platform.

A 32-bit JDK can only allocate up to 2GB memory for itself. So switching to a 64-bit JDK alone could solve your problem.

Parasynthesis answered 27/9, 2022 at 10:50 Comment(0)
S
0

newest test,build flink1.16 with maven 3.

if u got this error.config like this. it will work.

export MAVEN_OPTS="-Xmx6144m -Xms4096m"

if u still got out of memory error. increase it more.

Strike answered 8/3, 2023 at 7:19 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Snowstorm

© 2022 - 2024 — McMap. All rights reserved.