20

I ran into a Java StackOverflow error, but the offending recursive call is so deep, that it does not give me the full stack trace. It only dumps out the first 1024 lines of methods of the StackOverflow exception.

How can I get the full stack trace, so that I can know the root cause?

4
  • 2
    Not an answer, but consider also debugging with breakpoints, logging, etc. Commented Aug 5, 2010 at 18:58
  • 1
    The root cause is in the first lines, the rest should look the same. Could you try to post the stacktrace to have a look at it? ( probably in pastebin.com or something like that? ) Commented Aug 5, 2010 at 19:00
  • Are you using recursion by any chance? If so, this helps blogs.msdn.com/b/oldnewthing/archive/2009/01/07/9286576.aspx . See the related discussion on SO: stackoverflow.com/questions/951635/… Commented Aug 5, 2010 at 19:01
  • I would bet this is impossible. It might very well be so that the jvm intentionally has a limit on the number of stack frames to reify on exceptions. Otherwise the time complexity for throwing exceptions would be linear in the number of stack frames. Commented Apr 27, 2014 at 15:24

3 Answers 3

21

The switch -XX:MaxJavaStackTraceDepth allows larger and smaller stack trace lengths. Set it to -1 for an unlimited length. E.g.:

-XX:MaxJavaStackTraceDepth=-1
Sign up to request clarification or add additional context in comments.

6 Comments

With recent Java versions this doesn't work: MaxJavaStackTraceDepth=-1 is outside the allowed range [ 0 ... 1073741823 ]
Which version did you test on @paj28 ?
It was on 10.0.2
Does not work on 16.
Where does this go??
|
2

You can use -Xss to change the stack size, but it won't help if you have unbounded recursion. It sounds like your stopping condition might not be worked out.

1 Comment

If anything, lower the stack size so the exception happens sooner and the stacktrace fits within the lines allowed! :-)
-1

Print the stack manually, the limit put on e.printStackTrace() does not apply when you provide a writer (might need to quote me on that but I've never experienced it being cropped when using this method).

Here is some easy code that I employ to get full traces:

/**
 * Produces a Java-like stack trace string.
 * 
 * @param e
 * @return
 */
public static String exceptionStack(Throwable e) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    e.printStackTrace(printWriter);
    return result.toString();
}

1 Comment

Doesn't work. I have only 1024 lines, it doesn't show the root cause.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.