我已经按照测试类来检查JIT编译器逻辑:
public static final int COUNT = 2_000_000_000;
public static final MyLogger LOG = new MyLogger(false);
//Here IS_DEBUG is false
public static final boolean IS_DEBUG = LOG.isDebug();
private void run() throws Exception
{
System.out.println(getSum(COUNT));
//Compilation without OSR
System.out.println(getSum(COUNT + 2));
//Change value IS_DEBUG -> true over reflection
setFinalStatic(TestDeadCode.class.getField("IS_DEBUG"), true);
//Show true
System.out.println(IS_DEBUG);
COUNT = COUNT / 2;
System.out.println(getSum(COUNT + 3));
}
private int getSum(int count)
{
int result = 0;
for (int j = 0; j < count; j++)
{
result = result + 1;
if (IS_DEBUG)
{
//Dead code here
System.out.println("debug: " + result);
}
}
return result;
}
如果我调用方法run()
,那么“死代码”
System.out.println("debug: " + result);
。是JVM错误吗?
Java版:
Java(TM)SE运行时环境(版本1.7.0_17-b02)
Java HotSpot(TM)64位服务器VM(内置23.7-b01,混合模式)
更新:PrintCompilation输出:
79 1 % com.nau.sample.deadcode.TestDeadCode::getSum @ 7 (34 bytes) 85 1 com.nau.sample.deadcode.TestDeadCode::getSum (34 bytes)