Tuesday, December 23, 2008

OutOfMemoryError: PermGen space

Recently, I found a problem with our system in which it ran out of PermGen space under heavy usage. The exception from the logs is:
java.lang.OutOfMemoryError: PermGen space
        at java.lang.String.intern(Native Method)

PermGen Space
The memory a jvm uses is split up into three "generations": young (eden), tenured, and permanent. PermGen refers to the permanent generation which holds meta-data describing user classes and "interned" strings. If you have a large code base and intern lots of strings, more permgen space is used.

Interning Strings
You can intern strings using String.intern() which, from the javadocs:

Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
Interned strings are stored in permgen space, so if you interned all your strings, you would eventually run out of memory. In our application, I found that we were interning string representations of timestamps which wasn't necessary! You should only intern strings which are used frequently.

Increasing PermGen Space
The maximum size is typically 64MB. You can double it by starting your JVM with the following system properties:
-XX:MaxPermSize=128m -XX:PermSize=128m

Using JConsole to Monitor Memory
An easy way to see how much memory your JVM is using is to use jconsole. First, you need to enable monitoring on your JVM by starting it up with the following system properties:
-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=<port>
Next start jconsole and point it at this JVM using the host and port (or pid). Select the Memory tab and then choose the Memory Pool "PS Perm Gen" chart. You should now see the usage of your PermGen space as shown in the screenshot below:

1 comment:

  1. Anonymous9:33 AM

    Now do you worried about that in the game do not had enough cabal online alz to play the game, now you can not worried, my friend told me a website, in here you can buy a lot cabal alz and only spend a little money, do not hesitate, it was really, in here we had much cabal gold, we can sure that you will get the cabal money, quick to come here to buy cabal alz.

    I like play online game, I also buy dofus kamas and kamas, the cheap kamas is very cheap, and use the dofus gold can buy many things, I like dofus kamas, thanks, it is very good.