逃逸分析是JIT编译器的用来优化代码的一种手段,下面粘贴一下官方的Hot Spot说明。
链接打不开的话可能要扶个梯子往上爬。
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/performance-enhancements-7.html
Escape analysis is a technique by which the Java Hotspot Server Compiler can analyze the scope of a new object’s uses and decide whether to allocate it on the Java heap.
Escape analysis is supported and enabled by default in Java SE 6u23 and later.
The Java Hotspot Server Compiler implements the flow-insensitive escape analysis algorithm described in:
[Choi99] Jong-Deok Choi, Manish Gupta, Mauricio Seffano,
Vugranam C. Sreedhar, Sam Midkiff,
“Escape Analysis for Java”, Procedings of ACM SIGPLAN
OOPSLA Conference, November 1, 1999
Based on escape analysis, an object’s escape state might be one of the following:
-
GlobalEscape – An object escapes the method and thread. For example, an object stored in a static field, or, stored in a field of an escaped object, or, returned as the result of the current method.
-
ArgEscape – An object passed as an argument or referenced by an argument but does not globally escape during a call. This state is determined by analyzing the bytecode of called method.
-
NoEscape – A scalar replaceable object, meaning its allocation could be removed from generated code.
After escape analysis, the server compiler eliminates scalar replaceable object allocations and associated locks from generated code. The server compiler also eliminates locks for all non-globally escaping objects. It does not replace a heap allocation with a stack allocation for non-globally escaping objects.
Some scenarios for escape analysis are described next.
The server compiler might eliminate certain object allocations. Consider the example where a method makes a defensive copy of an object and returns the copy to the caller.
public class Person {
private String name;
private int age;
public Person(String personName, int personAge) {