C2 compiled引发的问题追踪

问题现象:

运行压力测试代码中抛错
异常代码
定位到抛错的代码如下:
【抛错代码
获取异常堆栈数组的时候,返回的是空数组,导致出现数组越界。

排查过程

由于是测试反馈到这边的问题,给的日志和代码都是测试给截图过来的,信息量不大。
但是根据图里面的方法来看,问题肯定是获取堆栈异常的时候返回了空数组,正常来说异常堆栈至少是有当前线程的堆栈信息的,怎么会返回的是空数组呢?
首先看了java.lang.Throwable#getStackTrace 方法的描述,

Provides programmatic access to the stack trace information printed by printStackTrace(). Returns an array of stack trace elements, each representing one stack frame. The zeroth element of the array (assuming the array’s length is non-zero) represents the top of the stack, which is the last method invocation in the sequence. Typically, this is the point at which this throwable was created and thrown. The last element of the array (assuming the array’s length is non-zero) represents the bottom of the stack, which is the first method invocation in the sequence.
Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method. Generally speaking, the array returned by this method will contain one element for every frame that would be printed by printStackTrace. Writes to the returned array do not affect future calls to this method.
Returns:
an array of stack trace elements representing the stack trace pertaining to this throwable.
Since:
1.4

注意到中间是有描述一些情况返回空数组的。

Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method.

顺着这个注释上网搜了一下
https://stackoverflow.com/questions/58696093/when-does-jvm-start-to-omit-stack-traces
找到了一些相关的回答,其中也解答了会出现返回空数组的原因。

原因

JVM对一些特定的异常类型做了Fast Throw优化,如果检测到在代码里某个位置连续多次抛出同一类型异常的话,C2 compiled会决定用Fast Throw方式来抛出异常,而异常Trace即详细的异常栈信息会被清空。
这就导致了如果这个异常重复抛出,可能会出现异常堆栈数组被清空长度为0的现象。
也就出现了上图数组越界的问题。

验证

public static void main(String[] args) {
        int counter = 0;
        while(true) {
            try {
                Object obj = null;
                if(counter % 2 == 0) {
                    obj = new Object();
                }
                // Cause NullpointerException
                obj.getClass();
            }catch(NullPointerException e) {
                e.printStackTrace();
            }
            counter++;
        }
    }

代码就是一直抛出NullPointerException,当异常到一定数量的时候会发现,抛出的异常有了变化,代表着此时异常堆栈已经被优化清除掉了。

解决方法

通过 -XX:-OmitStackTraceInFastThrow 禁用该默认的优化,使得异常堆栈一直完整的抛出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mirt_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值