Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to expand size of Java stack trace to see bottom of stack? (triggering a stack overflow)

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.

like image 260
Urban Vagabond Avatar asked Aug 08 '12 13:08

Urban Vagabond


People also ask

How do you read stack trace top to bottom?

To read this stack trace, start at the top with the Exception's type - ArithmeticException and message The denominator must not be zero . This gives an idea of what went wrong, but to discover what code caused the Exception, skip down the stack trace looking for something in the package com.

How do I view stack trace in Eclipse?

code. (Window --> show view --> Other .. --> debug > display). text' button). --> go to the concole view, the trace is printed.

What is Max stack size in Java?

Sets the maximum stack size for Java™ threads. The default is 320 KB for 31-bit or 32-bit JVMs and 1024 KB for 64-bit JVMs. The maximum value varies according to platform and specific machine configuration. If you exceed the maximum value, a java/lang/OutOfMemoryError message is reported.

How to get the size of the stack in Java?

The Java.util.Stack.size () method in Java is used to get the size of the Stack or the number of elements present in the Stack. Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Stack. Program 1: Stack with string elements. Program 2: Stack with integer elements.

What is stack trace in Java?

The stack trace collects the information of all methods run by a program and tells us the location of exception or error in the console. The JVM automatically shows the stack trace when an exception is thrown, or an error has occurred. This tutorial will demonstrate what stack trace is and how to use it to debug our code.

How do I read a stack trace from the bottom up?

Reading the stack trace from bottom to top you can trace the exact path from the beginning of your code, right to the Exception. What Triggered the Stack Trace? The thing that causes an Exception is usually an explicit throw statement.

What is the difference between stack trace and exception?

Stack traces and exceptions are often associated with each other. When you see a Java application throw an exception, you usually see a stack trace logged with it. This is because of how exceptions work. When Java code throws an exception, the runtime looks up the stack for a method that has a handler that can process it.


Video Answer


3 Answers

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).

like image 195
Odd Avatar answered Oct 23 '22 06:10

Odd


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

like image 29
Edmon Avatar answered Oct 23 '22 06:10

Edmon


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?

like image 28
Peter Lawrey Avatar answered Oct 23 '22 05:10

Peter Lawrey