Thursday, August 5, 2021

How to increase java heap space on Maven and ANT - Example

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 us 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 have’ read that you may find some useful info there.

Btw, if you are just starting with Maven, I suggest you first go through comprehensive Maven courses to learn some fundamentals. It will not only help you to build and deploy your Java and Spring Boot projects using Maven but also learn Maven itself in depth.

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 an ANT build tool. Here is the step by step guide to change the java heap space for ANT:
1.      Go to ANT_HOME directory (the directory where Ant has been installed)
2.      Open ant.bat file and add the following line on it
export ANT_OPTS=-Xmx512m
sometimes 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 to specify JVM specific arguments for ANT in windows. Here is a step by step guide to increasing the java heap memory for ANT in Windows:

4.      Go to ANT_HOME directory (the directory where Ant has been installed) 

5.      Open ant.bat file and add the following line on its ANT_OPTS=-Xmx512m
sometimes 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 is reached.


Further Reading
Free Maven Courses for Beginners
Difference between mvn install and mvn release command
Free Maven Books for Java Developers
10 Things about Maven Java Developer should know

Thanks for reading this article so far, if you like this article then please share it with your friends and colleagues. If you have any questions or suggestions then please drop a comment and I'll try to find an answer for you. 

6 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.

Unknown said...

Hi, please can anybody help me with this error
trying ant deploy using cmd
Error :-
[javac] The system is out of resources.
[javac] Consult the following stack trace for details.
[javac] java.lang.StackOverflowError
ANT_OPT :- -Xms1024m -Xmx1024m -XX:+UseParallelGC
OS :- 64 bit
windows version :- windows 7
Ram :- 8GB
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

Post a Comment