Core Java

Chaos Engineering – Metaspace OutOfMemoryError

JVM memory has following regions: 

a. Young Generation

b. Old Generation

c. Metaspace

d. Others region

When you encounter ‘java.lang.OutOfMemoryError: Metaspace’ it indicates that the Metaspace region in the JVM memory is getting saturated. Metaspace is the region where metadata details that are required to execute your application are stored. In nutshell they contain Class definitions and method definitions of your application. To learn more about what gets stored in each of the JVM memory regions, you may refer to this video clip. In this post let’s discuss how one can simulate java.lang.OutOfMemoryError: Metaspace.

Simulating java.lang.OutOfMemoryError: Metaspace

To simulate ‘java.lang.OutOfMemoryError: Metaspace’, we wrote this program:

public class MetaspaceLeakProgram {
   public static void main(String[] args) throws Exception {
      ClassPool classPool = ClassPool.getDefault();

      while (true) {
         // Keep creating classes dynamically!
         String className = "com.buggyapp.MetaspaceObject" + UUID.randomUUID();

This program leverages the ‘ClassPool’ object from the opensource javassist library. This ‘ClassPool’ object is capable of creating new classes at runtime. Please take a close look at the above program. If you notice, this program keeps on creating new classes. Below is the sample class names generated by this program:


Whenever a new class is created, its corresponding class metadata definitions are created in the JVM’s Metaspace region. Since metadata definitions are created in Metaspace, it’s size starts to grow. When the maximum metaspace size is reached, application will experience ‘java.lang.OutOfMemoryError: Metaspace’

java.lang.OutOfMemoryError: Metaspace causes

 ‘java.lang.OutOfMemoryError: Metaspace’ error happens because of two reasons:

 a. Metaspace region size is under allocated 

 b. Memory leak in the Metaspace region. 

You can address #a by increasing Metaspace region size. You can do this by passing the JVM argument ‘-XX:MaxMetaspaceSize’. 

In order to address #b, you have to do proper troubleshooting. Here is a post which walks through how to troubleshoot memory leaks in the Metaspace region.


Published on Java Code Geeks with permission by Ram Lakshmanan, partner at our JCG program. See the original article here: Chaos Engineering – Metaspace OutOfMemoryError

Opinions expressed by Java Code Geeks contributors are their own.

Ram Lakshmanan

Ram Lakshmanan developed world's finest DevOps tools:,, Every single day, millions & millions of people in North America—bank, travel, and commerce—use the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on scalability, availability, and performance topics. Recently, he has founded a startup, which specializes in troubleshooting performance problems.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button