Blog |

How to Fix java.lang.StackOverflowError in Java

How to Fix java.lang.StackOverflowError in Java
Table of Contents

The java.lang.StackOverflowError is a runtime error which points to serious problems that cannot be caught by an application. The java.lang.StackOverflowError indicates that the application stack is exhausted and is usually caused by deep or infinite recursion.

What Causes java.lang.StackOverflowError in Java

The java.lang.StackOverflowError occurs when the application stack continues to grow until it reaches the maximum limit. Some of the most common causes for a java.lang.StackOverflowError are:

  1. Deep or infinite recursion - If a method calls itself recursively without a terminating condition.
  2. Cyclic relationships between classes - If a class A instantiates an object of class B, which in turn instantiates an object of class A. This can be considered as a form of recursion.
  3. Memory intensive applications - Applications that rely on resource heavy objects such as XML documents, GUI or java2D classes.

java.lang.StackOverflowError Example in Java

Here is an example of java.lang.StackOverflowError thrown due to unintended recursion:

public class StackOverflowErrorExample {
    public void print(int myInt) {
        System.out.println(myInt);
        print(myInt);
    }

    public static void main(String[] args) {
        StackOverflowErrorExample soee = new StackOverflowErrorExample();
        soee.print(0);
    }
}

In this example, the recursive method print() calls itself over and over again until it reaches the maximum size of the Java thread stack since a terminating condition is not provided for the recursive calls. When the maximum size of the stack is reached, the program exits with a java.lang.StackOverflowError:

Exception in thread "main" java.lang.StackOverflowError
    at java.base/sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:564)
    at java.base/java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:585)
    at java.base/sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:301)
    at java.base/sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:290)
    at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:131)
    at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:208)
    at java.base/java.io.BufferedWriter.flushBuffer(BufferedWriter.java:120)
    at java.base/java.io.PrintStream.writeln(PrintStream.java:722)
    at java.base/java.io.PrintStream.println(PrintStream.java:938)
    at StackOverflowErrorExample.print(StackOverflowErrorExample.java:3)
    at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)
    at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)
    at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)
    at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)

How to fix java.lang.StackOverflowError in Java

Inspect the stack trace

Carefully inspecting the error stack trace and looking for the repeating pattern of line numbers enables locating the line of code with the recursive calls. When the line is identified, the code should be examined and fixed by specifying a proper terminating condition. As an example, the error stack trace seen earlier can be inspected:

at java.base/java.io.PrintStream.writeln(PrintStream.java:722)
at java.base/java.io.PrintStream.println(PrintStream.java:938)
at StackOverflowErrorExample.print(StackOverflowErrorExample.java:3)
at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)
at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)
at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)
at StackOverflowErrorExample.print(StackOverflowErrorExample.java:4)

In the above trace, line number 4 can be seen repeating, which is where the recursive calls are made and causing java.lang.StackOverflowError.

Increase Thread Stack Size (-Xss)

If the code has been updated to implement correct recursion and the program still throws a java.lang.StackOverflowError, the thread stack size can be increased to allow a larger number of invocations. Increasing the stack size can be useful, for example, when the program involves calling a large number of methods or using lots of local variables.

The stack size can be increased by changing the -Xss argument on the JVM, which can be set when starting the application. Here is an example:

-Xss4m

This will set the thread’s stack size to 4 mb which should prevent the JVM from throwing a java.lang.StackOverflowError.

Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates Java error monitoring and triaging, making fixing errors easier than ever. Try it today.

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape