tags:

views:

501

answers:

2

I'm running into 'Error: Java heap space, java.lang.OutOfMemoryError: Java heap space' when compiling an existing project on OS X (Snow Leopard).

I am using the default Java 1.6 64-bit SDK, and default install of Ant (both are provided as part of the OS).

I have encountered this issue in other OS environments and normally solve it by using ANT_OPTS to increase the allocated memory but this does not appear to be working.

I tried setting ANT_OPTS as follows:

In the same shell I entered

export ANT_OPTS="-Xmx1024m' ant build

I also tried setting the value in ~/.bash_profile (which I sourced to ensure it was picked up).

I eventually solved the memory problem by increasing the memory in the javac task in my Ant script, but this is not ideal (I would much rather use ANT_OPTS).

What is the correct way to increase the amount on Heap memory when running Ant in OS X?

UPDATE:

It appears I was wrong and ANT_OPTS is being picked up as excepted. The problem is that I am forking the javac task (as described by bkail below).

Which leads me to my next question. Why does the JAVAC process keep running out of memory on OSX when the same code/ant script works fine on Linux? What is different about the default OSX install of Java that means it requires additional memory allocated?

+2  A: 

If the javac memoryMaximumSize setting is having any effect, then your javac must be fork="true", which means the javac JVM is separate from the Ant JVM, which means your ANT_OPTS will have no effect on the javac JVM. You'll need to manually propagate the -Xmx setting. Something like this:

<project>
  <script language="javascript"><![CDATA[
    importClass(java.lang.Runtime)
    project.setProperty("memoryMaximumSize", Runtime.getRuntime().maxMemory());
  ]]></script>
  <javac srcdir="." memoryMaximumSize="${memoryMaximumSize}" fork="true"/>
</project>

ant -verbose confirms the setting is being propagated to the forked javac:

[javac] Compilation arguments:
[javac] '-J-Xmx520290304'
bkail
+1  A: 

To answer the updated question, the default maximum heap varies depending on whether the JVM classifies the system as "client" or "server". If classified as "server", then the default maximum heap varies depending on the available memory on the system. See here for more information:

http://java.sun.com/docs/hotspot/gc5.0/ergo5.html

java -version should show which VM is being selected:

java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)
bkail