HOW TO: ant OutOfMemoryError: Java heap space

If you are compiling with javac task in Apache Ant, set fork attribute to true, to run javac in a separate process with its own heap size settings.

If fork is set to false, or not set (default is false), javac will run in the same process as Ant, which has a default maximum heap size of 64m.

The following is a snippet from build.xml:

<javac fork="true"
     srcdir="${basedir}/src"
     destdir="${basedir}/build/classes"
     classpath="${project.classpath}"
     includeantruntime="false"
     memoryinitialsize="256m"
     memorymaximumsize="256m">
     <compilerarg line="-endorseddirs ${env.CATALINA_BASE}/endorsed" />
</javac>

If you don't want to modify existing build.xml files, another option is to increase the heap size for Ant JVM and still execute javac task in-process.

You just need to set environment variable ANT_OPTS:

export ANT_OPTS="-Xms256m -Xmx256m"    (ksh/bash)
setenv ANT_OPTS="-Xms256m -Xmx256m"    (tcsh/csh)
set ANT_OPTS=-Xms256m -Xmx256m         (Windows)

A disadvantage of this approach is users will need to remember to set this environment variable, or use some sort of wrapper script on top of %ANT_HOME%\bin\ant.bat, or $ANT_HOME/bin/ant.

wiki/ant/out_of_memory_error.txt ยท Last modified: 2016/09/29 16:11 (external edit)