Ming, I had the same problem as you.  I found 2 solutions.

1) Increase the memory partition in the java command that launches Ant.  You
have already seen messages to the effect.  I found that I could compile all
of my files with a 75mb heap, your mileage may vary.  I added these flags:
"-Xms75m -Xmx75m" for the Sun 1.3 jdk that we use.

2) If requiring that much memory to compile is a concern, then use the
foreach task to compile the code a directory at a time.  I wrote the
following tasks to do this, and it works with the default memory heap.
Start by calling the target build_all_by_dir, and then if compiles all of
the .java files, a directory at a time.

    <!-- ===================================================================
-->
    <!-- build_all_by_dir target
-->
    <!-- ===================================================================
-->
    <taskdef name="foreach"
classname="org.apache.tools.ant.taskdefs.optional.ForeachTask"/>
    <target name="build_all_by_dir" depends="init, prepare_dirs">
        <antcall target="build_each_dir">
            <param name="parentDir" value=""/>
            <param name="srcDir" value="${mySrcSir}"/>
            <param name="destDir" value="${myDestDir}"/>
            <param name="classPath" value="${myClasspath}"/>
            <param name="debug" value="${debug}"/>
            <param name="optimize" value="${optimize}"/>
        </antcall>
    </target>

    <!-- ===================================================================
-->
    <!-- build_each_dir target
-->
    <!-- ===================================================================
-->
    <target name="build_each_dir">
        <foreach target="build_each_compile" type="dir">
            <param name="parentDir">
                <item value="${parentDir}"/>
            </param>
            <param name="srcDir">
                <item value="${srcDir}"/>
            </param>
            <param name="destDir">
                <item value="${destDir}"/>
            </param>
            <param name="classPath">
                <item value="${classPath}"/>
            </param>
            <param name="debug">
                <item value="${debug}"/>
            </param>
            <param name="optimize">
                <item value="${optimize}"/>
            </param>
            <param name="src.subdir">
                <fileset dir="${parentDir}" includes="*"/>
            </param>
        </foreach>
    </target>
    
    <!-- ===================================================================
-->
    <!-- build_each_compile target
-->
    <!-- ===================================================================
-->
    <target name="build_each_compile">
        <echo message="checking: ${parentDir}${src.subdir}"/>
        <javac srcdir="${srcDir}"
               destdir="${destDir}"
               includes="${parentDir}${src.subdir}/*.java"
               classpath="${classPath}"
               optimize="${optimize}">
            <patternset excludesfile="${srcDir}/build_excludes" />
        </javac>
        <antcall target="build_each_dir">
            <param name="parentDir" value="${parentDir}${src.subdir}/"/>
        </antcall>
    </target>


It is very verbose with the output, and I am not sure it is a good argument
for including foreach functionality in Ant, but it works.  I tried to
combine these three tasks into a smaller number, but didn't have much
success and ran out of time.  If someone out there can find a way to reduce
them, I would love to hear about it.  I really dislike that I have to
constantly set the same params over and over.

To use these tasks, you will need to include the ForeachTask class in your
ant classpath.  You can find the source here:
http://marc.theaimsgroup.com/?l=ant-dev&m=98838238130156&w=2

Hope this helps, and if anyone has any comments or suggestions, I am all
ears.

-Mark

-----Original Message-----
From: Ming-Fang Wang [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 06, 2001 5:26 AM
To: [EMAIL PROTECTED]
Subject: java compilation out of memory problem




Hi,

I run javac task on Ant to compile about 1250 java files. It starts
fine, but after compiling several java programs ( about 20 of them) I
got a run-out-of-memory error. The message also suggests using a
switch  -J-mx<nmbs>. I cannot find any document mentioning such a
switch. Has any one encountered such a situation and got a solution to
this problem?

Thanks.

-Ming

Reply via email to