'"Invalid maximum heap size" when running Maven

I'm having problems setting up maven. I'm using windows 7 64 bit with 4 GB of RAM.

I get this error when trying to run mvn:

Invalid maximum heap size: -Xmx512m.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Here are my user variables:

JAVA_HOME=C:\Program Files\Java\jdk1.7.0_55

M2=%M2_HOME%\bin

M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.2.1

MAVEN_OPTS=-Xms256m -Xmx512m

Path=%M2%;%JAVA_HOME%\bin


Solution 1:[1]

User @med_alpa had the right suggestion in my case (Windows):

I resolved the same issue on windows executing this command on windows command line : set MAVEN_OPTS=-Xmx1024M -XX:MaxPermSize=256M

So make sure to not put quotes around the value you set for MAVEN_OPTS

Solution 2:[2]

alter jvm.config in .mvn directory ( it is in controller directory ). study mvn.sh that comes with maven. you will see MAVEN_OPTS setting in the script. try out java -version before altering. eg : java -Xmx512m -version now you know how to go about and make the changes.

/sunil

Solution 3:[3]

Try using: -Xmx1024M

Note: 'M' instead 'm'

Solution 4:[4]

On MacOS a version of @adriaan-koster's answer works.

In your ~/.zshrc (or .bashrc or .bash_profile) config file add this line:

export MAVEN_OPTS="-Xmx2G"

To test, run mvn -v again and verify that it prints the Maven version, Maven home location, and Java version.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 sunil
Solution 3 Alex
Solution 4 Micah Stubbs