37

I'm compiling an open source project with "mvn install" but ended up with java.lang.OutOfMemoryError: Java heap space. I tried to execute java -Xmx256m but the output was java synopsis which indicated it's an invalid command.

I'm using jdk1.5.0_08, any ideas why this is happening?

Thanks,

cletus
  • 616,129
  • 168
  • 910
  • 942
user124858
  • 645
  • 3
  • 9
  • 16

9 Answers9

55

Set the environment variable:

MAVEN_OPTS="-Xmx512m"
cletus
  • 616,129
  • 168
  • 910
  • 942
  • 12
    Sometimes is good also to extend perm memory size - MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=256m" – cetnar Dec 18 '09 at 10:55
  • 2
    Setting MAVEN_OPTS alone is not sufficient if the build fails while running unit tests. In such cases, you may have to configure the "argLine" of maven-surefire-plugin explicitly. – James Selvakumar Jan 13 '14 at 02:29
  • 1
    I was getting this error on mvn install, using "mvn install -DMAVEN_OPTS=-Xmx1024m" solved my problem! – Surekha Nov 07 '14 at 19:47
19

It depends on which JVM instance require more memory. As example, if tests are forked (by default), and fails due OutOfMemoryError then try configure plugin which launching them:

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>-Xmx1024m</argLine>
            </configuration>
        </plugin>
Andriy Plokhotnyuk
  • 7,883
  • 2
  • 44
  • 68
9

Not only heap memory. You have to increase perm size also to resolve that exception in maven use these variables in environment variable.

variable name: MAVEN_OPTS
variable value: -Xmx512m -XX:MaxPermSize=256m

sjngm
  • 12,423
  • 14
  • 84
  • 114
msuresham
  • 91
  • 1
  • 1
  • How can you get the current value of `Xmx` and `MaxPermSize`? After all, I can't increase the values without knowing what they're already set to. – ArtOfWarfare Oct 11 '13 at 15:01
  • I'm not sure of MaxPermSize, but Xmx is usually 1/4th of the physical memory. Again, this may vary depending on the JRE implementations and/or "server", "client" mode of JVM. – James Selvakumar Jan 13 '14 at 02:33
  • -bash: export: `-XX:MaxPermSize=256m': not a valid identifier – user2568374 Jan 19 '17 at 21:12
6

Sometimes, if you use other programs inside maven, like in integration tests or Surefire, the MAVEN_OPTS is not enough. Apart from MAVEN_OPTS, try to use JAVA_TOOL_OPTIONS. This did the trick for me in Maven when running some integration tests:

export JAVA_TOOL_OPTIONS="-Xmx2048m -XX:MaxPermSize=1024m -Xms2048m"

Reference: osx maven running tests Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"

4lberto
  • 505
  • 6
  • 14
1

on a Unix-like system:

export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=512m"

OutOfMemoryError

hahakubile
  • 6,978
  • 4
  • 28
  • 18
0

Alternatively use set MAVEN_OPTS="-XX:MaxPermSize=256m" while running from command prompt.

user666
  • 1,104
  • 12
  • 20
0

I always use this with my shell on ubuntu:

export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=500m"
Lucas Pires
  • 1,281
  • 14
  • 21
-1

Try to change or update your Maven install. I had this problem but I solved it by upgrading Maven (3.0.5 to 3.3.3).

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Abdourahmane FALL
  • 1,836
  • 13
  • 7
-5

I experienced the same problem. According to this link it might help to change jvm implementation - this can be done by setting JAVA_HOME system variable.

Try for example in the link mentioned ibm jvm or oracle jrockit:

SET JAVA_HOME=C:\bea10\jrockit160_22
mvn install
Matus
  • 11