28

I am facing this issue while building my build.xml.

BUILD FAILED
java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2786)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
    at org.codehaus.cargo.module.DefaultJarArchive.<init>(DefaultJarArchive.
java:75)
    at org.codehaus.cargo.module.DefaultJarArchive.<init>(DefaultJarArchive.
java:56)
    at org.codehaus.cargo.module.webapp.DefaultWarArchive.<init>(DefaultWarA
rchive.java:69)
    at org.apache.cactus.integration.ant.CactifyWarTask.addJarWithClass(Cact
ifyWarTask.java:652)
    at org.apache.cactus.integration.ant.CactifyWarTask.addCactusJars(Cactif
yWarTask.java:627)
    at org.apache.cactus.integration.ant.CactifyWarTask.execute(CactifyWarTa
sk.java:519)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    at sun.reflect.GeneratedMethodAccessor31.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:62)
    at net.sf.antcontrib.logic.IfTask.execute(IfTask.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
    at org.apache.tools.ant.TaskAdapter.execute(TaskAdapter.java:154)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    at sun.reflect.GeneratedMethodAccessor31.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:357)
    at org.apache.tools.ant.Target.performTasks(Target.java:385)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
JW8
  • 1,496
  • 5
  • 21
  • 36
vibhas
  • 311
  • 1
  • 4
  • 4

4 Answers4

57

It sounds like you need to launch your JVM with a larger memory limit. Try something like (if you're using Bourne shell):

export ANT_OPTS=-Xmx1g
ant

or, if you use cmd under Windows:

set ANT_OPTS=-Xmx1g
ant

Above, the 1g means 1 GB. You can tweak that to whatever you like; for example, if you want to use 1.5 GB, you can use -Xmx1536m.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 1
    Where should i add set ANT_OPTS=-Xmx1g ant is it in ant script file?? – vibhas Sep 21 '11 at 06:30
  • Run that command before you type `ant`. I am assuming that you're running `ant` from the command line. If you're running ant from an IDE, your IDE should provide you a way to pass Java options. – C. K. Young Sep 21 '11 at 06:31
  • awesome ... thanks a lot. but is there a way to set it like permanently? – Ajo Koshy May 26 '15 at 05:23
  • You can set it permanently if you set it as an environment variable. So add the environment variable in the standard editor under windows (or try www.rapidee.com), or simply add the `export` line in `~/.bashrc` or `~/.profile` under linux and mac. – Leon S. Jan 16 '17 at 16:11
13

(For < Java 8, permgen doesn't apply to Java 8 and up)

For benefit of those who got a similar trace with the word "PermGen" in the Error message,

java.lang.OutOfMemoryError: PermGen space

Increasing the perm-gean space in ANT_OPTS (preferably, though not necessarily along with the Max heap size) should solve the issue

example

export ANT_OPTS="-Xmx2g -XX:MaxPermSize=512m"

Adjust the numbers as per your your need.

Cheers!

Y123
  • 915
  • 13
  • 30
7

If 'javac' task is used with fork option then , javac will run in his own process and memory should be directly assigned to the javac using 'memoryinitialsize' and 'memorymaximumsize':

javac   srcdir="${src}"
            destdir="${classes}"
            source="1.6"
            debug="true"
            fork="true"
            deprecation="off"
            memoryinitialsize="512m"
            memorymaximumsize="1024m"
DejanR
  • 441
  • 4
  • 11
0

You need to set ANT_OPTS to next higher level in dx.bat file.which is located \build-tools\versions\dx.bat. set defaultXmx=-Xmx2048M I set it to 2GB.

Ramesh Yankati
  • 1,197
  • 9
  • 13