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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • How to Quarantine a Malicious File in Java
  • Buildpacks: An Open-Source Alternative to Chainguard
  • Dust Actors and Large Language Models: An Application
  • Dust: Open-Source Actors for Java

Trending

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • DGS GraphQL and Spring Boot
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  1. DZone
  2. Coding
  3. Languages
  4. Simulating and Troubleshooting OOMError in Scala

Simulating and Troubleshooting OOMError in Scala

As we explore simulating and troubleshooting performance problems in Scala, learn how to simulate the java.lang.OutOfMemoryError: Java Heap space problem.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Feb. 07, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.6K Views

Join the DZone community and get the full member experience.

Join For Free

As we continue in this series of simulating and troubleshooting performance problems in Scala, now let’s discuss how to simulate the java.lang.OutOfMemoryError: Java Heap space problem. java.lang.OutOfMemoryError: Java Heap space will be thrown by the Scala application when it generates more objects than the maximum configured heap size.

Scala OutOfMemoryError Program

Here is a sample Scala program, which generates the java.lang.OutOfMemoryError: Java Heap space problem:

package com.yc 
import java.util 
class OOMApp { 
}
object OOMApp {   
   var myMap = new util.HashMap[String,String]()      
      def main(args: Array[String]): Unit = {         
       var counter = 0;            
         while (true) {               
           System.out.println("Inserting large String")               
           myMap.put("key"+counter, "Large stringgggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"               
           + counter)               
           counter = counter+(1);            
        }      
    } 
}


This sample Scala program contains an OOMApp class. Since this class contains the while(true) loop, it keeps on inserting records into the HashMap infinitely. When HashMap grows beyond the maximum heap size (i.e., -Xmx), the java.lang.OutOfMemoryError: Java Heap space will be thrown. The diagram below illustrates the records present in the HashMap.

HashMap causing OutOfMemoryError

HashMap causing OutOfMemoryError

When we executed the program above, as expected, java.lang.OutOfMemoryError: Java heap space was thrown within a few seconds.

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 never get removed, java.lang.OutOfMemoryError will be thrown.

How To Troubleshoot OutOfMemoryError

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

Manual Approach

In the manual approach, you will need to capture a heap dump as the first step. A heap dump is a snapshot of memory that will show all the objects in memory, the values contained by those objects, and their references. You can capture a heap dump using one of the 7 approaches given here, but an important criterion is that 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 and HeapHero to analyze the heap dumps.

Automated Approach

On the other hand, you can also use the yCrash open source script, which would capture 360-degree data (GC log, 3 snapshots of thread dump, heap dump, netstat, iostat, vmstat, top, top -H, etc.) from your application stack within a minute and generate a bundle zip file. You can then either manually analyze these artifacts or upload them to the yCrash server for automated analysis. 

We used the automated approach. Once the captured artifacts were uploaded to the yCrash server, it instantly generated the below root cause analysis report highlighting the source of the problem.  

Heap dump analysis report generated by the yCrash tool

Heap dump analysis report generated by the yCrash tool 

yCrash tool pointing out the root cause of OutOfMemoryError

yCrash tool pointing out the root cause of OutOfMemoryError

The heap dump analysis report above is from the tool which precisely points out that the HashMap (i.e., myMap) data structure present in the com.yc.OOMApp$.myMap to be the root cause of the memory leak. Also, the tool is reporting that this HashMap is holding 99% of memory. 

Equipped with this information one can easily go ahead and fix the problematic code.

Video

To see the visual walk-through of this post, click below:



Open source Java (programming language) Scala (programming language) Error message

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

Opinions expressed by DZone contributors are their own.

Related

  • How to Quarantine a Malicious File in Java
  • Buildpacks: An Open-Source Alternative to Chainguard
  • Dust Actors and Large Language Models: An Application
  • Dust: Open-Source Actors for Java

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!