Simulating & Troubleshooting OOMError in Kotlin
source link: https://devm.io/java/kotlin-java-outofmemoryerror
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
In this series of simulating and troubleshooting performance problems in Kotlin, 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 application when it generates more objects than the maximum configured heap size.
Video: To see the visual walk-through of this post, click here.
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 the 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.
Fig. 1: 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 the heap dump as the first step. Heap dump is a snapshot of memory and it shows all of the objects in the memory, the values contained by those objects, and their references. You can capture the heap dump using one of the 7 approaches given here. But an important criteria 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
On the other hand, you can also use the yCrash open source script, which will capture 360-degree data (GC log, 3 snapshots of thread dump, heap dump, netstat, iostat, vmstat, top, top -H,…) from your application stack within a minute and generate a bundle zip file. You can then either manually analyze these artifacts or upload it to 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.
Fig. 2: yCrash tool pointing out the root cause of OutOfMemoryError
Fig. 3: 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 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.
Every single day, millions and millions of people in North America travel, bank, and do commerce and shipments using the applications that Ram Lakshmanan has architected. Ram is the founder of the highly popular GCEasy.io – Universal Garbage Collection log analyser and FastThread.io – Java thread dump analyser. Ram advises startu-ps to Fortune 500 enterprises to Governmental organisations on their critical technology initiatives. He is the recipient of popular developer contest awards and highly sought speaker in major developer conferences.
Recommend
-
112
Go's concurrency model makes it easy to develop scalable servers and data pipelines.Many of the patterns we use in developing concurrent code mirror structures in real-world systems.In this talk, I'll present a simulation of a small real world s...
-
51
README.rst
-
67
README.md Little Virtual Computer Have you ever wanted to know how a computer works? How the software is understood by the computer at a hardware level...
-
42
Simulating the Passport Seva Kendra using Clojure Vedang Manerikar A year ago, I went to the Passport Seva Kendr...
-
4
News Idiomatic Kotlin: Simulating a console to solve Advent of Code
-
3
Stream the event live: July 15 2022 | 16:00 - 18:30 CEST Join our live event: Java Performance & Troubleshooting Training Java
-
6
Android Q&A, Help & Troubleshooting This forum is for all of your questions about Android Development and Hacking. If you need help troubleshooting a...
-
9
In this series of simulating and troubleshooting performance problems in Kotlin, let’s discuss how to simulate StackOverflow errors. StackOverflow error is a runtime...
-
4
In this series of simulating and troubleshooting performance problems in Kotlin, let’s discuss how to make threads go into BLOCKED state. A thread will enter into a B...
-
2
In this latest article of the series about simulating and troubleshooting performance problems in Kotlin, let’s discuss how to make threads go into a BLOCK...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK