Troubleshooting Spring Boot StackOverflowError

Spring Boot is a widely used framework for building highly transactional Java-based web applications and backend systems. These applications process heavy loads and often have high processing requirements. In Spring Boot applications, a StackOverflowError occurs when a thread’s stack size exceeds its allocated memory (-Xss) limit. In this post, let’s discuss how to diagnose StackOverflowError that surfaces in a Spring Boot application.

Video

Watch this video to learn more about StackOverflowError.

What is a StackOverflowError?

StackOverflowError happens when the thread stack size exceeds its allocated memory. The two main reasons that can lead to StackOverflowErrors are:

  • When a function or method calls itself in an infinite loop, leading to the exhaustion of the call stack space.
  • The stack calls exceed the allocated memory limits. 

Simulating StackOverflowError in Spring Boot

In order to simulate the StackOverflowError in Spring Boot, we leveraged the open source BuggyAPI application, a comprehensive Spring Boot service capable of simulating various performance problems. When we launched the Buggy API application, it looked as below:

Spring Boot Buggy API Service
Fig 1: Spring Boot Buggy API Service

Here Spring Boot Buggy API services for simulating StackOverflowError:

@Service
public class StackOverflowDemoService {

public int counter = 1000;

private List<SimpleObject> objects = new ArrayList<>();
private static final Logger log = LoggerFactory.getLogger(StackOverflowDemoService.class);

public void start() {

++counter;

SimpleObject so0 = new SimpleObject("Simple Object created");

if (counter % 1000 == 0) {
log.info("Looped " + counter + " times");
}
counter++;

start();
}

You can notice the sample program contains the ‘StackOverflowDemoService’ 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. 

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’.

When we executed above program, as expected ‘java.lang.StackOverflowError’ was thrown in seconds:

2023-12-30 11:55:19.520 ERROR 68965 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause

java.lang.StackOverflowError: null
at com.ycrash.Spring Boot.buggy.app.service.stackoverflow.StackOverflowDemoService.start(StackOverflowDemoService.java:22) ~[classes!/:1.0.0]
at com.ycrash.Spring Boot.buggy.app.service.stackoverflow.StackOverflowDemoService.start(StackOverflowDemoService.java:73) ~[classes!/:1.0.0]
at com.ycrash.Spring Boot.buggy.app.service.stackoverflow.StackOverflowDemoService.start(StackOverflowDemoService.java:73) ~[classes!/:1.0.0]
at com.ycrash.Spring Boot.buggy.app.service.stackoverflow.StackOverflowDemoService.start(StackOverflowDemoService.java:73) ~[classes!/:1.0.0]
at com.ycrash.Spring Boot.buggy.app.service.stackoverflow.StackOverflowDemoService.start(StackOverflowDemoService.java:73) ~[classes!/:1.0.0]

Troubleshooting StackOverflowError in Spring Boot

In order to troubleshoot this problem, we leveraged the yCrash monitoring tool. This tool is capable of predicting outages before it surfaces in the production environment. Once it predicts outage in the environment, it captures 360° troubleshooting artifacts from your environment, analyses them and instantly generates a root cause analysis report. Artifacts it captures include Garbage Collection log, Thread Dump, Heap Substitute, netstat, vmstat, iostat, top, top -H, dmesg, kernel parameters, disk usage… 

You can register here and start using the free-tier of this tool.

Below is the report generated by the yCrash tool when the above sample SpringBoot program is executed:

yCrash highlighting thread may result in StackOverflowError
Fig 2: yCrash highlighting thread may result in StackOverflowError

You can notice the yCrash tool precisely pointing out the thread stack length is greater than 200 lines and it has the potential to generate StackOverflowError. The tool also points out the 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 problematic code.

Possible Solutions

An appropriate solution needs to be applied based on the specific StackOverflowError. Below are some recommended resolutions for StackOverflowError problems:

RecommendationDescription
Terminate Recursive MethodsProper termination conditions in recursive methods
Avoid RecursionsAvoid excessive recursion altogether
Increase Stack MemoryIncrease stack memory with -Xss java runtime argument

Conclusion

In this blog, we looked in detail at troubleshooting StackOverflowErrors with an emphasis on detecting the root cause and resolving the exceptions in Spring Boot applications.     

Share your Thoughts!

Up ↑

Index

Discover more from yCrash

Subscribe now to keep reading and get access to the full archive.

Continue reading