Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:15:18+00:00 2026-06-04T15:15:18+00:00

Using Ant tasks that create subprojects such as <antcall> and <ant> may cause build

  • 0

Using Ant tasks that create subprojects such as <antcall> and <ant> may cause build failures when invoked repeatedly due to one of the following errors:

  • java.lang.OutOfMemoryError: PermGen space
  • java.lang.OutOfMemoryError: Java heap space

The error only occurs when one of the tasks being called is defined using <typedef> or <taskdef> and does not appear when using tasks bundled with Ant such as <javadoc>.

Is there a way to avoid the OutOfMemoryError without increasing the
maximum Java heap size? Although increasing the heap size works for a
while, the problem still resurfaces if more memory intensive tasks are added.


The following example task and associated build.xml file cause an
OutOfMemoryError on my Linux box with the Java heap set to 10 MB (for
testing). The Ant task constructs a memory-hungry object (in this case a Guice
injector for a Closure Template Soy Module), which is then repeatedly called
using <antcall>.

 

CreateGuiceInjectorTask.java

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.template.soy.SoyModule;

import org.apache.tools.ant.Task;

/** Custom Ant task that constructs a Guice injector. */
public final class CreateGuiceInjectorTask extends Task {
  private Injector injector;

  public CreateGuiceInjectorTask() {
    injector = Guice.createInjector(new SoyModule());
  }

  public void execute() {
    System.out.println("Constructed Guice injector...");
  }
}

 

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="out-of-memory-test" basedir=".">

  <property name="build.dir" location="${basedir}/build" />
  <property name="CreateGuiceInjectorTask.jar"
      location="${build.dir}/CreateGuiceInjectorTask.jar" />

  <taskdef name="create-injector"
      classname="CreateGuiceInjectorTask"
      classpath="${CreateGuiceInjectorTask.jar}" />

  <target name="call-create-injector">
    <create-injector />
  </target>

  <target name="test"
      description="Create multiple injectors until out of memory">
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
    <antcall target="call-create-injector" />
  </target>
</project>

 

Test Output:

$ ant test

test:

call-create-injector:
[create-injector] Constructed Guice injector...

call-create-injector:
[create-injector] Constructed Guice injector...

...

call-create-injector:

BUILD FAILED
Could not create type create-injector due to java.lang.OutOfMemoryError: Java heap space
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:15:19+00:00Added an answer on June 4, 2026 at 3:15 pm

    Using Keith Gregory’s method described here I was able to work around the memory problem, at least for the simple example code you posted.

    The problem, in outline, is that each time you use a taskdef via an antcall Ant uses a different class loader, hence you eat up your permgen in no time. To confirm this you could modify your class to print the classloader hash code – you’ll see its different on each iteration.

    The work-around is to package your taskdef as an antlib and use the antlib namespace to load it. The upshot is that Ant’s own classloader is used. For this to work you must place the class on the Ant classpath.

    To test this I put your test class in a package namespace (called memtest), compiled, then added an antlib.xml in the package directory that looks like this:

    <antlib>
      <taskdef name="create-injector" classname="memtest.CreateGuiceInjectorTask" />
    </antlib>
    

    The buildfile project declaration was changed to

    <project name="out-of-memory-test" basedir="." default="test" xmlns:mt="antlib:memtest">
    

    and the target to

    <target name="call-create-injector">
      <mt:create-injector />
    </target>
    

    To test I put everything needed on the Ant classpath. It ran many antcalls successfully, and the debug print of the classloader hash code confirmed the expected single instance was used.

    There’s a note in the Ant documentation for the antlib namespace that says “the requirement that the resource is in the default classpath may be removed in future versions of Ant.” Depending on how that is implemented the work-around may break in future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working with JAX-WS with Weblogic Server, using their Ant tasks to build
In my ant build, I am using maven-ant-tasks to pull dependencies for my project.
I am trying to create a rpm package using ant task for that I
When declaring external ant tasks using taskdef, for instance ant-contrib, the proposed setup is
I have seen this question about deploying to WebSphere using the WAS ant tasks.
what's the point of using ant, maven, and buildr? won't the using build in
I'm trying to build my project using ANT. My project uses ActionbarSherlock, and Roboguice-Sherlock.
I'm trying to execute with Maven some test written using Ant tasks. I generated
I am trying to automate pde tests, using pde-maven-plugin, maven calls, or ant tasks.
I'm using Ant to build some Java projects. In some, I've got a lib/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.