Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Advertise Contact Us Write For Us RSS feed Donate

Home All Tutorials Java Core JSF Spring Hibernate Struts Android Others Search

Tomcat – Java.Lang.OutOfMemoryError: PermGen Recent Posts

Space Eclipse - SimpleTagSupport was not found


on the Java Build Path
Posted on October 23, 2008 , Last modified : March 12, 2014
Java - Get nameservers of a website
By mkyong

jConsole - JMX remote access on Tomcat


Often time, Tomcat may hits the following java.lang.OutOfMemoryError: PermGen space error.
Eclipse + Tomcat -
java.lang.OutOfMemoryError: Java heap
java.lang.OutOfMemoryError: PermGen space space

at java.lang.ClassLoader.defineClass1(Native Method) Grep for Windows - findstr example


at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

Popular Tutorials
It’s usually happened after the Tomcat restarts a few times.
Android Tutorial

JSF 2.0 Tutorial


1. Solution
By default, Tomcat is assigned a very little PermGen memory for the running process. To fix it, increase the PermGen
Spring Tutorial
memory settings by using the following Java VM options.
Maven Tutorial

-XX:PermSize<size> - Set initial PermGen Size.


Hibernate Tutorial
-XX:MaxPermSize<size> - Set the maximum PermGen Size.

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

set JAVA_OPTS=-Dfile.encoding=UTF-8 -Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m

P.S No double quotes, set JAVA_OPTS={value}.

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

$ sudo find / -name "catalina.sh"


Password:
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/Users/mkyong/Downloads/apache-tomcat-6.0.35/bin/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

export JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m"

P.S With double quotes, export JAVA_OPTS=”{value}”.

3.4 Restart Tomcat.

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

Tags : heap size perm gen tomcat

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.

Recommended Video Tutorials :

The Java Spring Tutorial


A guide to the basics of the Java Spring framework, including web programming with Spring MVC and
Hibernate.
1644 students

Servlets and JSPs: Creating Web Applications With Java


Learn how to create dynamic websites using the Java programming language with this java web application
tutorial.
3970 students

Related Posts Popular Posts


Eclipse + Tomcat – java.lang.OutOfMemoryError: Top 8 Java People You Should Know
Java heap space Top 20 Java Websites
Find out your Java heap memory size Top 5 Free Java eBooks
jConsole – JMX remote access on Tomcat Top 10 Java Regular Expression Examples
Eclipse – java.lang.OutOfMemoryError: Java heap Top 5 Open Source Q&A Systems
space
Eclipse IDE – Tomcat version 6.0 only supports
J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules

You might also like following tutorials :

78 Comments Mkyong.com Login

Sort by Best Share Favorite

Join the discussion…

Tim • 2 years ago

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 ›

mkyong Tim • 2 years ago


Thanks for sharing your invaluable comment and also the Oracle link.

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 ›

Jaa Jehova mkyong • 2 years ago


SEVERE: Exception invoking method check
java.lang.OutOfMemoryError: PermGen space
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2818)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1148)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1643)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2818)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1148)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1643)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
• Reply • Share ›

Andrei • 2 months ago

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 ›

Dean Schulze • 3 months ago

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 ›

gnom1gnom Dean Schulze • 2 months ago


For windows it should be
set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m -
XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -
XX:+DisableExplicitGC
1 • Reply • Share ›

mkyong Mod Dean Schulze • 16 days ago


Article is updated, thanks.
• Reply • Share ›

kon • 4 months ago

not working with me. :(


1 • Reply • Share ›

Teerasak • 7 months ago

Thanks
1 • Reply • Share ›

Miguel Palma • a year ago

Excellent!

Thank you very much!

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 ›

mkyong Mod Miguel Palma • 16 days ago


Article is updated with "setenv.sh" example.
• Reply • Share ›

Flip • 8 months ago

MYKONG if i were a girl i'd say your fine. You have saved me a dozen times.
1 • Reply • Share ›

Bob • 16 days ago

There isn't a catalina.bat in my Windows Tomcat 7 install


• Reply • Share ›

may215 • a month ago

On windows(tomcat 7), just write: set JAVA_OPTS=-Xms128m -Xmx192m fixed the provlem
• Reply • Share ›

Mrinmoy Sarkar • 2 months ago

Works for me. Thank you very much.


• Reply • Share ›

Aditya • 3 months ago

Thank you
• Reply • Share ›

GOWRI SANKAR • 4 months ago


GOWRI SANKAR • 4 months ago

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 ›

Ayas • 4 months ago

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.

Got resolved by adding -XX:+UseConcMarkSweepGC and -XX:+CMSClassUnloadingEnabled parameters to


the tomcat startup script along with the other parameters. -XX:+CMSClassUnloadingEnabled parameter clears
the PermgenSpace which is a non-heap memory.
• Reply • Share ›

Yama Farooq Ayas • 3 months ago


Hello Ayas, I have the same problem. I try mkyong post and it help for few days. could you please help
me with providing some more information how to do that. I am new with linx .
• Reply • Share ›

Awanish yadav • 5 months ago

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 ›

Goycochea • 6 months ago

Plz , how to change the “Xms” and “PermSize” value on Apache Tomcat 7 ?
• Reply • Share ›

Anita Goycochea • 5 months ago


-XX:PermSize=###m -XX:MaxPermSize=###m
• Reply • Share ›

Garima • 7 months ago

Thanks a ton! Worked very well for me! :)


• Reply • Share ›

WiN • 7 months ago

Thanks!!
It worked for me :)
• Reply • Share ›

Bhaskar • 7 months ago

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 ›

Dilip Shah • 7 months ago

Very helpful... thanks!


• Reply • Share ›

Victor • 7 months ago

Thanks!!! This is the n-th time you save me ;). Keep up the good work!
• Reply • Share ›

Almir Victor • 7 months ago


Victor said what I want to say!!!

Thank you!!!
• Reply • Share ›

Bineesh • 8 months ago

My server has only 1024 MB Memory. What configuration do u suggest ? Am confused about 1536m !
• Reply • Share ›

Naveen • 9 months ago

Thanks a lot!!!
• Reply • Share ›

jayaraj • 10 months ago


jayaraj • 10 months ago

i could not found the catalina.sh or catalina.bat file. wat should i do


• Reply • Share ›

ptr jayaraj • 9 months ago


tomcat7/bin/catalina.sh
• Reply • Share ›

Kathiresh • 10 months ago

Thanks a lot!!!!It's working for my ubuntu server..


• Reply • Share ›

Ouadi • 10 months ago

Thanks,

But what's the default memory size configuration in Tomcat7 ?


• Reply • Share ›

Rajib Ghosh • 11 months ago

Bravo !!! Works for me . Thank you very much!!!


• Reply • Share ›

Nitin • 11 months ago

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 ›

Renu Nitin • 10 months ago


That was useful for me. Thanks Nitin.
Thanks MKYong
• Reply • Share ›

Mr Hai • a year ago

mkyong verry verry great, i love you.


• Reply • Share ›

Mr Hai • a year ago

Thanks bilion.
• Reply • Share ›

Umar Faisol • a year ago

Thanks,problem solved.
• Reply • Share ›

Anton • a year ago

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 ›

Victor • 2 years ago

Hello, thanks for sharing solutions. Your blog saved lots of time for me.

There is strange thing in your example:


"-Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m"
1) memory heap size for new objects limited by 256Mb
2) memory heap size for permanent objects limited by 256Mb
So we have 500Mb reserved for objects. And it's not clear how rest 1000Mb will be used?
• Reply • Share ›

Sunil • 2 years ago

Million Thanks..
• Reply • Share ›

Marcos • 2 years ago

Thanks a lot Dude...


• Reply • Share ›

soni • 2 years ago

thanks .... it helped a lot.


• Reply • Share ›

paran • 2 years ago

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 ›

Mlungisi Sincuba • 2 years ago

Thanks, it's working fine for me.


• Reply • Share ›

Nilesh • 2 years ago

Above thing does not work when you use tomcat with eclipse "(
• Reply • Share ›

Ryan Gardner • 2 years ago

Oh, and you should create a file "setenv.sh" rather than messing with catalina.sh
• Reply • Share ›

Load more comments

ALSO ON MKYONG.COM WHAT'S THIS?

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 …

Wicket multiple checkboxes example – TestNG + Spring Integration Example


CheckBoxMultipleChoice 4 comments • 2 months ago
1 comment • 4 months ago Avner — Note that assertEquals parameters are
Suresh Akella — Hi,I am trying to get the selected reversed: first put the expected value and then the
values on AjaxLink submit but it is not working. I need actual, and not the other way around
Ajax Behaviour because I have to redisplay …

Subscribe Add Disqus to your site

All Available Tutorials Favorites Links Friends & Links About Us

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

MongoDB, Quartz Scheduler Eclipse IDE for Java developer


Struts 1.3 documentation
Struts 2.2 documentation
Maven home page
Maven central repository Search
Java.Net Maven repository
Ant home page
JAX-WS Official Website
JAX-RS Official Website (Jersey)
MongoDB Official Website

Copyright © 2008-2014 Mkyong.com, all rights reserved.

You might also like