6/18/2006

Fix Javac java lang OutOfMemoryError

When javac is compiling a large number of java source files, it may fail with java.lang.OutOfMemoryError:

The system is out of resources.
Consult the following stack trace for details.
java.lang.OutOfMemoryError: Java heap space
It's no different than OutOfMemoryError in other java applications. When you run javac in Sun JDK, it's invoking com.sun.tools.javac.main.Main located in %JAVA_HOME%\lib\tools.jar.

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>
Setting fork to true will also limit any memory leaks in javac implementation to its own child process, without affecting the parent Ant process.

If setting fork, memoryInitialSize, and memoryMaximumSize still doesn't fix the problem, you can execute javac task several times, each javac compiling a subset of your source tree. But this should really be the last rescort, since you are now managing source code dependency, which should be javac's business. You will need to decide which modules get compiled first, and classes in certain modules cannot have direct references to classes in certain other modules, and so on. I'd rather increase the memoryMaximumSize to 2g.

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.

If you are invoking javac directly, you can also increase the heap size for the underlying JVM:
javac -d build/classes -classpath ... -J-Xms256m -J-Xmx256m java-source-files

50 comments:

Ritesh said...

Thanks for such a nice article

vijaya said...

Hi.. it was a very helpful article

Anonymous said...

Thank you! this has helped me out.

Anonymous said...

This solved my problam thanks!!

AntirisK said...

Saved a weekend!!!

Thanks much.

Randolph said...

Saved me an important portion of my life...

You sir, are a hero.

Anonymous said...

it is a good article.
thanks. u have saved me sometime. thanks a lot

Anonymous said...

it helped. thanks a lot

Anonymous said...

Thanks a lot. It worked.

Cory said...

I was already using the fork attribute. I added the memory attibutes and it worked like a champ!

Anonymous said...

Thanks for the wonderful article that helped us fix the issue.

Anonymous said...

Thanks for help.

Anonymous said...

Good god, you saved my life!

Anonymous said...

Keep posting such good article

Anonymous said...

Your the man

Andrea said...

Just great... I had 1486 source files and everytime I tried I got that error. Thanks for the solution, I didn't find it anywhere else

Anonymous said...

Thanks a lot.....

Anonymous said...

I changed my system, (any excuse really) but the new super machine also threw that exception. Found your article and smooth sailing.

Anonymous said...

Thanks for you comments..It helped to fixing my ant build problem

RaM said...

Thanks a lot!!! Very crisp article.

Kathleen said...

I still hit the same error even after setting to fork="true" memoryinitialsize="1024m" memorymaximumsize="1024m"
and set ANT_OPTS... :(

fredley said...

Excellent and to the point.
Thank you!

praveen said...

Thanks this solved my problem too.

Anonymous said...

If you're having the same problem as Kathleen, and you're running Eclipse, go into your "run As" dialog for your Build.xml and add -Xms256m -Xmx256m to the "VM Arguments" text box. I couldn't get the strategies in this article to work... However, this is still the most comprehensive and concise article I was able to find for this

123 123 said...

Great post you got here. It would be great to read a bit more about that matter.
BTW check the design I've made myself Overnight escort

Ajit Agarkar sucks said...

Great post. this solved my problem

-Hemang.

Shweta said...

Thanks a lot. Solved my problem. Nice article

Anonymous said...

wow..great solution..Thanks a lot.

rajesh said...

Thank u very nuch it helped me alot,

Can any one tell me wht if Our os is 2007 windows bcz it is based on 2003 server

Anonymous said...

Thanks .Good Article .It helped me a lot.

Anonymous said...

Thanks! this was resoleved my issue.

Laxman Surabhi.

Anonymous said...

Thank you so much. Solved my problems. Saved a lot time for me.

Harish said...

Thanks a lot , It worked perfectly fine.Searched so many forums no where they have mentioned this.

Eoan said...

Cheers, concise and useful. :)

Anonymous said...

Thank you very much :)

Anonymous said...

Thank a lot. Saved my day

Netbeans User said...

Thanks for the article. I ran into 'The system is out of resources'-Error while compiling a large Netbeans Web-Project.
Setting the -J-Xms/Xmx params as compiler options in the project settings saved my day.

Anonymous said...

Thanks so much.. I had fork="yes" in my build file, but reading this article i increased the memoryMaximumSize="1024m" and it got rid of heap error when building...

javabuddy said...

Very useful information, not many knows about it. I recently faced OutOfMemory issue while buiding my project with ANT but ANT gets its argument with some environment variable but I think I could have used this as well.

JP
10 point about Heap space in Java

Anonymous said...

ANT_OPTS did not work for me with a Weblogic deployment script.
The only thing that worked was to provide maxmemory="1536m"

Anonymous said...

I'm using Eclipse Helios/6.8 and increased the heap size in the eclipse.ini file but it made no difference, and Eclipse was showing heap size not coming close to the limit I set. I added fork=true and min/max size to javac in build.xml per your instructions and it worked! Thanks for your post oh so many years ago ..

-Robert

Padmaratna said...

Thanks a Tonne.
It solved my ant running problem.

Gangadhar said...

I still hit the same error even after setting to fork="true" memoryinitialsize="1024m" memorymaximumsize="1024m"
and set ANT_OPTS in ant.bat file as well as command prompt, no luck. i even tried VM args while running from Eclipse. can you please me suggest.

Thanks,
Gangadhar

Anonymous said...

thnks :)

Dhirendra said...

Thanks it was a life saver

Anonymous said...

If you are familiar with different generations on heap and How garbage collection works in java
and aware of new, old and permanent generation of heap space then you would have easily figured out
this OutOfMemoryError in Java. but another important thing to keep in mind is size of perm space
which is independente of maximum heap size , if you are working on large project you can easily
ran out of memory in this area even though you could have memory in other heap generations. you
can control this with JVM options "-XX:MaxPermSize".
source: how to solve java.lang.OutOfMemoryError in Java

MAHESH KUMAR SANKA said...

Thank you.This helped me a lot

Boyko Todorov said...

Hey thanks! This helped!

Dileep Donepudi said...

very useful article , Thanks it's saved my 2 days time ....

radha said...

Thanks it helped me to resolve the problem ...