Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does JVM start to omit stack traces?

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?

like image 732
Mohamed Anees A Avatar asked Nov 04 '19 15:11

Mohamed Anees A


People also ask

What causes a stack trace error?

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.

What is stack trace exception?

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.

Does error contain stack trace in Java?

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.

How does stack trace work in Java?

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.


1 Answers

  1. OmitStackTraceInFastThrow is an optimization in C2 compiled code to throw certain implicit exceptions without stack traces.
  2. The optimization applies only to implicit exceptions, i.e. exceptions thrown by JVM itself, not by user code. These implicit exceptions are:
    • NullPointerException
    • ArithmeticException
    • ArrayIndexOutOfBoundsException
    • ArrayStoreException
    • ClassCastException
  3. Stack traces are omitted, only if JVM knows the exception has happened earlier at this particular place.

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.

like image 167
apangin Avatar answered Sep 29 '22 05:09

apangin