Main Tutorials

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 Author

author image
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

Subscribe
Notify of
99 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Lokesh
8 years ago

Thanks. It helped me.

Ivo Mägi
9 years ago

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 cause and offering solution guidelines.

Full disclosure: I am affiliated with Plumbr.

JTW
4 years ago
Reply to  Ivo Mägi

often true, though in case of JSF and JSP applications it isn’t always that simple as the JSP compiler itself can easily run out of permgen space in some versions when you have highly complex pages.

Of course with the advent of Java 8 and newer, there’s no more permgen space (though you can now run out of system RAM in extreme cases).

Victor
10 years ago

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

Almir
10 years ago
Reply to  Victor

Victor said what I want to say!!!

Thank you!!!

Parvesh
4 years ago

Awesome article.. nicely explained.

Harry
5 years ago

Can someone clarify what is meaning of Xms,Xmx, PermSize and MaxPermSize so I can configure server accordingly.

H.W.Darshana
5 years ago

Thanks. It s Work.

kavya
6 years ago

Thank yo so much it works for me 🙂

chandrashekhargoka
6 years ago

Nice Explanation

akir
7 years ago

Thanks for this detailed artcile..

Waseem
8 years ago

You Saved my life 🙂 Stay blessed 🙂

sumit
9 years ago

amazing solution

Guillermo
9 years ago

Thanks for all the site and sharing your knowledge

Atul Sachan
9 years ago

Thanks mkyong! Thanks a lot..

chidza
9 years ago

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

krishna
9 years ago

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

jMind
9 years ago

Will this work if tomcat runs as service?

Robik Shrestha
9 years ago

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

Bob
10 years ago

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

may215
10 years ago

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

Andrei
10 years 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.

Mrinmoy Sarkar
10 years ago

Works for me. Thank you very much.

Dean Schulze
10 years ago

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

mkyong
10 years ago
Reply to  Dean Schulze

Article is updated, thanks.

Venugopalan Govindan
9 years ago
Reply to  mkyong

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

gnom1gnom
10 years ago
Reply to  Dean Schulze

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

Aditya
10 years ago

Thank you

kon
10 years ago

not working with me. 🙁

Venugopalan Govindan
9 years ago
Reply to  kon

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

Ayas
10 years 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.

Yama Farooq
10 years ago
Reply to  Ayas

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
10 years 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”

Goycochea
10 years ago

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

Anita
10 years ago
Reply to  Goycochea

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

Garima
10 years ago

Thanks a ton! Worked very well for me! 🙂

WiN
10 years ago

Thanks!!
It worked for me 🙂