I've searched much on the topic. But no concrete solution/guide is present.
From the docs,
Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method.
Can someone shed light on what conditions this may/will happen?
I know it happens if the same stack trace is produced over and over again (There is no hard-and-fast-rule on the count, AFAIK- Correct me otherwise). But must not this be having a defined behaviour?
Also,As per this, passing -XX:-OmitStackTraceInFastThrow
will make sure my StackTrace
is not lost. Isn't it wise always to do that in Production machines?
Whenever a certain function call throws an error, you'll have a collection of function calls that lead up to the call that caused the particular problem. This is due to the LIFO behavior of the collection that keeps track of underlying, previous function calls. This also implies that a stack trace is printed top-down.
A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.
A Java stack trace is displayed when an error or exception occurs. The stack trace, also called a backtrace, consists of a collection of stack records, which store an application's movement during its execution.
It traces the locations where exception raised. It collects the information of all the methods that are invoked by a program. When we do not care about the unhandled exceptions and the program throws the exceptions then Java stack trace prints the stack trace on the console by default.
OmitStackTraceInFastThrow
is an optimization in C2 compiled code to throw certain implicit exceptions without stack traces.So, in HotSpot JVM an exception won't have a stack trace, if all of the following conditions are met: 1) a throwing method is hot, i.e. compiled by C2; 2) it is an implicit exception, e.g. NPE thrown by obj.method()
, but not by throw new NullPointerException()
; 3) an exception is thrown at least for the second time.
The purpose of the optimization is to eliminate performance impact of those (arguably rare) cases when an implicit exception is repeatedly thrown on the fast path. In my opinion, this isn't a normal situation. Exceptions, especially implicit, typically denote an error condition which needs to be fixed. In this sense, it's OK to disable -XX:-OmitStackTraceInFastThrow
. E.g. in our production environment we always disable it, and it saved us much debugging time. However, I admit that if such an optimization exists, there were cases where it helped to solve performance issues.
TL;DR Adding -XX:-OmitStackTraceInFastThrow
option can be indeed a good idea, unless there are many implicit exceptions on a hot path in your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With