美文网首页
-XX:-OmitStackTraceInFastThrow参数

-XX:-OmitStackTraceInFastThrow参数

作者: 越陌先生 | 来源:发表于2021-09-15 17:51 被阅读0次

    现象

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


    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

    栈异常信息打印出来了

    相关文章

      网友评论

          本文标题:-XX:-OmitStackTraceInFastThrow参数

          本文链接:https://www.haomeiwen.com/subject/pvtlgltx.html