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. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
82 Comment threads
22 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
88 Comment authors
ParveshJTWHarryH.W.Darshanakavya Recent comment authors
newest oldest most voted
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 »

Lokesh
Guest
Lokesh

Thanks. It helped me.

mango
Guest
mango

Thanks buddy!!!

trackback
Manuel 2.0» Blog Archive » Sonar on Debian with Tomcat 6

[…] not even the deployment of Sonar worked. Thus, you have to change your “catalina.sh” according to this article. Search for the JAVA_OPTS variable and add these […]

Saeid Zebardast
Guest
Saeid Zebardast

Thank you :0

Anara
Guest
Anara

Thanks a lot! 🙂

Leon
Guest
Leon

Thank you so much!

Ryan
Guest
Ryan

you should not edit your catalina.sh file to shove these things in there – you should instead create a

“setenv.sh” file that catalina calls when it runs and put the export JAVA_OPTS=” ” line in that setenv.sh file.

Saurabh Tyagi
Guest
Saurabh Tyagi

Thank You sir

Gavin
Guest
Gavin

Thanks, very cool!

jhelbert
Guest
jhelbert

thank you… uffffffff.. many hours searching the solution

trackback
Grails + Tomcat ???????? – java.lang.OutOfMemoryError: PermGen space « NAzT's Blog
freediver
Guest
freediver

Excellent! Works well now!

Piter
Guest
Piter

uhmmmm, that’s great, but how can i do this in windows tomcat.
There is no catalina.sh file, just tomcat5.exe, dll, and jar files.

There is a Configure.. option in tomcat monitor, but I’m not sure how to configure this options.

kumudu
Guest
kumudu

Thank you.

Nesma
Guest
Nesma

Please help I added the above line i get this error while calling ./ahutdown.sh
Exception in thread “main” java.lang.NoClassDefFoundError: $/-Djava/awt/headless=true
Please reply thanks

Silvio Guedes
Guest
Silvio Guedes

Thnak buddy

Ravi
Guest
Ravi

Got to start->All programs->Apache TomCat->Configure Tomcat in that select Java Tab there you get java options change the java settings.

trackback
Tomcat – java.lang.OutOfMemoryError: PermGen space « Wateron's Blog
trackback
Tomcat 6 – java.lang.OutOfMemoryError: PermGen space « SCyASIG

[…] en internet encontré este sitio donde dán una solución. La memoria de mi computadora es menor a la mostrada ahi, asi que yo puse […]

Soumya
Guest
Soumya

Thanks!

Amila Nandana
Guest
Amila Nandana
 That's working.
Tom
Guest
Tom

I have “Error occurred during initialization of VM
Too small initial heap for new size specified”

What’s it that?

Mahmoud Saleh
Guest
Mahmoud Saleh

can you please edit the article and describe the parameters and what every parameter does, so people don’t just copy paste and understand what’s going on.

Adrian
Guest
Adrian

Great!!!
Thank you.

Anonymous
Guest
Anonymous

Before fixing OutOFMemoryError it’s good to know the real cause of it because fixing OOME on heap is entirely different than fixing it for PERM space. check this article for more details 2 Solution of OutOfMemoryError in Java

trackback
Increasing the memory for JBoss running space | lparisi79

[…] running Jboss it’s always ending with  an error , java.lang.OutOfMemoryError: PermGen space, i fixed it increasing the memory dedicated  to Jboss putting into the properties of the server […]

Tim
Guest
Tim

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… Read more »

Ryan Gardner
Guest
Ryan Gardner

You should use CATALINA_OPTS instead of JAVA_OPTS to set your heap size – because JAVA_OPTS is also used when you call “stop” – and it’s unlikely you need a huge heap to just shut down the app.

Ryan Gardner
Guest
Ryan Gardner

Oh, and you should create a file “setenv.sh” rather than messing with catalina.sh