Tomcat – java.lang.OutOfMemoryError: PermGen space

Often time, Tomcat may hits the following java.lang.OutOfMemoryError: PermGen space error.


java.lang.OutOfMemoryError: PermGen space
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

It’s usually happened after the Tomcat restarts a few times.

1. Solution

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


-XX:PermSize<size> - Set initial PermGen Size.
-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

About the Author

author image
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 make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Lokesh
Guest
Lokesh

Thanks. It helped me.

Ivo Mägi
Guest
Ivo Mägi
After working with applications dying with OutOfMemoryErrors for three years, I can say that adding more PermGen to application servers is 95% of the cases just masking the underlying symptoms instead of solving the actual problem. Most often the java.lang.OutOfMemoryError: Permgen space is triggered during redeploys when buggy code blocks classloaders from being garbage collected. There are loads of bugs in 3rd party libraries triggering it – JDBC drivers, threadpool implementations, job schedulers, you name it. There are several ways to find the offending code heap dumps, profilers or specialized tools such as https://plumbr.eu directly pinpointing you to the root… Read more »
kavya
Guest
kavya

Thank yo so much it works for me :)

chandrashekhargoka
Guest
chandrashekhargoka

Nice Explanation

akir
Guest
akir

Thanks for this detailed artcile..

Waseem
Guest
Waseem

You Saved my life :) Stay blessed :)

sumit
Guest
sumit

amazing solution

Guillermo
Guest
Guillermo

Thanks for all the site and sharing your knowledge

Atul Sachan
Guest
Atul Sachan

Thanks mkyong! Thanks a lot..

chidza
Guest
chidza

Thank you MKyong, i spent 6 hours trying to solve this tomcat server startup problem.

krishna
Guest
krishna

how to do the same in tomact8 where catalina.sh is not availbale? Could any one respond?

jMind
Guest
jMind

Will this work if tomcat runs as service?

Robik Shrestha
Guest
Robik Shrestha

Isn’t it better to use CATALINA_OPTS though? Even shutdown would use JAVA_OPTS.

Bob
Guest
Bob

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

Venugopalan Govindan
Guest
Venugopalan Govindan
may215
Guest
may215

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

Andrei
Guest
Andrei

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.

Mrinmoy Sarkar
Guest
Mrinmoy Sarkar

Works for me. Thank you very much.

Dean Schulze
Guest
Dean Schulze

Serious error in your instructions for catalina.bat. You can’t have the quotes when you set JAVA_OPTS=… in catalina.bat.

gnom1gnom
Guest
gnom1gnom

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

mkyong
Guest
mkyong

Article is updated, thanks.

Venugopalan Govindan
Guest
Venugopalan Govindan

No Sir, doesn’t help.

Please check,

http://www.12robots.com/index.cfm/2010/10/8/Giving-more-memory-to-the-Tomcat-Service-in-Windows

which solved our problem..

Aditya
Guest
Aditya

Thank you

kon
Guest
kon

not working with me. :(

Venugopalan Govindan
Guest
Venugopalan Govindan

Please visit,

http://www.12robots.com/index.cfm/2010/10/8/Giving-more-memory-to-the-Tomcat-Service-in-Windows

which solved our problem – when tomcat runs as a windows service

GOWRI SANKAR
Guest
GOWRI SANKAR

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.

Ayas
Guest
Ayas

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.

Yama Farooq
Guest
Yama Farooq

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 .

Awanish yadav
Guest
Awanish yadav

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”

Goycochea
Guest
Goycochea

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

Anita
Guest
Anita

-XX:PermSize=###m -XX:MaxPermSize=###m

Garima
Guest
Garima

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

WiN
Guest
WiN

Thanks!!
It worked for me :)

Bhaskar
Guest
Bhaskar

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.

Dilip Shah
Guest
Dilip Shah

Very helpful… thanks!

Victor
Guest
Victor

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

Almir
Guest
Almir

Victor said what I want to say!!!

Thank you!!!

Teerasak
Guest
Teerasak

Thanks