Wednesday, August 3, 2011

How to increase java heap space on Maven and ANT

Many times we get java.lang.OutOfMemoryError: java heap space while building our project either by using maven or ANT just because heap size is not enough to build the project. At this point we just want to increase the heap size used by ANT or MAVEN and this java tip will let you do this:

How to increase Java Heap size for Maven on Linux/Unix

1. Open your mvn.sh file and add the following line:
export MAVEN_OPTS=-Xmx512m
this will allow to specify java heap space based on your project needs.



How to increase Java Heap space for Maven on Windows
1.      go to C:\apache-maven-2.2.1\bin
2.      open your mvn.bat file and add the following line :
set  MAVEN_OPTS=-Xmx512m
next time you run maven commands like mvn clean or mvn install it will use this heap size.
This article is in continuation of my earlier post on java heap 10 points on java heap space  if you haven’ read that you may find some useful info there.


How to increase Java Heap size/space/memory for ANT on Linux/Unix

Similar to Maven you can specify ANT_OPTS to specify JVM specific arguments for ANT build tool. Here is step by step guide to change the java heap space for ANT:
1.      Go to ANT_HOME directry (directory where Ant has been installed)
2.      Open ant.bat file and add following line on it
export ANT_OPTS=-Xmx512m
some time you may need to put the value –Xmx512M inside double quotes.
3.      Done next time you run “ant” it will use modified java heap size and will not throw OutOfMemoryError till the new limit reached.


How to increase Java Heap size/space/memory for ANT on Windows

As shown above for Linux ANT_OPTS can also be used specify JVM specific arguments for ANT in windows.  Here is step by step guide to increase the java heap memory for ANT in Windows:
4.      Go to ANT_HOME directory (directory where Ant has been installed)
5.      Open ant.bat file and add following line on it
set ANT_OPTS=-Xmx512m
some time you may need to put the value –Xmx512M inside double quotes.
6.      Done next time you run “ant” it will use modified java heap size and will not throw OutOfMemoryError till the new limit reached.

Related post:

5 comments :

Anonymous said...

This is exactly I was looking for setting heap size for ANT and Maven. just one question do we need to specify ANT_OPTS or MAVEN_OPTS in there respective batch file or we can just export heap size as environment variable ?

al0 said...

Definitely, you can set/export an environment variable without editing of the batch file. Moreover, it is a preferred method.

Javin @ comparable in java said...

Definitely al0, but that could override any parameter which is getting set inside JAVA_OPTS in script but in normal case until you don't have complex build setup its better to use environment variable rather than changing batch file.

Anonymous said...

Hi, please can anybody help me with this error

Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

OS :- 64 bit
windows version :- windows 7 ultimate
Ram :- 8GB
Jre6 :-64 bit version
jdk 1.5 :- 64 bit version
eclipse juno release 2

Neeshan said...

for UNIX operating system, environment variable should be inside single or double quotes. So right way of setting Maven options are

export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=256m"

You can either exclude settings for permgen heap if you are only getting outofmemoryerror on java heap space, but setting them both to a higher value is good, especially if you are using Maven compile plugin with thousands of classes e.g. those generated by framework like XML beans by reading XSD files.

Post a Comment