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
Diagnose it
Stack traces can be cut off, so increase depth when you need to see the start of the cycle.
# 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 ThreadStackSizeFixes
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.
Check it now, free
- Security headers checker: Grade HSTS, CSP, X-Frame-Options and more, A+ to F.
Related reading
- 502 Bad Gateway: causes and how to fix it: What a 502 Bad Gateway means, the common causes behind Nginx, load balancers and CDNs, how to find yours and how to stop it happening again.
- ERR_CONNECTION_RESET: causes and how to fix it: Fix ERR_CONNECTION_RESET: crashed backends, firewalls and antivirus resetting traffic, MTU problems and keep-alive races, for both visitors and site owners.
- 408 Request Timeout: causes and how to fix it: Understand 408 Request Timeout: slow client uploads, idle keep-alive connections and proxy settings, and how it differs from a 504 Gateway Timeout.
- ERR_HTTP2_PROTOCOL_ERROR: causes and how to fix it: Fix ERR_HTTP2_PROTOCOL_ERROR in Chrome: invalid response headers, crashed backends behind HTTP/2 proxies, buffer limits and antivirus interference.
- Gunicorn WORKER TIMEOUT: causes and how to fix it: Fix Gunicorn CRITICAL WORKER TIMEOUT: slow requests, blocked sync workers, memory pressure and the --timeout setting, plus which worker class to pick.
- 503 Service Unavailable: causes and fixes: Why servers return 503 Service Unavailable, from overload and maintenance to no healthy backends, how to diagnose each cause and how to recover and prevent it.
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.