Missing stacktraces for exceptions in Java apps

If you log exceptions manually, or use a tool like DripStat to do so automatically, you may sometimes not see a stacktrace at all.

This is specially so for production environments which handle a lot of requests.

Why is the Stacktrace not present?

This happens because the JVM optimizes your code to the point that in order to gain maximum performance, the stack trace is omitted.

Here is a quote from the Java release notes:

For performance purposes, when such an exception is thrown a few times, the method may be recompiled. After recompilation, the compiler may choose a faster tactic using preallocated exceptions that do not provide a stack trace. To disable completely the use of preallocated exceptions, use this new flag:
-XX:-OmitStackTraceInFastThrow

Remedy

As mentioned, you should add the argument -XX:-OmitStackTraceInFastThrow to your JVM launch to see full stack traces.

Does it impact performance?

Barely, if at all. Most typical webapps spend a majority of their time external to the JVM anyway, so the performance impact is minimal, especially compared to the benefit of having a full stacktrace of your exceptions.

Here is a screenshot from our own application, before and after adding the argument. Focus here on just the 'JVM' layer, since that is the only layer impacted by the above. As you can see the time spent in JVM is so minimal, it barely matters.

Before adding argument:

After adding argument:

Of course, if you are using DripStat, you can accurately how much, if at all, adding this argument impacts the performance of your app.

Show Comments