19

In Java, is there any way to view the complete, untruncated stack trace (e.g. by increasing the number of frames recorded), or otherwise get at the bottom of a stack trace? Normally the stack trace is truncated from the top at 1024 frames worth, but for a stack overflow problem this is fairly worthless as you really need to see who made the call that triggered the recursion, down near the bottom. Much better would truncation in the middle of the stack, but evidently Sun's JVM isn't smart enough to do this.

Perhaps even some special Sun-specific flags? I tried reducing the stack size to the minimum allowable (-Xss1000) but that's still more than 1024 frames worth.

In my case, I'm trying to debug a stack overflow that occurs in a Hadoop mapper, but only when running on really large input. I assume the problem comes because a recursive operation (Scala's foldRight) is being done on a really, really large linked list, and I need to rewrite it non-recursively ... but I need to know who made the call to foldRight. This is a basic routine called directly and indirectly in a lot of places and there's lots and lots of code out there that I'm working with, so this is highly non-obvious.

2
  • 1
    Have you considered rewriting the calls to foldright as functions which are tail-recursive? It may not be possible (or at least, not easy), but it may fix your issue if you can do it. Commented Aug 8, 2012 at 14:37
  • Here is a JDK bug report that describes the same issue. In the comments, a workaround is described which is the same as Odd's answer Commented May 20, 2022 at 21:10

4 Answers 4

29

Try the -XX:MaxJavaStackTraceDepth JVM option.

Here is a description from Stas's Blog

Max. no. of lines in the stack trace for Java exceptions (0 means all). With Java > 1.6, value 0 really means 0. value -1 or any negative number must be specified to print all the stack (tested with 1.6.0_22, 1.7.0 on Windows). With Java <= 1.5, value 0 means everything, JVM chokes on negative number (tested with 1.5.0_22 on Windows).

Sign up to request clarification or add additional context in comments.

1 Comment

Java 11 also doesn’t use negative numbers anymore: intx MaxJavaStackTraceDepth=-1 is outside the allowed range [ 0 ... 1073741823 ]
2

Try using jstack:

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jstack.html

http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/tooldescr.html#gblfh

Comments

2

You can iterate over the stack trace yourself

Throwable t =

for(StackTraceElement ste: t.getStackTrace()) {
    // is it a repeat??

}

The default printStackTrace will print every level which has been recorded. Your problem is that the stack is too deep for what it put in the stack trace.

Can you try reducing your stack size?

Comments

-2

If you have access to the call before it is entering the suspected stack overflow, you could just generate an additional stack trace like new Exception().getStackTrace. Note that foldRight itself won't produce an infinite recursion. Maybe you just run out of memory, try increasing the JVM's stack size.

2 Comments

Thread.currentThread().getStackTrace() would be more appropriate.
foldRight can produce infinite recursion if it is invoked on an infinite collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.