There are 9 types of java.lang.OutOfMemoryError, each signaling a unique memory-related issue within Java applications. Among these, ‘java.lang.OutOfMemoryError: Permgen space’ is a challenging error to diagnose. In this post, we’ll delve into the root causes behind this error, explore potential solutions, and discuss effective diagnostic methods to troubleshoot this problem. Let’s equip ourselves with the knowledge and tools to conquer this common adversary.
JVM Memory Regions
To better understand OutOfMemoryError, we first need to understand different JVM Memory regions. Here is a video clip that gives a good introduction about different JVM memory regions. But in nutshell, JVM has following memory regions:

- Young Generation: Newly created application objects are stored in this region.
- Old Generation: Application objects that are living for longer duration are promoted from the Young Generation to the Old Generation. Basically this region holds long lived objects.
- Permgen: If you are running on an older Java version, then you will see the Permgen region. However, starting from Java 8, Permgen has been replaced by Metaspace. Class definitions, method definitions and other metadata that are required to execute your program are stored in the Permgen region. This region was added in Java 8. Before that metadata definitions were stored in the PermGen.
- Threads: Each application thread requires a thread stack. Space allocated for thread stacks, which contain method call information and local variables are stored in this region.
- Code Cache: Memory areas where compiled native code (machine code) of methods is stored for efficient execution are stored in this region.
- Direct Buffer: ByteBuffer objects are used by modern framework (i.e. Spring WebClient) for efficient I/O operations. They are stored in this region.
- GC (Garbage Collection): Memory required for automatic garbage collection to work is stored in this region.
- JNI (Java Native Interface): Memory for interacting with native libraries and code written in other languages are stored in this region.
- misc: There are areas specific to certain JVM implementations or configurations, such as the internal JVM structures or reserved memory spaces, they are classified as ‘misc’ regions.
What is ‘java.lang.OutOfMemoryError: Permgen space’?

When lot of class definitions, method definitions are created in the ‘Permgen space’ region than the allocated Permgen memory limit (i.e., ‘-XX:PermSize’), JVM will throw ‘java.lang.OutOfMemoryError: Permgen space’.
What causes ‘java.lang.OutOfMemoryError: Permgen space’?
‘java.lang.OutOfMemoryError: Permgen space’ is triggered by the JVM under following circumstances:
- Creating large number of dynamic classes: If your application uses Groovy kind of scripting languages or Java Reflection to create new classes at runtime.
- Loading large number of classes: Either your application itself has a lot of classes or it uses a lot of 3rd party libraries/frameworks which have a lot of classes in it.
- Loading large number of class loaders: Your application is loading a lot of class loaders.
Solutions for ‘OutOfMemoryError: Permgen space’
Following are the potential solutions to fix this error:
- Increase Permgen Size: If OutOfMemoryError surfaced due to increase in number of classes loaded, then increase the JVM’s Permgen size (-XX:PermSize). This solution is sufficient to fix most of the ‘OutOfMemoryError: Permgen space’ errors, because memory leaks rarely happen in the Permgen region.
- Fix Memory Leak: Analyze memory leaks in your application using the approach given in this post. Ensure that class definitions are properly dereferenced when they are no longer needed to allow them to be garbage collected.
Sample Program that generates ‘OutOfMemoryError: Permgen space’
To better understand ‘java.lang.OutOfMemoryError: Permgen space’, let’s try to simulate it. Let’s leverage BuggyApp, a simple open-source chaos engineering project. BuggyApp can generate various sorts of performance problems such as Memory Leak, Thread Leak, Deadlock, multiple BLOCKED threads, … Below is the Java program from the BuggyApp project that simulates ‘java.lang.OutOfMemoryError: Permgen space’ when executed.
import java.util.UUID;
import javassist.ClassPool;
public class OOMPermgen {
public static void main(String[] args) throws Exception {
ClassPool classPool = ClassPool.getDefault();
while (true) {
// Keep creating classes dynamically!
String className = "com.buggyapp.PermgenObject" + UUID.randomUUID();
classPool.makeClass(className).toClass();
}
}
}
In the above program ‘OOMPermgen’ class’s ‘main()’ method contains an infinite ‘while (true)’ loop. Within the loop, thread uses open-source library javassist to create dynamic classes whose names start with ‘com.buggyapp.PermgenObject’. Class names generated by this program will look something like this: ‘com.buggyapp.PermgenObjectb7a02000-ff51-4ef8-9433-3f16b92bba78’. When so many such dynamic classes are created, Permgen memory region will reach its limit and JVM will throw ‘java.lang.OutOfMemoryError: Permgen space’.
How to troubleshoot ‘OutOfMemoryError: Permgen space’?
To diagnose ‘OutOfMemoryError: Permgen space’, we need to inspect the contents of the Permgen region. Upon inspecting the contents, you can figure out the leaking area of the application code. Here is an approach to inspect the contents of the Permgen region.
-verbose:class: If you are running on Java version 8 or below then you can use this option. When you pass the ‘-verbose:class’ option to your application during startup, it will print all the classes that are loaded into memory. Loaded classes will be printed in the standard error stream (i.e. console, if you aren’t routing your error stream to a log file). Example:
java {app_name} -verbose:class
If you are still unable to determine the origination of the leak based on the class name, then you can do a deep dive by taking a heap dump from the application. You can capture heap dump using one of the 8 options discussed in this post. You might choose the option that fits your needs. Once a heap dump is captured, you need to use tools like HeapHero, JHat, … to analyze the dumps.
What is Heap Dump?
Heap Dump is basically a snapshot of your application memory. It contains detailed information about the objects and data structures present in the memory. It will tell what objects are present in the memory, whom they are referencing, who are referencing, what is the actual customer data stored in them, what size of they occupy, are they eligible for garbage collection… They provide valuable insights into the memory usage patterns of an application, helping developers identify and resolve memory-related issues.
How to analyze Permgen Memory leak through Heap Dump?
HeapHero is available in two modes:
1. Cloud: You can upload the dump to the HeapHero cloud and see the results.
2. On-Prem: You can register here and get the HeapHero installed on your local machine & then do the analysis.
Note: I prefer using the on-prem installation of the tool instead of using the cloud edition, because heap dump tends to contain sensitive information (such as SSN, Credit Card Numbers, VAT, …) and don’t want the dump to be analyzed in external locations.
Once the heap dump is captured, from the above program, we load it into the HeapHero tool. Tool analyzed the dump and generated the report. In the report, go to the ‘Histogram’ view. This view will show all the classes that are loaded into the memory. In this view, you will notice the classes with the prefix ‘com.buggyapp.PermgenObject’ . Right click on the ‘…’ that is next to the class name. Then click on the ‘List Object(s) with > incoming references’ as shown in the below figure.

Once you do it, the tool will display all the incoming references of this particular class. This will show the origin point of these classes as shown in the below figure. It will clearly show which part of code is creating these class definitions. Once we know which part of code is creating these class definitions, then it would be easy to fix the problem.

Conclusion
In this post, we’ve covered a range of topics, from understanding JVM memory regions to diagnosing and resolving ‘java.lang.OutOfMemoryError: Permgen space’. We hope you’ve found the information useful and insightful. But our conversation doesn’t end here. Your experiences and insights are invaluable to us and to your fellow readers. We encourage you to share your encounters with ‘java.lang.OutOfMemoryError: Permgen space’ in the comments below. Whether it’s a unique solution you’ve discovered, a best practice you swear by, or even just a personal anecdote, your contributions can enrich the learning experience for everyone.
FAQ
What are the JVM Memory Regions?
The JVM has several memory regions:
- Permgen/Metaspace: Stores class definitions and metadata. Permgen was replaced by Metaspace starting from Java 8.
- Young Generation: Stores newly created application objects.
- Old Generation: Holds long-lived objects promoted from the Young Generation.
What tools are available for troubleshooting Java performance issues?
Commonly used tools include JVisualVM, Java Flight Recorder (JFR), Java Mission Control (JMC), JProfiler, YourKit, and Eclipse MAT. Developers rely on these tools to understand what’s happening inside a Java application, whether it’s high CPU usage, growing memory consumption, frequent garbage collection, thread contention, or issues hidden in heap dumps.
But tools like yCrash, GCeasy, fastThread, and HeapHero are often preferred when teams need quicker, clearer answers. By automating much of the analysis, they reduce manual effort and help teams get to the root cause faster, something that becomes especially important in production environments where every minute counts.
How to solve ‘OutOfMemoryError: Direct buffer memory’?
This error typically indicates issues such as memory leaks or excessive direct buffer allocation. To resolve it, you should identify and fix any memory leaks and review how direct buffers are being allocated and released. In some cases, upgrading to a newer Java version, such as Java 17, may also help. A clear understanding of JVM memory areas is essential to effectively troubleshoot this issue.
How can I troubleshoot ‘OutOfMemoryError: Permgen space’?
To diagnose OutOfMemoryError: Permgen space, you need to inspect the contents of the Permgen region. This can be done by capturing a heap dump and analyzing it using tools such as HeapHero. The analysis will show all the classes loaded into memory, helping you identify those contributing to the memory issue. This error typically occurs when too many dynamic classes are created, eventually filling up the Permgen space.
How can I analyze a Permgen Memory leak through a Heap Dump?
You can analyze a Permgen memory leak using HeapHero, which is available in two modes: Cloud and On-Prem.
- In On-Prem version, you can register and analyze the heap dump locally within your environment.
- In Cloud version, you can upload the heap dump to the HeapHero cloud and review the analysis results.
