In the series of chaos engineering articles, we have been learning to simulate various performance problems. In this post, let’s discuss how to simulate StackOverflow error. StackOverflow error is a runtime error. In this post let’s discuss how to simulate StackOverflowError, diagnose it and solve the problem.
Sample Program
Here is a sample program from the open source BuggyApp application, which would generate ‘java.lang.StackOverflowError.
package com.buggyapp.stackoverflow; public class StackOverflowDemo { public void start() { start(); } }
You can notice the sample program contains the ‘StackOverflowDemo’ class. This class has a start() method which calls itself recursively. This implementation will cause the start() method to be invoked an infinite number of times.
Fig: start() method repeatedly added to thread’s stack, resulting in StackOverflowError’
As per the implementation start() method will be added to thread’s stack frame 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’ will be thrown in seconds:
java.lang.StackOverflowError at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33) at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
How to diagnose ‘java.lang.StackOverflowError’
You can diagnose StackOverflowError either through a manual or automated approach.
Manual approach
When application experience ‘StackOverflowError’ it will be either printed in the application log file or in standard error stream. From the stacktrace you will be able to figure which line of code causing the infinite looping.
Automated approach
You can use root cause analysis tools like yCrash – which automatically captures application-level data (thread dump, heap dump, Garbage Collection log) and system-level data (netstat, vmstat, iostat, top, top -H, dmesg,…). Besides capturing the data automatically, it marries these two datasets and generates an instant root cause analysis report. Below is the report generated by the yCrash tool when the above sample program is executed:
Fig: yCrash tool pointing out the root cause of StackOverflowError