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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Metal and the Simulated Annealing Algorithm
  • Programming Solutions for Graph and Data Structure Problems With Implementation Examples (Word Dictionary)
  • Troubleshooting Memory Leaks With Heap Profilers

Trending

  • Getting Started With Weaviate: Building AI-Powered Search Applications With Java
  • Getting Started With Qdrant: Building Intelligent Search Applications With Java
  • Ingesting Public Data That Fails Silently: Patterns From a TypeScript and Cloudflare Workers Pipeline
  • Building Reliable Async Processing Pipelines Using Temporal

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Simulating and Troubleshooting OOMError in Kotlin

Simulating and Troubleshooting OOMError in Kotlin

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

By 
Ram Lakshmanan
DZone Core CORE ·
Nov. 09, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Kotlin OutOfMemoryError Program

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

package com.buggyapp   
  class OOMDemo {      
     var myMap = HashMap<Any, Any>() @Throws(Exception::class)      
     fun start() {          
        while (true) {            
         var counter = 1000000;            
         myMap["key$counter"] = ("Large stringgggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"            
         + counter)            
         ++counter         
      }      
   }    
}


This Sample Kotlin program contains an OOMDemo class. This class contains the start() method. This method keeps 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 below diagram illustrates the records present in the HashMap.

HashMap causing OutOfMemoryError

HashMap causing OutOfMemoryError

When we executed the above program, as expected, java.lang.OutOfMemoryError: Java heap space was thrown in 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 and it shows all the objects in memory, the values contained by those objects, and their references. You can capture 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 or HeapHero to analyze the heap dumps.

Automated Approach

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.  

yCrash tool pointing out the root cause of OutOfMemoryError

yCrash tool pointing out the root cause of OutOfMemoryError

Above is the heap dump analysis report from the tool which precisely points out that the HashMap (i.e., myMap) data structure present in the com.yc.OOMApp to be the root cause of the memory leak. Also, the tool is reporting that this HashMap is holding 100% 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:



Data structure garbage collection Kotlin (programming language)

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

Opinions expressed by DZone contributors are their own.

Related

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Metal and the Simulated Annealing Algorithm
  • Programming Solutions for Graph and Data Structure Problems With Implementation Examples (Word Dictionary)
  • Troubleshooting Memory Leaks With Heap Profilers

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook