/**
* 开启逃逸分析:-XX:+DoEscapeAnalysis
* 关闭逃逸分析:-XX:-DoEscapeAnalysis
* -XX:+EliminateLocks开启锁消除(jdk1.8默认开启,其它版本未测试)
* -XX:-EliminateLocks 关闭锁消除
**/
public class Main {
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 5000000; i++) {
alloc();
lock("TestLockEliminate ", "Suffix");
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static String lock(String s1, String s2) {
StringBuffer sb = new StringBuffer();
sb.append(s1);
sb.append(s2);
return sb.toString();
}
/**
* 栈上分配
*/
private static void alloc() {
User user = new User();
user.setName("a");
}
@Data
static class User {
private String name;
private String age;
}
}
网友评论