-XX:-OmitStackTraceInFastThrow参数解决错误日志不打印问题

现象

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


image.png

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


image.png

寻找原因

当时 和同事觉得很奇怪 难道是有地方根据日志量智能进行了打印优化?
遂进行百度 过不其然 发现了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();
                }
            }

        }
    }

异常信息如下


image.png

可以看出循环115711次后 栈异常信息不打印了

main方法启动 添加启动参数 -XX:-OmitStackTraceInFastThrow


image.png

再次执行main方法


image.png

栈异常信息打印出来了

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容