线上运行久的Tomcat项目偶尔会在catalina.out日志中出现大量java.lang.NullPointerException,而没有错误的堆栈信息。
查阅资料后得知,是jvm优化导致的,可以参考Oracle官网的说明:
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.
大致意思为jvm为所有的“冷”内置异常提供正确的堆栈回溯信息。从优化考虑,当一个异常被抛出几次,方法可能被重新编译。重新编译后,编译器会选择一个更快的策略–预分配异常(不提供堆栈信息)。说明中同时也给出了禁用预分配异常的方式 -XX:-OmitStackTraceInFastThrow.