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.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
A pool of strings, initially empty, is maintained privately by the classString
.
When the intern method is invoked, if the pool already contains a string equal to thisString
object as determined by theequals(Object)
method, then the string from the pool is returned. Otherwise, thisString
object is added to the pool and a reference to thisString
object is returned.
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 MemoryAn 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: