Back to Project Page Home
Escape Analysis Demonstration
We apply ITA control flow escape analysis combined with intraprocedural escape analysis to the following code, using Class1.method1 as the entry point.
Code
class Class1 {
Class4 field1;
Class1() {}
void method1() {
try {
method2(new Class1());
} catch(Class2 exception) {
exception.method1();
Class4 local = method7(exception);
if(local != null) {
local.method1();
}
field1 = new Class4();
if(exception instanceof Class3) {
((Class3) exception).method3(field1);
}
throw exception;
}
}
static void method2(Class1 arg) {
arg.method3();
}
void method3() {
method4(null);
}
void method4(Class3 arg) {
method5(arg);
}
Class3 method5(Class3 arg) {
Class3 local = method6();
Class3.method2(this);
throw local;
}
Class3 method6() {
Class3 local = new Class3();
local.field4.method1();
return local;
}
Class4 method7(Class2 arg) {
return null;
}
}
class Class2 extends java.lang.RuntimeException {
boolean field2;
Class2 field3;
Class2() {}
Class4 method1() {
field2 = true;
return null;
}
}
class Class3 extends Class2 {
Class4 field4;
static Class4 field5;
Class3() {
field4 = new Class4();
}
/**
* overrides method7 in parent class
*/
Class4 method1() {
return null;
}
static void method2(Class1 arg) {
field5 = null;
}
final void method3(Class4 arg) {
field5 = arg;
}
}
class Class4 {
char[] field5;
Class4() {}
void method1() {}
}
Here are the class representations. Fields of primitive type are ignored. Static fields will be represented just once, while the other fields will have a unique copy in each object instantiated.
Click on the controls above to see the progress of the escape analysis.
Class1.method1 is the initial method, and we create a single generic object of type Class1 to act as the sole object argument for the non-static method.
Generic object 3 is the generic object returned from the generic invocation of Class1.method7. The invocation of Class1.method7 is generic because the object for which the invocation takes place is generic, Object 2. Objects 2 and 4 are normal objects instantiated in method1.
Using the generic Object 3, we have a second generic invocation, an invocation of Class4.method1.
Object 5 is thrown from Class1.method5 as an exception object. Object 6 is a new object instantiated within the constructor of Class3.
Object 4 is written to a static field. At this time ITA control flow propagation has been completed, there are no new method invocactions, field accesses, or object movements that can take place.
Here we can see the roots, the root sources of escape. One root is the argument to the initial method Object 1, another is the object that was thrown from the initial method, Object 5. The two generic method invocations Class1.method7 and Class4.method1 are roots. The static field is the final root.
Here we can see how the various objects escape, or whether they do not escape at all. This demonstration is a simple example, and so we can easily identify the escaped objects. In general the search is not so straightforward, the algorithm searches from the roots to their contained objects, and from these objects to their contained fields and the additional objects contained in these fields, and so on recursively until all reachable objects have been identified. Any objects that have not been reached have not escaped.