Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The StackOverflowError class

Reference for java.lang.StackOverflowError: what exhausts the call stack, how to spot runaway recursion, and how to fix it with loops or an explicit stack.

java.lang.StackOverflowError is thrown when a thread's call stack runs out of space. Each method call pushes a frame holding its local variables and return address; when calls nest too deeply the JVM cannot push another frame and throws this error.

It is an Error (a subclass of VirtualMachineError), not an Exception, so ordinary catch (Exception e) blocks do not see it. Nearly always the cause is recursion without a reachable base case, or a base case that is reached only after far too many levels.

Java
public class StackOverflowError extends VirtualMachineError

Try it

Java Runaway recursion and a legitimate deep one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Fixing it with a loop or an explicit stack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public StackOverflowError()
public StackOverflowError(String s)
Constructor Description
StackOverflowError() No message. The JVM throws this form, so getMessage() is null.
StackOverflowError(String s) Detail message.

Usage notes

  • Typical causes: a recursive method missing its base case, mutual recursion between two methods, toString() or equals()/hashCode() that call each other on cyclic object graphs (a parent printing its children, who print their parent), and accidental self-calls such as a setter that calls itself instead of assigning the field.
  • Depth is not fixed. It depends on the stack size (default 512 KB to 1 MB on 64-bit platforms, set with -Xss or the Thread constructor's stackSize argument), and on how large each frame is. Typical simple recursion overflows somewhere between 5,000 and 50,000 levels, but never rely on a number.
  • Fixes, in order of preference: rewrite as a loop; use an explicit Deque as the stack; restructure the algorithm (for example iterate over a tree level by level); only as a last resort raise -Xss or run the work in a thread created with a larger stack size.
  • Java does not optimize tail calls, so tail-recursive code still grows the stack.
  • Catching it is rarely useful. After the error the stack unwinds, so the program can continue, but state may be inconsistent if the overflow happened in the middle of an update. Catch it only at a top-level boundary, such as a request handler that logs and returns an error.
  • Stack traces of this error contain a repeating group of lines; the JVM limits the recorded depth (1024 frames by default, -XX:MaxJavaStackTraceDepth), so you see the repeating pattern rather than the real depth.
  • Virtual threads have growable stacks limited by a maximum size, but can still overflow with runaway recursion.