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.
//...
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.
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
:
$ 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.
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.
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.
Thanks. It helped me.
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.
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).
Thanks!!! This is the n-th time you save me ;). Keep up the good work!
Victor said what I want to say!!!
Thank you!!!
Awesome article.. nicely explained.
Can someone clarify what is meaning of Xms,Xmx, PermSize and MaxPermSize so I can configure server accordingly.
Thanks. It s Work.
Thank yo so much it works for me 🙂
Nice Explanation
Thanks for this detailed artcile..
You Saved my life 🙂 Stay blessed 🙂
amazing solution
Thanks for all the site and sharing your knowledge
Thanks mkyong! Thanks a lot..
Thank you MKyong, i spent 6 hours trying to solve this tomcat server startup problem.
how to do the same in tomact8 where catalina.sh is not availbale? Could any one respond?
Will this work if tomcat runs as service?
Isn’t it better to use CATALINA_OPTS though? Even shutdown would use JAVA_OPTS.
There isn’t a catalina.bat in my Windows Tomcat 7 install
Please visit
http://www.12robots.com/index.cfm/2010/10/8/Giving-more-memory-to-the-Tomcat-Service-in-Windows
On windows(tomcat 7), just write: set JAVA_OPTS=-Xms128m -Xmx192m fixed the provlem
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.
Works for me. Thank you very much.
Serious error in your instructions for catalina.bat. You can’t have the quotes when you set JAVA_OPTS=… in catalina.bat.
Article is updated, thanks.
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..
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
Thank you
not working with me. 🙁
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
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.
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.
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 .
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”
Plz , how to change the “Xms” and “PermSize” value on Apache Tomcat 7 ?
-XX:PermSize=###m -XX:MaxPermSize=###m
Thanks a ton! Worked very well for me! 🙂
Thanks!!
It worked for me 🙂