逃逸分析、标量替换、线程本地分配

2021-02-16  本文已影响0人  心無旁騖丶
实验:

开启和关闭逃逸分析、标量替换、线程本地分配后的运行速度

public class TestTLAB {
//-XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:-UseTLAB
//逃逸分析               标量替换                   线程本地分配
    class User {
        int id;
        String name;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    void alloc(int i) {
        new User(i, "name" + i);
    }

    public static void main(String[] args) {
        TestTLAB test = new TestTLAB();
        long start = System.currentTimeMillis();

        for (int i = 0; i <= 1000_0000; i++) {
            test.alloc(i);
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

Run -> Edit Configurations -> VM options
关闭逃逸分析、标量替换、线程本地分配
输入:-XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:-UseTLAB
结论:运行速度明显变慢。

上一篇 下一篇

猜你喜欢

热点阅读