Simulating and Troubleshooting OOMError in Kotlin

Continuing this troubleshooting series on simulating and troubleshooting performance issues in Kotlin, let us now discuss how to simulate 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 outofmemory error program

Here is a sample Kotlin program, which generates 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: OOMDemo Class. This class includes start() way. This method keeps on inserting the records HashMap infinitely. When HashMap grows beyond the maximum heap size (ie, -Xmx), Were java.lang.OutOfMemoryError: Java Heap space will be thrown. The diagram below shows the records present: HashMap,

HashMap causes OutOfMemory error

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 imitates java.lang.OutOfMemoryError, thus a typical memory leak in enterprise applications. When records are inserted into a data structure (eg HashMap, ArrayList, Setetc) and never 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 would need to capture the heap dump as a 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 the heap dump using any of the 7 methods given here. But one important criterion is that you need to grab the heap dump right before OutOfMemoryError is thrown. If you are going to capture the heap dump later OutOfMemoryError If this happens, leaking objects can lead to garbage collection, and the problem will be difficult (or even impossible) to diagnose. Once the heap dump is captured, you need to import the heap dump from your production server to your local machine. From your local machine, you can use heap dump analysis tools like jHat or HeapHero to analyze heap dumps.

automatic approach

You can also use the yCrash open source script, which captures 360-degree data (GC logs, 3 snapshots of thread dumps, heap dumps, netstat, iostat, vmstat, top, top -H, etc.) from your application stack Will do a minute and generate a bundled zip file. You can then either analyze these artifacts manually or upload them to yCrash servers for automated analysis.

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


yCrash tool points to root cause of OutOfMemoryError

y crash Tool to pinpoint the root cause of OutOfMemoryError

Above is the heap dump analysis report from the tool which accurately indicates that HashMap (meaning, myMap) data structure present in com.yc.OOMApp To be the root cause of the memory leak. Also, the tool is reporting that it HashMap Is holding 100% memory. Armed with this information, one can easily go ahead and fix the problematic code.

Video

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



Leave a Comment