现象
今天遇到个奇怪的问题 前端用户访问出错了 后台没有错误日志出现

然后去找再早一点的日志 发现 有异常抛出

寻找原因
当时 和同事觉得很奇怪 难道是有地方根据日志量智能进行了打印优化?
遂进行百度 过不其然 发现了jvm会 默认对于异常信息堆栈进行优化 以节省资源消耗
https://www.cnblogs.com/yeqfa/p/10116024.html
后去官方文档寻找
https://www.oracle.com/java/technologies/javase/release-notes-introduction.html
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在server模式下 基于性能考虑 同一异常抛出多次后 编译器会重新编译该方法 并且不提供异常栈信息。
可以通过 -XX:-OmitStackTraceInFastThrow 取消这一设置
验证
下面进行验证
main方法循环抛出异常
public static void main(String[] args) {
String name = null;
int i =0;
while (i<115900){
synchronized (NullPointExceptionTest.class){
try {
System.out.println(i);
i ++;
Thread.sleep(1);
name.length();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
异常信息如下

可以看出循环115711次后 栈异常信息不打印了
main方法启动 添加启动参数 -XX:-OmitStackTraceInFastThrow

再次执行main方法

栈异常信息打印出来了
完