Java JVM GC, PermGen, and Memory Options

I’m still new to Java and so, I still am reading about the nitty-gritty, so when I ran into a situation with some code where we were running out of PermGen space I had to do some reading. In the JVM, PermGen holds the metadata about classes that have been loaded/created. This information is garbage collected like the other parts of the heap, however there are rough edges that can prevent this from happening, class loaders in particular (which I won’t discuss here.) Generally, the amount of PermGen space needed is small in relation to the rest of the heap and default JVM values should work for you.

So, what do you do? Simple answer, just increase PermGen space. Of course it is never that simple, so read on for more tips.

How do you increase PermGen space?

“Exception in thread “main” java.lang.OutOfMemoryError: PermGen space” signifies too many classes loaded or being generated; or the String.intern table is too big. Increase using the -XX:MaxPermSize option. “

Click here to learn how to set PermGen for Tomcat, JBoss, Weblogic, etc.

How big should PermGen be?

“30. Should I increase the size of the permanent generation in the client vm?
In general increasing the size of a generation (and this applies not just to the permanent generation) can reduce the incidence of a wide variety of problems. However… There are two failure modes to consider. When raising MaxPermSize, it is possible that previously well behaved programs that used to garbage collect to recover the permanent generation space will die by endless paging. For the permanent generation this usually only happens with the heavy interning of temporary strings. The other failure mode is that address space must be reserved for the permanent generation and this will reduce that available for the rest of the heap (the maximum -Xmx may then be too large). This will cause programs configured to use all available space to fail at initialization.

If you increase PermGen size and still run out, you might be leaking classes somewhere.

How do you find out what’s classes are being stored in PermGen? Trace class loading/unloading.

  1. How do I know what classes are being loaded or unloaded? Use the command line options -XX:+TraceClassloading and -XX:+TraceClassUnloading

What if you’re just getting out-of-memory exceptions and it seems you have plenty of memory? Are you using the throughput collector?

” The throughput collector will throw an out-of-memory exception if too much time is being spent doing garbage collection. For example, if the JVM is spending more than 98% of the total time doing garbage collection and is recovering less than 2% of the heap, it will throw an out-of-memory expection. The implementation of this feature has changed in 1.5. The policy is the same but there may be slight differences in behavior due to the new implementation.” ” In the J2SE Platform version 1.5 the throughput collector will be chosen as the garbage collector on server class machines.”

This is probably in Java Performance Tuning (2nd Edition) , but there are so many of those books out there, good luck!

9 Responses to “Java JVM GC, PermGen, and Memory Options”

  1. I’ve actually had to increase Permgen for a web app running in Tomcat. Was difficult to track down what was going on, but after I upped the space, the app was much happier :)

  2. The Tomcat specific issue has to do with classloaders from what I’ve read, and that topic I said I wasn’t going to touch.

  3. [...] get familiar with jvm args that help with this kind of debugging and performance optimizations. Verbose gc logging, tracing of class loading, etc. (, [...]

  4. [...] After reading several resources (#1, #2, #3) we’d determined the cause of our PermGen Problems: [...]

  5. [...] [...]

  6. [...] [...]

  7. [...] Java JVM GC, PermGen, and Memory Options Preventing Java’s java.lang.OutOfMemoryError: PermGen space failure Java Permgen space, String.intern, XML parsing 10 Good Riddance, PermGen OutOfMemoryError ! Posted by rubensa Filed in Java [...]

  8. Java PermGen space out of memory error : Steps to rectify this error :

    Hi Guys This is common error people get when they are using tomcat for big web application .so following are the steps to rectify this error : 1.0 click on tomcat start up 2.0 click on java tab in tomcat startup 3.0 Now add the following parameter in java option field :

    -XX:MaxPermSize=512m -verbose:gc -XX:+PrintClassHistogram

    4.0 assign intial pool memory 256 mb

    5.0 assign maximum memory pool 1024mb

    6.0 restart the server

    This setting is applicable or this is an example when your system RAM is 2GB where the tomcat is running .

    for other RAM size please make sure that memory division does not exceed to system memory.otherwise tomcat server will not restart.

  9. Hi just wondering if we can change 65K limition on method size(this is something i experienced in the JSP page) with -XX:MaxPermSize. Or is there a way to do it.

Leave a Reply