Skip to content

Troubleshooting guide

java.lang.StackOverflowError: causes and how to fix it

A thread used up its stack, almost always through unbounded recursion. The repeating frames in the stack trace show the cycle.

3 min read11 sectionsUpdated

On this page

Key points

  • Read the repeating frames to find the cycle.
  • Circular entity references are a common cause.
  • toString, hashCode and equals can recurse.
  • -Xss rarely addresses the cause.

What it means

Each thread has a fixed stack size, typically 1 MB on 64-bit Linux. Each method call pushes a frame. When space runs out, the JVM throws StackOverflowError. It is an Error, so catch (Exception e) will not catch it.

Common causes

  • Recursion without a working base case.
  • Bidirectional JPA entities serialised by Jackson.
  • Lombok @Data on related entities, generating mutual toString and hashCode.
  • Very deep but finite recursion.
  • Mutual calls between Spring beans or filters.

Diagnose it

Stack traces can be cut off, so increase depth when you need to see the start of the cycle.

bash
# Show more frames (default 1024)
java -XX:MaxJavaStackTraceDepth=100000 -jar app.jar

# See the frames that repeat most
grep '^\s*at ' error.log | sort | uniq -c | sort -rn | head -10

# Current default thread stack size
java -XX:+PrintFlagsFinal -version | grep ThreadStackSize

Fixes

Correct the base case, or convert to a loop using an explicit Deque. For Jackson, use @JsonManagedReference and @JsonBackReference, or return DTOs instead of entities. With Lombok, use @ToString.Exclude and @EqualsAndHashCode.Exclude on relationship fields. Only increase -Xss when depth is truly bounded and only a little larger than the default: it applies to every thread, so it can multiply memory use.

Why DTOs are the better fix

Annotating entities works, but it couples your JSON output to your persistence model. A DTO exposes only the fields a client needs, avoids cycles entirely and prevents accidental lazy loading from triggering dozens of queries during serialisation. A related trap: an overridden hashCode that includes a collection of children which also reference their parent will overflow when the object is placed in a HashSet, and the stack trace then points at the collection and not your code.

Catch it automatically

Because it is often not caught by ordinary exception handlers, confirm that your framework records Throwable. Error tracking groups by the repeating frame, so that one bug does not become thousands of separate alerts.

Frequently asked questions

Can I catch StackOverflowError?

Technically yes, but the application state may be inconsistent. Fix the cause.

What does -Xss do?

It sets stack size per thread.

Why does it only happen with certain data?

Data depth, such as a long parent chain, determines recursion depth.

Check it now, free

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.