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.It's no different than OutOfMemoryError in other java applications. When you run javac in Sun JDK, it's invoking
Consult the following stack trace for details.
java.lang.OutOfMemoryError: Java heap space
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"Setting fork to true will also limit any memory leaks in javac implementation to its own child process, without affecting the parent Ant process.
srcdir="${basedir}/src"
destdir="${basedir}/build/classes"
classpath="${project.classpath}"
includeantruntime="false"
memoryinitialsize="256m"
memorymaximumsize="256m">
<compilerarg line="-endorseddirs ${env.CATALINA_BASE}/endorsed" />
</javac>
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)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
setenv ANT_OPTS="-Xms256m -Xmx256m" (tcsh/csh)
set ANT_OPTS=-Xms256m -Xmx256m (Windows)
%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
Tags:
49 comments:
Thanks for such a nice article
Hi.. it was a very helpful article
Thank you! this has helped me out.
This solved my problam thanks!!
Saved a weekend!!!
Thanks much.
Saved me an important portion of my life...
You sir, are a hero.
it is a good article.
thanks. u have saved me sometime. thanks a lot
it helped. thanks a lot
Thanks a lot. It worked.
I was already using the fork attribute. I added the memory attibutes and it worked like a champ!
Thanks for the wonderful article that helped us fix the issue.
Thanks for help.
Good god, you saved my life!
Keep posting such good article
Your the man
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
Thanks a lot.....
I changed my system, (any excuse really) but the new super machine also threw that exception. Found your article and smooth sailing.
Thanks for you comments..It helped to fixing my ant build problem
Thanks a lot!!! Very crisp article.
I still hit the same error even after setting to fork="true" memoryinitialsize="1024m" memorymaximumsize="1024m"
and set ANT_OPTS... :(
Excellent and to the point.
Thank you!
Thanks this solved my problem too.
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
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
Great post. this solved my problem
-Hemang.
Thanks a lot. Solved my problem. Nice article
wow..great solution..Thanks a lot.
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
Thanks .Good Article .It helped me a lot.
Thanks! this was resoleved my issue.
Laxman Surabhi.
Thank you so much. Solved my problems. Saved a lot time for me.
Thanks a lot , It worked perfectly fine.Searched so many forums no where they have mentioned this.
Cheers, concise and useful. :)
Thank you very much :)
Thank a lot. Saved my day
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.
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...
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
ANT_OPTS did not work for me with a Weblogic deployment script.
The only thing that worked was to provide maxmemory="1536m"
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
Thanks a Tonne.
It solved my ant running problem.
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
thnks :)
Thanks it was a life saver
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
Thank you.This helped me a lot
Hey thanks! This helped!
very useful article , Thanks it's saved my 2 days time ....
Post a Comment