Blogroll

Subscribe via email

Enter your email address:

Delivered by FeedBurner

Subscribe Now: Feed

Tuesday, May 29, 2007

PermGen OutOfMemory


If you are frustrated with Tomcat, JBoss, any other Java Application Server dying with an OutOfMemory Error every few times you redeploy your Java application code or if you have recently installed a new application in your Stable Java Environment and your Tomcat, JBoss is giving you hiccups by intermittently Crashing with OutOfMemory Errors , then keep on reading as I would like to share my thoughts with you as I have experienced the same issue.

You have checked that your Server has lot of Physical RAM, even JVM Heap Memory has been allocated a significant amount of Physical Memory, your Garbage Collection (GC) is tuned properly but you are still getting OutOfMemory error. I am not going to discuss about Java Virtual Machine (JVM) Memory model and Garbage Collection (GC) as this is out of the scope of this article. But you can read the details here

Introduction
SUN Java has a Generational garbage collector. Permanent Generation holds reflective data (meta-data) of the JVM itself, such as class and method objects. These reflective objects are allocated directly into the permanent generation, and it is sized independently from the other generations.[Ref: SUN Java website ]

The Error
SUN Java website says that the default size of PermGen (64 MB for Server) is adequate in most cases but programs that load many classes may need a larger permanent generation. When you hot deploy/redeploy your web application, the WAR file is unpacked and its class definition are loaded into the JVM. These classes are never unloaded and they end up in the Permanent Generation. Now, if you hot deploy your application few times or install a new web application with large code base that loads up lot of class files, the default PermGen space get quickly filled and is never Garbage Collected (by standard GC) causing java.lang.OutOfMemoryError: PermGen.

But I have large JVM Heap
It doesn't matter how high is your overall -Xmx heap setting and how much memory you have on the machine. PermGen size i.e. PermSize is additional to the -Xmx value on the JVM Options.

The FIX
There is no garbage collection by standard garbage collection in Permanent Generation. Although concurrent garbage collector can collect in PermGen but this is beyond the scope of this article. I would be rather focussing on increasing the PermGen size.
  1. Make sure your Server has enough Physical RAM (considering the allocation to JVM Heap -Xmx).
  2. Use larger PermGen size using the Options -XX:PermSize= and -XX:MaxPermSize=
  3. e.g. Set the maximum permanent generation size using -XX:MaxPermSize=164m Option.
MaxPermSize allows for the JVM to be able to grow the PermSize to the amount specified. Initially when the JVM is loaded, the MaxPermSize will still be the default value (64mb for Server) but will not actually take up that amount until it is needed. But, if you want to Set a new initial perm size on Sun JVM, you can use -XX:PermSize=164m Option when starting the JVM.

Conclusion
  • The PermGen default size can be a bottleneck in your Web Applications that dynamically generate and load many classes.
  • The PermGen parameters discussed above when applied to the JVM that runs your application server, considerably reduces the chances of running into a PermGen OutOfMemoryError.


AddThis Social Bookmark Button

1 comment:

Kalpesh Soni said...

brilliant oracle/sun engineers

if i bump up max memory 3 times the default, how difficult is it for you to bump up everything 3 times and maintain ratios?

apparantly too much