- 栈上分配
1.线程私有小对象
2.无逃逸,就在某段代码中使用
3.支持标量替换
无需调整 - 线程本地分配TLAB(Thread Local Allocation Buffer)
1.占用Eden,默认1%
2.多线程的时候不用竞争Eden就可以申请空间,提高效率
3.小对象
无需调整
实验:
开启和关闭逃逸分析、标量替换、线程本地分配后的运行速度
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
结论:运行速度明显变慢。