Simulating & troubleshooting StackOverflowError in Kotlin – Fast thread
source link: https://blog.fastthread.io/2022/10/18/simulating-troubleshooting-stackoverflowerror-in-kotlin/
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 StackOverflow errors. StackOverflow error is a runtime error, which is thrown when a thread’s stack size exceeds its allocated memory limit.
Video: To see the visual walk-through of this post, click below:
Sample Program
Here is a sample kotlin program, which generates the StackOverflowError:
package com.buggyapp
class StackOverflowApp {
fun start() {
start()
}
}
fun main() {
System.`in`.read()
try {
println(StackOverflowApp().start())
} finally {
System.`in`.read()
}
}
You can notice the sample program contains the ‘StackOverflowApp’ class. This class has a start() method which calls itself recursively. As a result of this implementation, the start() method will be invoked infinitely.
Fig: start() method repeatedly added to thread’s stack, resulting in StackOverflowError
As per the implementation, the start() method will be added to the thread’s stack frame an infinite number of times. Thus, after a few thousand iterations thread’s stack size limit would be exceeded. Once the stack size limit is exceeded it will result in ‘StackOverflowError’.
Execution
When we executed above program, as expected ‘java.lang.StackOverflowError’ was thrown in seconds:
Exception in thread "main" java.lang.StackOverflowError
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5)
:
:
How to diagnose ‘java.lang.StackOverflowError’?
You can diagnose StackOverflowError either through a manual or automated approach.
Manual approach
When an application experiences ‘StackOverflowError’, it will be either printed in the application log file or in a standard error stream. From the stack trace, you will be able to figure which line of code causing the infinite looping.
Automated approach
On the other hand, you can also use 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,…) 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: yCrash highlighting thread may result in StackOverflowError
You can notice the yCrash tool precisely pointing out the thread stack length is greater than 400 lines and it has potential to generate StackOverflowError. The Tool also points out the exact stack trace of the thread which is going on an infinite loop. Using this information from the report, one can easily go ahead and fix the StackOverflowError.
Share this:
Like this:
Recommend
-
30
在使用 AccessibilityService 遍历包含 WebView 的 AccessibilityNodeInfo 时会在某些情况下必现 StackOverflowError 的错误,导致应用崩溃。 0x00 异常 最近使用 Android 的 AccessibilitySer...
-
57
-
27
前言 某天业务方的同事和我反馈,说系统出现了 StackOverflowError .坦白说, Exception 见得过了,但是 Error 倒是很少出现,此时他的心情是这样的
-
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...
-
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 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...
-
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...
-
3
Java StackOverflowError 与递归及尾递归优化 2023-12-30 | 阅读(5) 本篇同样是阅读《100 Java Mistakes and How to Avoid Them》的一则笔记,只是火力全集中在 StackOverflowError 这个单一主题之上, 且主...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK