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

  • Troubleshooting Memory Leaks With Heap Profilers
  • The Ultimate Chaos Testing Guide
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Chaos Engineering for Microservices

Trending

  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • A Guide to Developing Large Language Models Part 1: Pretraining
  1. DZone
  2. Data Engineering
  3. Data
  4. Chaos Engineering: Simulating OutOfMemoryError

Chaos Engineering: Simulating OutOfMemoryError

This 'java.lang.OutOfMemoryError: Java Heap space' will be thrown by the application when the application generates more objects than the allocated heap size.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Mar. 09, 21 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
8.2K Views

Join the DZone community and get the full member experience.

Join For Free

In the series of chaos engineering articles, we have been learning to simulate various performance problems. In this post, let’s discuss how to simulate 'java.lang.OutOfMemoryError: Java Heap space' problem. This 'java.lang.OutOfMemoryError: Java Heap space' will be thrown by the application when the application generates more objects than the allocated heap size.

Sample Program

Here is a sample program from the open-source BuggyApp application, which would generate 'java.lang.OutOfMemoryError: Java Heap space' problem.

package com.buggyapp.memoryleak; 
import java.util.HashMap; 
public class OOMDemo {    
  static HashMap<Object, Object> myMap = new HashMap<>();      
  public static void start() throws Exception {            
   while (true) {               
   myMap.put("key" + counter, "Large stringgggggggggggggggggggggggggggg"              
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"                
    + counter);                  
++counter;      
   }   
  }   
}


Sample Java program contains an 'OOMDemo' class. This class contains the 'start()' method. This method keeps inserting records into the HashMap infinitely. When HashMap goes beyond maximum heap size (i.e. -Xmx) then 'java.lang.OutOfMemoryError: Java Heap space' will be thrown. The below diagram illustrates the records present in the HashMap:
Fig: HashMap causing OutOfMemoryError

Fig: HashMap causing OutOfMemoryError

Execution

We executed the above sample program in the following environment:

a. AWS 't3a.medium' EC2 instance.

b. Used OpenJDK 1.8.0_265, 64-Bit Server VM.

c. Heap size (i.e., -Xmx) was set to 512MB.

When we executed the above program, as expected below mentioned 'java.lang.OutOfMemoryError: Java heap space' was thrown in few seconds:

java.lang.OutOfMemoryError: Java heap space        
      at java.util.Arrays.copyOf(Arrays.java:3332)        
      at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)        
      at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:700)        
      at java.lang.StringBuilder.append(StringBuilder.java:214)        
      at com.buggyapp.memoryleak.OOMDemo.start(OOMDemo.java:21)        
      at com.buggyapp.LaunchPad.main(LaunchPad.java:49)        
      at com.buggyapp.servlet.LaunchBuggyAppServlet$BuggyAppThread.run(LaunchBuggyAppServlet.java:155)


Even though this is a hypothetical example that simulates ‘java.lang.OutOfMemoryError’, this is how a typical memory leak happens in enterprise applications. When records get inserted into a data structure (like HashMap, ArrayList, Set, etc), and they don’t get removed, OutOfMemoryError will be thrown.

How to Diagnose OutOfMemoryError?

You can diagnose OutOfMemoryError either through a manual or automated approach. 

Manual Approach

In the manual approach, you need to capture heap dump as the first step. A heap dump is a snapshot of memory, which shows all the objects in memory, values contained by those objects, and their references. You can capture heap dump using one of the 7 approaches given here. But an important criteria is: You need to capture the heap dump right before OutOfMemoryError is thrown. If you are going to capture a heap dump after OutOfMemoryError has occurred, then leaking objects can get garbage collected, and it will become hard (or even impossible) to diagnose the problem. Once heap dumps are captured, you need to import the heap dumps from your production servers to your local machine. From your local machine, you can use heap dump analysis tools like jHat, HeapHero to analyze the heap dumps.

Automated Approach

You can use root cause analysis tools like yCrash — which automatically captures application-level data (thread dump, heap dump, Garbage Collection log) and system-level data (netstat, vmstat, iostat, top, top -H, dmesg,…). Besides capturing the data automatically, it marries these two datasets and generates an instant root cause analysis report. Below is the report generated by the yCrash tool when the above sample program is executed:

Fig: yCrash tool pointing out the root cause of OutOfMemoryError

Fig: yCrash tool pointing out the root cause of OutOfMemoryError.

You can notice the yCrash tool precisely pointing out the HashMap (i.e., myMap) data structure present in the ‘com.buggyapp.memoryleak.OOMDemo’ to be the root cause of memory leak. The tool is reporting that this HashMap is holding 95.5% of memory.

Data structure garbage collection Chaos engineering

Opinions expressed by DZone contributors are their own.

Related

  • Troubleshooting Memory Leaks With Heap Profilers
  • The Ultimate Chaos Testing Guide
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • 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!