67

In my application log (using log4j), I see a NullPointerException, but without the stack trace. I know that as an optimization, when an exception occurs many times - the jvm stops producing the stack trace. The problem is the exception occurred some time ago, and all my logs are filled with the exception without the stack trace. Is there a way to "reset" this mechanism, so the next thrown exception will be printed with the full stack trace? I don't want to restart the application, as it is hard to reproduce this bug, and restarting may cause to "go away"...

Thanks!

duduamar
  • 3,816
  • 7
  • 35
  • 54
  • 1
    "I know that as an optimization, when an exception occurs many times - the jvm stops producing the stack trace". You know this how? It's not true. – skaffman Jan 11 '11 at 15:25
  • it can be because of in catch block only the cause of the exception has been logged – jmj Jan 11 '11 at 15:25
  • 4
    Yes it is kind of optimization - see the answer of @dogbane. @Jigar - this is not related to the logging, the exception itself has no stack trace. – duduamar Jan 11 '11 at 15:34
  • 1
    There's a similar question with some more information on the subject on SO. Just posting a link so it can be found from here. http://stackoverflow.com/q/2411487/1407656 – toniedzwiedz Jul 16 '14 at 12:32

1 Answers1

103

Try running with the following JVM property:

-XX:-OmitStackTraceInFastThrow

From the Release Notes:

The compiler in the server VM now provides correct stack backtraces for all "cold" built-in exceptions. 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.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 4
    Thanks, that sounds good. Is it possible to enable this flag on the fly for a running java process? – duduamar Jan 11 '11 at 15:33
  • @duduamar I don't know for certain, but my guess is that you can't. – dogbane Jan 11 '11 at 15:47
  • 1
    @duduamar: There is now an open ticket for changing this flag at runtime - [JDK-8046503 - Ability to reset OmitStackTraceInFastThrow counter](https://bugs.openjdk.java.net/browse/JDK-8046503). Still open though. – sleske Aug 27 '15 at 15:03
  • 3
    Last comment from JDK-8046503 is discouraging: "Turning off OmitStackTraceInFastThrow at runtime is not enough. The affected methods would also need to be recompiled and StackTraceInThrowable needs to be enabled as well. This is a Will-Not-Fix candidate." – Alexander Klimetschek Feb 01 '17 at 23:52