What causes a java.lang.StackOverflowError What causes a java.lang.StackOverflowError java java

What causes a java.lang.StackOverflowError


Check for any recusive calls for methods. Mainly it is caused when there is recursive call for a method. A simple example is

public static void main(String... args) {    Main main = new Main();    main.testMethod(1);}public void testMethod(int i) {    testMethod(i);    System.out.println(i);}

Here the System.out.println(i); will be repeatedly pushed to stack when the testMethod is called.


One of the (optional) arguments to the JVM is the stack size. It's -Xss. I don't know what the default value is, but if the total amount of stuff on the stack exceeds that value, you'll get that error.

Generally, infinite recursion is the cause of this, but if you were seeing that, your stack trace would have more than 5 frames.

Try adding a -Xss argument (or increasing the value of one) to see if this goes away.


What is java.lang.StackOverflowError

The error java.lang.StackOverflowError is thrown to indicate that the application’s stack was exhausted, due to deep recursion i.e your program/script recurses too deeply.

Details

The StackOverflowError extends VirtualMachineError class which indicates that the JVM have been or have run out of resources and cannot operate further. The VirtualMachineError which extends the Error class is used to indicate those serious problems that an application should not catch. A method may not declare such errors in its throw clause because these errors are abnormal conditions that was never expected to occur.

An Example

Minimal, Complete, and Verifiable Example :

package demo;public class StackOverflowErrorExample {    public static void main(String[] args)     {        StackOverflowErrorExample.recursivePrint(1);    }    public static void recursivePrint(int num) {        System.out.println("Number: " + num);        if(num == 0)            return;        else            recursivePrint(++num);    }}

Console Output

Number: 1Number: 2...Number: 8645Number: 8646Number: 8647Exception in thread "main" java.lang.StackOverflowError    at java.io.FileOutputStream.write(Unknown Source)    at java.io.BufferedOutputStream.flushBuffer(Unknown Source)    at java.io.BufferedOutputStream.flush(Unknown Source)    at java.io.PrintStream.write(Unknown Source)    at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)    at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)    at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source)    at java.io.OutputStreamWriter.flushBuffer(Unknown Source)    at java.io.PrintStream.newLine(Unknown Source)    at java.io.PrintStream.println(Unknown Source)    at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:11)    at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:16)    .    .    .    at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:16)

Explaination

When a function call is invoked by a Java Application, a stack frame is allocated on the call stack. The stack frame contains the parameters of the invoked method, its local parameters, and the return address of the method. The return address denotes the execution point from which, the program execution shall continue after the invoked method returns. If there is no space for a new stack frame then, the StackOverflowError is thrown by the Java Virtual Machine (JVM).

The most common case that can possibly exhaust a Java application’s stack is recursion. In recursion, a method invokes itself during its execution. Recursion one of the most powerful general-purpose programming technique, but must be used with caution, in order for the StackOverflowError to be avoided.

References