Tomcat – java.lang.OutOfMemoryError: PermGen space
Tomcat production server sometime will hit 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 when the Tomcat start and stop few times. It’s just funny, however you can fine tune it with some minor changes in the Tomcat configuration setting. By default, Tomcat assigned very little memory for the running process, you should increase the memory by make change in catalina.sh or catalina.bat file.
How to fix it?
1) Find where is Cataline.sh located. We need to make some changes in “catalina.sh” file.
P.S Cataline.sh is located at \tomcat folder\bin\catalina.sh
2) Assign following line to JAVA_OPTS variable and add it into catalina.sh file.
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"
Partial example of the catalina.sh file
# JSSE_HOME (Optional) May point at your Java Secure Sockets Extension # (JSSE) installation, whose JAR files will be added to the # system class path used to start Tomcat. # # CATALINA_PID (Optional) Path of the file which should contains the pid # of catalina startup java process, when start (fork) is used # # $Id: catalina.sh 609438 2008-01-06 22:14:28Z markt $ # ----------------------------------------------------------------------------- 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" # OS specific support. $var _must_ be set to either true or false. cygwin=false os400=false darwin=false case "`uname`" in CYGWIN*) cygwin=true;; OS400*) os400=true;; Darwin*) darwin=true;; esac # resolve links - $0 may be a softlink PRG="$0"
3) Done. Restart Tomcat.
Oh, and you should create a file “setenv.sh” rather than messing with catalina.sh
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.
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/fkieviet/entry/classloader_leaks_the_dreaded_java
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…
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.
[...] 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 [...]
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
Great!!!
Thank you.
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.
and also how to check that the memory increased already on windows or linux ?
I have “Error occurred during initialization of VM
Too small initial heap for new size specified”
What’s it that?
Is increase the heap size didn’t work?
Thanks!
[...] 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 [...]
[...] http://www.mkyong.com/tomcat/tomcat-javalangoutofmemoryerror-permgen-space/ LikeBe the first to like this [...]
Got to start->All programs->Apache TomCat->Configure Tomcat in that select Java Tab there you get java options change the java settings.
Thnak buddy
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
Thank you.
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.
Not familiar with the Window version, cant find the Java options in the “conf” folder as well.
But you can run the “tomcat6w.exe”, click on the “Java” tab, and configure the value in the Java Options field.
add in the bin\startup.bat file
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”
Excellent! Works well now!
[...] http://www.mkyong.com/tomcat/tomcat-javalangoutofmemoryerror-permgen-space/ [...]
thank you… uffffffff.. many hours searching the solution
Thanks, very cool!
Thank You sir
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.
you have your point, thanks for sharing
Thank you so much!
Thanks a lot!
Thank you :0
[...] 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 [...]
Thanks buddy!!!
Thank buddy