DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Ultimate Chaos Testing Guide
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • Chaos Engineering for Microservices

Trending

  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Coding
  3. Java
  4. Chaos Engineering: Metaspace OutOfMemoryError

Chaos Engineering: Metaspace OutOfMemoryError

Here, simulate an encounter with ‘java.lang.OutOfMemoryError: Metaspace,’ which indicates that the Metaspace region in the JVM memory is getting saturated.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Sep. 15, 22 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
6.5K Views

Join the DZone community and get the full member experience.

Join For Free

JVM memory has the following regions: 

JVM memory regions

JVM memory regions

  1. Young Generation
  2. Old Generation
  3. Metaspace
  4. 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 a 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 the "JVM Memory - Learn Easily" video. 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();         
           classPool.makeClass(className).toClass();     
          }   
     }    
}


This program leverages the ClassPool object from the open-source 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:

com.buggyapp.MetaspaceObject76a9a309-c9c6-4e5f-a302-8340eb3acdef 
com.buggyapp.MetaspaceObjectb9bd6832-bacd-4c7c-a6e6-3bfa19a85e80 
com.buggyapp.MetaspaceObject81d9d086-7245-4304-818f-0bfcbf319fd3 
com.buggyapp.MetaspaceObjecte27068b6-f4cb-498a-80d5-0e5b61c2ada0 
com.buggyapp.MetaspaceObject06f9d773-d365-48c8-a5cc-9c69b3178f4c 
:
: 
:


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, its size starts to grow. When the maximum metaspace size is reached, the application will experience ‘java.lang.OutOfMemoryError: Metaspace.’

java.lang.OutOfMemoryError: Metaspace Causes

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

  1.  Metaspace region size is under-allocated 
  2.  Memory leak in the Metaspace region

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

In order to address reason #2, you have to do proper troubleshooting. Read further about how to troubleshoot memory leaks in the Metaspace region.

Video


Chaos engineering Java virtual machine

Published at DZone with permission of Ram Lakshmanan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Ultimate Chaos Testing Guide
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • Chaos Engineering for Microservices

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!