强制要求JVM始终抛出含堆栈的异常(-XX:-OmitStackTraceInFastThrow)

来源:互联网 发布:qt高级编程 编辑:程序博客网 时间:2024/05/28 15:07
强制要求JVM始终抛出含堆栈的异常(-XX:-OmitStackTraceInFastThrow)问题描述:生产环境抛异常,但却没有将堆栈信息输出到日志,可以确定的是日志输出时用的是log.error("xx发生错误", e)问题分析:它跟JDK5的一个新特性有关,对于一些频繁抛出的异常,JDK为了性能会做一个优化,即JIT重新编译后会抛出没有堆栈的异常          而在使用-server模式时,该优化选项是开启的,因此在频繁抛出某个异常一段时间后,该优化开始起作用,即只抛出没有堆栈的异常信息问题解决:由于该优化是在JIT重新编译后才起作用,因此起初抛出的异常还是有堆栈的,所以可以查看较旧的日志,寻找完整的堆栈信息          另一个解决办法是暂时禁用该优化,即强制要求每次都要抛出有堆栈的异常,幸好JDK提供了通过配置JVM参数的方式来关闭该优化          即-XX:-OmitStackTraceInFastThrow,便可禁用该优化了(注意选项中的减号,加号则表示启用)官方说明: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.