Pivotal Knowledge Base

Follow

Application Server Error: "java.lang.OutOfMemoryError: PermGen Space"

Environment

 Product  Version
 Spring  All
 OS  All Supported OS

Symptom

Server fails or becomes unresponsive The JVM error log file contains errors similar to:

Exception in thread "thread-name" java.lang.OutOfMemoryError: PermGen space

Note: These errors are often for many threads.

Cause

The server JVM has run out of PermGen space. When PermGen becomes full, the JVM can no longer function properly and must be restarted.

In the HotSpot JVM, PermGen space is separate from heap space and is used to store class definitions. The defaults for the JVM are frequently inadequate for an application server, which tends to host a lot of class definitions, and may do a lot of dynamic class loading and unloading. Also, applications may have bugs that cause leaks or otherwise create excessive class definitions.

Resolution

Memory allocated to PermGen is separate from the heap and not affected by Java options like -Xmx or -Xms. PermGen can run out of space even if there is plenty of space in a heap. The HotSpot JVM provides options -XX:PermSize and -XX:MaxPermSize to set the initial size and maximum allowed size of the PermGen, respectively. In a server situation, unless you have reason to do otherwise, we recommend setting these to the same value, beginning with the maximum extent and avoiding the full garbage collections that occur when PermGen needs to expand.

For example, you could add this to your existing JAVA_OPTS:

-XX:PermSize=256M -XX:MaxPermSize=256M

You need to set this large enough that the error does not recur.

If you find that no matter how you size PermGen, you eventually run into this error, then you need to check your applications for the source of the problem. Particularly during development (where applications are re-deployed frequently), you are likely to see subtle problems leading to classloader leaks. There are a number of missteps your applications can make that can affect the whole server, and you likely need a memory profiler to find them. It is usually better to catch these as soon as possible rather than let them strike unpredictably in production.

Impact/Risks

When you increase PermGen, your server reserves more memory. Sizing PermGen too high may be wasteful.

The amount allocated for the heap and PermGen added together must not exceed the memory limit for the Java process, or it'll fail. Check error messages carefully for signs of what exactly ran out of memory.

Additional Information

Although this article refers to application servers, which are most prone to this problem, any Java application that loads a large amount of class definitions can use the same HotSpot JVM options to avoid this error.

The -XX:MaxPermSize option is described in the Java HotSpot VM Options page. The -XX:PermSize option is mentioned in this garbage collection FAQ and this tuning document from Oracle.

Note: Technically, these options (and all -X options) are experimental and may disappear without notice.

For a good introduction to JVM memory usage, classloader leaks, and profiling, watch this presentation by VMware's Mark Thomas.

Comments

Powered by Zendesk