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"
<compilerarg line="-endorseddirs ${env.CATALINA_BASE}/endorsed" />
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


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

Anonymous 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


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.

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 ..


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.


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


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 ...

sandeep said...

Thanks a lot for the clean article. Saved time.

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Jasmine Thompson said...

Com mais de 100 milhões de fãs, o Photo Grid é o criador de colagens fotográficas mais popular do mercado. Baixar Photo Grid para o conforto da máquina inovadora. Photo Grid baixar - download Photo Grid - Photo Grid gratis - Photo Grid - baixar Photo Grid - Photo Grid baixar

Alexander Sara said...

Thanks for sharing this quality information with us.
super smash flash 2 unblocked | super smash flash | super smash flash 2 |

usps tracking map said...

Thank you very much for the information you shared, it's all I've been looking for
slither io

happy wheels said...

Thanks for your article! I have been looking for quite a long time and fortunately I read this article! I wish you would continue to have valuable articles like this or more to share with everyone!

abdhul ece89 said...

your article is more informative, thanks for sharing your views and ideas....

Java Training in Chennai

house-hotel said...

Your blog is great. It’s very interesting

gowsalya said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

john jersy said...

Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
online Python training
python training in chennai

hansika hans said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.

Java training in Pune

Java interview questions and answers

Java training in Chennai | Java training institute in Chennai | Java course in Chennai

Java training in Bangalore | Java training institute in Bangalore | Java course in Bangalore

john brito said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject

rpa training in chennai |rpa course in chennai|
rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

lenin christ said...

Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
Top 250+AWS Interviews Questions and Answers 2019 [updated]
Learn Amazon Web Services Tutorials 2019 | AWS Tutorial For Beginners
Best AWS Interview questions and answers 2019 | Top 110+AWS Interview Question and Answers 2019
Best and Advanced AWS Training in Bangalore | Amazon Web Services Training in Bangalore
AWS Training in Pune | Best Amazon Web Services Training in Pune
AWS Online Training 2018 | Best Online AWS Certification Course 2018
Best Amazon Web Services Training in Pune | Best AWS Training in Pune

Riya Raj said...

Really great blog…. Thanks for your information. Waiting for your new updates.
Best JAVA Training in Chennai
Core Java training in Chennai
Advanced Java Training in Chennai
J2EE Training in Chennai
JAVA Training Chennai

Durai Raj said...

Perfect blog… Thanks for sharing with us… Waiting for your new updates…
Web Development courses in Chennai
Web development training in Chennai
Web Design Training Coimbatore
Web Designing Training Institute in Coimbatore
Web Development Training in Bangalore
Web Designing Training in Bangalore

armagedon said...

درمان بواسیر قطعی
درمان بواسیر با لیزر
هزینه درمان بواسیر با لیزر
علائم شقاق مقعدی
بواسیر چیست
بهترین دکتر بواسیر
علائم بواسیر
عمل جراحی بواسیر
درمان شقاق مقعدی
پزشک متخصص بواسیر
علائم بواسیر
هزینه جراحی بواسیر
درمان شقاق با لیزر
خون تازه در مدفوع
علت مشاهده خون در مدفوع

pooja said...

I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.
Java training in Chennai

Java training in Bangalore

Inigo joseph said...

Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
Python Online certification training
python Training institute in Chennai
Python training institute in Bangalore

Anexas Europe said...

I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic.Artificial Intelligence Training in Bangalore. Keep sharing your information regularly for my future reference.

Dale Morris said...

I just want to say thanks for your wonderful post, it is contain a lot of knowledge and information that i needed right now. You really help me out my friend, thanks!
Click here |Norton Customer Service
Click here |Mcafee Customer Service
Click here |Phone number for Malwarebytes
Click here |Hp printer support number
Click here |Canon printer support online

yashnit said...

Thanks For Sharing Your Information Please Keep UpDating Us Time Just Went On Reading The article The Information shared Is Very Helpful
Datascience Online Training Aws Online Training Python Online Training Devops Online Training