无法打印出Error堆栈信息

  • 现象,在查看线上的错误日志的时候,看到一些报空指针的异常,却没有堆栈信息,赶紧去看代码是谁写的,捕获异常不打印堆栈信息,还让怎么修复。一看惊呆了,居然是自己。而且,明明已经是logger.error("xxxxxxxError", e),为什么不打印堆栈呢。
  • 经过查询资料才发现:JIT编译器对异常进行了优化,如果在同一个位置多次抛出相同的异常,则会采用FastThrow的方案来处理——抛出一个之前分配好的,类型匹配的异常对象。优点也很明显,抛出速度特别快,对服务器影响很小,不需要额外的分配内存。成本就是看不到堆栈信息了。
  • 解决:
    1. 知道了原理,当然可以直接去找上一次服务器重启的时间节点,向后找最早出现异常的点,那里是有堆栈信息的;
    2. 也可以禁止这种模式,添加额外的JVM参数:-XX:-OmitStackTraceInFastThrow ;(不推荐)
  • 附:
    1. 官方解释:
      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.

       

posted @ 2019-06-08 16:41  Cooqer  阅读(1072)  评论(0编辑  收藏  举报