93

I get this message during build of my project

java.lang.OutOfMemoryError: Java heap space

How do I increase heap space, I've got 8Gb or RAM its impossible that maven consumed that much, I found this http://vikashazrati.wordpress.com/2007/07/26/quicktip-how-to-increase-the-java-heap-memory-for-maven-2-on-linux/ how to do it on linux, but I'm on windows 7. How can I change java heap space under windows ?

rlong
  • 187
  • 3
  • 10
Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263
  • I assume you are on 64bit version of windows? If not, you are not going to specify any more than 1.5G as the heap size, the other 0.5G windows allows you to use directly is going to be required by the JVM. There is an option to get windows to only use 1G for the kernal mode code and 3G for the user mode code, but the sun JVM won't allow you to use it. – vickirk May 12 '10 at 14:58
  • @vickirk I'm on 64bit version of windows – Gandalf StormCrow May 12 '10 at 15:01
  • 1
    Are you also using a 64-bit version of the JVM? It'll be listed as Windows x64 in the dropdown list on the JDK download page: http://java.sun.com/javase/downloads/widget/jdk6.jsp – Powerlord May 12 '10 at 16:32

8 Answers8

120

The environment variable to set is MAVEN_OPTS, for example MAVEN_OPTS=-Xmx1024m. The maxmem configuration in the pom only applies when you set the compiler plugin to fork javac into a new JVM. Otherwise the plugin runs inside the same VM as Maven and thus within the memory passed on the command line via the MAVEN_OPTS.

To set MAVEN_OPTS under Windows 7:

  1. Right click on My Computer and select Properties (keyboard shortcut press Windows + Pause/Break)
  2. Click the Advanced System Settings link located in the left navigation of System Properties to display the Advanced System Properties
  3. Go to the Advanced tab and click the Environment Variables button located at the bottom of the Advanced System Properties configuration window
  4. Create a New user variable, set the Variable name to MAVEN_OPTS and set the Variable value to -Xmx1024m (or more)

Open a new command window and run mvn.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Does this have any impact on speed of my build? Is there any tip to boostup my maven build with allocating more memory to it? – Mahendran Oct 12 '12 at 11:25
45

If you are running out of heap space during the surefire (or failsafe) JUnit testing run, changing MAVEN_OPTS may not help you. I kept trying different configurations in MAVEN_OPTS with no luck until I found this post that fixed the problem.

Basically the JUnits fork off into their own environment and ignore the settings in MAVEN_OPTS. You need to configure surefire in your pom to add more memory for the JUnits.

Hopefully this can save someone else some time!


Edit: Copying solution from Keith Chapman's blog just in case the link breaks some day:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <forkMode>pertest</forkMode> 
    <argLine>-Xms256m -Xmx512m</argLine>
    <testFailureIgnore>false</testFailureIgnore> 
    <skip>false</skip> 
    <includes> 
      <include>**/*IntegrationTestSuite.java</include>
    </includes>
  </configuration>
</plugin>

Update (5/31/2017): Thanks to @johnstosh for pointing this out - surefire has evolved a bit since I put this answer out there. Here is a link to their documentation and an updated code sample (arg line is still the important part for this question):

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
        <forkCount>3</forkCount>
        <reuseForks>true</reuseForks>
        <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
        <systemPropertyVariables>
            <databaseSchema>MY_TEST_SCHEMA_${surefire.forkNumber}</databaseSchema>
        </systemPropertyVariables>
        <workingDirectory>FORK_DIRECTORY_${surefire.forkNumber}</workingDirectory>
    </configuration>
  </plugin>
Marquee
  • 1,776
  • 20
  • 21
  • It did save some of my time! Thanks! Unfortunately I can only upvote once! :) – javanna Jan 16 '13 at 17:46
  • This is the most helpful answer between others, in real maven style. Thanks! – Andremoniy May 06 '14 at 14:36
  • This is the actual answer to the question – Jonathan E. Landrum Jul 25 '16 at 21:07
  • 1
    See the docs on surefire. surefire versions prior 2.14 used the parameter forkMode to configure forking. Although that parameter is still supported for backward compatibility, users are strongly encouraged to migrate their configuration and use forkCount and reuseForks instead. – johnstosh May 28 '17 at 05:30
21

It should be the same command, except SET instead of EXPORT

  • set MAVEN_OPTS=-Xmx512m would give it 512Mb of heap
  • set MAVEN_OPTS=-Xmx2048m would give it 2Gb of heap
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • I agree that it should be the same command, but that doesn't make it true statement, here is why `C:\my_project>MAVEN_OPTS=-Xmx512m 'MAVEN_OPTS' is not recognized as an internal or external command, operable program or batch file.` – Gandalf StormCrow May 12 '10 at 14:51
  • glowcoder is slightly wrong. You don't need to EXPORT the variables, but to assign a value to them you do need set. – JeremyP May 12 '10 at 15:04
  • You can also set MAVEN_OPTS as a windows environment variable if you want to use the same values for every build. – digitaljoel May 12 '10 at 15:45
  • @Gandalf you need to put the set in front of it @Jeremy yes, that's why it says to use set instead of export – corsiKa May 12 '10 at 16:04
9

On the Mac: Instead of JAVA_OPTS and MAVEN_OPTS, use _JAVA_OPTIONS instead. This works!

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Steve
  • 91
  • 1
  • 1
  • this works also on windows. Add this as an environment variable in maven run configuration in eclipse, tests run from m2e no longer give PermGen error – fedeb Mar 12 '19 at 08:44
3

After trying to use the MAVEN_OPTS variable with no luck, I came across this site which worked for me. So all I had to do was add -Xms128m -Xmx1024m to the default VM options and it worked.

To change those in Eclipse, go to Window -> Preferences -> Java -> Installed JREs. Select the checked JRE/JDK and click edit.

kent_e
  • 111
  • 1
  • 7
0

It worked - To change in Eclipse, go to Window -> Preferences -> Java -> Installed JREs. Select the checked JRE/JDK and click edit.

Default VM Arguments = -Xms128m -Xmx1024m

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
0

On windows:

Add an environmental variable (in both system and user's variables, I have some weird problem, that it gets the var from various places, so I add them in both of them).

Name it MAVEN_OPTS.

Value will be: -Xms1024m -Xmx3000m -XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled

The numbers can be different, make them relative to your mem size.

I had that problem and this fixed it, nothing else!

arghtype
  • 4,376
  • 11
  • 45
  • 60
Hrabia
  • 19
  • 7
-7

You are looking for 2 options to java:

  • -Xmx maximum heap size
  • -Xms starting heap size

Put them in your command line invocation of the java executable, like this:

java -Xms512M -Xmx1024M my.package.MainClass

Keep in mind that you may want the starting and max heap sizes to be the same, depending on the application, as it avoids resizing the heap during runtime (which can take up time in applications that need to be responsive). Resizing the heap can entail moving a lot of objects around and redoing bookkeeping.

For every-day projects, make them whatever you think is good enough. Profile for help.

Phil
  • 2,828
  • 1
  • 23
  • 20
  • 1
    as ebelisle states, this options would not work at all as you, using Maven, have to specify at least that is a Maven option with "-D" but still is not a java command what we're talking about – frandevel Aug 28 '12 at 13:56