Professional Documents
Culture Documents
Tomcat - Java - Lang
Tomcat - Java - Lang
Home All Tutorials Java Core JSF Spring Hibernate Struts Android Others Search
Popular Tutorials
It’s usually happened after the Tomcat restarts a few times.
Android Tutorial
In the next step, we will show you how to set the VM options in Tomcat, under Windows and Linux environment.
2. Windows
Tomcat is managed by this script file catalina.bat, dive inside the script, you will find out that catalina.bat always find
and run the setenv.bat file to set the environment variables.
{$tomcat-folder}\bin\catalina.bat
//...
rem Get standard environment variables
if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone
//...
2.1 To set the environment variable on Windows, create a setenv.bat manually, and put it into the ${tomcat-folder}\bin
folder.
${tomcat-folder}\bin\setenv.bat
2.2 Restart Tomcat, it will call the setenv.bat file to set the environment variable automatically.
{$tomcat-folder}\bin\catalina.bat restart
3. Linux
On Linux, the process is same, just Tomcat is using catalina.sh and setenv.sh instead.
3.1 Find out where is catalina.sh :
catalina.sh
3.2 Review the catalina.sh, script, it behaves like Windows, but use setenv.sh instead.
//...
# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in setenv.sh, in rare case when it is needed.
CLASSPATH=
if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
. "$CATALINA_BASE/bin/setenv.sh"
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
. "$CATALINA_HOME/bin/setenv.sh"
fi
//...
3.3 Create a setenv.sh manually, and put it into the ${tomcat-folder}\bin\ folder.
${tomcat-folder}\bin\setenv.sh
Note
The heap size and non-heap size (perm gen) value is just an example, you should change the value accordingly to
suit your project needs.
References
1. Configure Tomcat Memory Settings
2. Oracle : Presenting the Permanent Generation
3. Useful JVM Flags – Part 5 (Young Generation Garbage Collection)
4. How To Install Tomcat On Ubuntu
5. Find Out Your Java Heap Memory Size
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him
on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity,
thanks.
This issue is far more complicated than your article acknowledges. For some people, they do not have a
PermGen memory leak and simply have more classes to load into PermGen than their memory settings allow,
and your solution works correctly (and is a true solution) for them.
However, for many people, they are getting PermGen errors because they have an actual PermGen memory
leak (yes, it's possible in Java, just not in quite the same way as in C++) that becomes apparent following a hot
re-deploy, and your solution will delay, BUT NOT PREVENT their problem. In those situations, the memory leak
occurs because classes loaded by the webapp's classloader are referenced by some object loaded in a
different classloader, and so cannot be garbage-collected after the webapp is undeployed/reloaded, or because
their webapp leaves one or more threads running when it is stopped. A very good explanation of the problem
can be found here: http://blogs.oracle.com/fkievi...
For people with this kind of memory leak, your configuration settings will make it look like the problem has been
fixed (for a while), and then after a certain number of hot re-deploys, it will occur again. So they'll leave your blog
thinking you're the man for solving their problems (they do, from the comments), only to discover later that your
solution didn't actually work, and they'll no longer think so highly of you. So you should probably modify the post
to include more nuance in your explanation, so people don't think badly of you for giving them information that
turned out not to be right...
5 • Reply • Share ›
To be frankly, i don't mind how people think of me, and believe people should learn through sharing, and
your provided Oracle classloader leaks link is really useful.
2 • Reply • Share ›
Dear MKyong, in the last 2 days i had to solve several problems with my project, and found a great part of the
solutions in this site. Congratulations for maintaining such a useful place and thank you so much for sharing
your knowledge.
3 • Reply • Share ›
Serious error in your instructions for catalina.bat. You can't have the quotes when you set JAVA_OPTS=... in
catalina.bat.
3 • Reply • Share ›
Thanks
1 • Reply • Share ›
Excellent!
My two cents is that I had to apply JAVA_OPTS with setenv.sh script into CATALINA_BASE/bin:
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8
-server -Xms1536m -Xmx1536m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
it is working!
1 • Reply • Share ›
MYKONG if i were a girl i'd say your fine. You have saved me a dozen times.
1 • Reply • Share ›
On windows(tomcat 7), just write: set JAVA_OPTS=-Xms128m -Xmx192m fixed the provlem
• Reply • Share ›
Thank you
• Reply • Share ›
Great, thanks! Worked like a charm. Before reading this post, I was going through the hundreds of lines in the
catalina script to find out which variable would best fit the JVM arguments.
• Reply • Share ›
The above solutions (setting PermSize & MaxPermSize) sometimes don't help while doing a hot deploy or
redeploy of a web application in tomcat. I could see PermGenSpace:OutOfMemory error while doing that in my
tomcat.
In catalina.bat file. can i simplay PAST these line or its 1st need to "SET"
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8
-server -Xms1536m -Xmx1536m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
• Reply • Share ›
Plz , how to change the “Xms” and “PermSize” value on Apache Tomcat 7 ?
• Reply • Share ›
Thanks!!
It worked for me :)
• Reply • Share ›
Thanks a lot.
I was wondering is there any way to set it through a java config class for a java web application so that we just
have to copy paste the war into any tomcat installations.
• Reply • Share ›
Thanks!!! This is the n-th time you save me ;). Keep up the good work!
• Reply • Share ›
Thank you!!!
• Reply • Share ›
My server has only 1024 MB Memory. What configuration do u suggest ? Am confused about 1536m !
• Reply • Share ›
Thanks a lot!!!
• Reply • Share ›
Thanks,
For all those who use the installer version of tomcat. You can set it(java_opts) through running tomcat6w.exe
(configuration manager) and under the java tab, in the java/jvm options textbox.
• Reply • Share ›
Thanks bilion.
• Reply • Share ›
Thanks,problem solved.
• Reply • Share ›
It is just perm gen error! Why are you changing java.awt.headless, file.encoding and java heap size? This
arguments is de trop.
• Reply • Share ›
Hello, thanks for sharing solutions. Your blog saved lots of time for me.
Million Thanks..
• Reply • Share ›
Thanks for the solution. It may great for the Tomcat family with who want to learn.
• Reply • Share ›
Fun With Puzzles • 2 years ago
Thanks!
I was struggling with this error from last week. This article clearly explains why this error is coming and how to
avoid it. I will try suggested solution. Hope this error will get fixed.
• Reply • Share ›
Above thing does not work when you use tomcat with eclipse "(
• Reply • Share ›
Oh, and you should create a file "setenv.sh" rather than messing with catalina.sh
• Reply • Share ›
Java – Append values into an Object[] array Run Spring batch job with
2 comments • a month ago CommandLineJobRunner
Umashankar — System.arraycopy widely used in 2 comments • 4 months ago
internals of Java datastructures (collections). More Matt Kindzerske — This example is good, but there
over some calculations are done in bitwise … are many errors with your downloadable source: pom
is describing different finalName than your …
Java Core Technologies : Android Developer Java Code Geeks Mkyong.com is a weblog dedicated to Java/J2EE
Java I/O, Java RegEx, Java XML, Java JSON, JDBC, Google App Engine using Java PHP Tutorials developers and Web Developers. We constantly
Java Misc publish useful tricks, tutorials on J2EE or web
DZone - Fresh Links TenthOfMarch
J2EE Frameworks : development.
Official Java EE 5 Tutorial Web Security Blog
Hibernate, JSF 2.0, Spring Core, Spring MVC, Official Java EE 6 Tutorial Web Development
All examples are simple, easy to read, and full
Spring Security, Spring MongoDB, Spring
Spring 2.5.x documentation Cédric Beust (TestNG) source code available, and of course well tested in
BatchApache Wicket, Struts 1.x, Struts 2.x
Spring 3.2.x documentation our development environment.
Web Service :
Hibernate core documentation
JAX-WS (SOAP), JAX-RS (REST)
Java SE 6.0 API documentation We're Social
Build Tools : 1. Twitter - Follow Me
Java EE 6.0 API documentation
Maven, Archiva 2. Facebook - Like Me
Java Secure Socket Extension
Unit Test Frameworks : 3. Google Plus - Add Me
(JSSE) Reference Guide
jUnit, TestNG 4. RSS - Subscribe Me
JSP home page
Others :
Android, Google App Engine, jQuery, Java JSF home page