1 // java -Xms32M -Xmx32M -XX:+AlwaysPreTouch -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -XX:-UseOnStackReplacement -XX:+UseTLAB -XX:CompileCommand='compileonly,Example2b::foo*' -Xlog:gc -XX:+DoPartialEscapeAnalysis -XX:+PrintEscapeAnalysis -XX:+PrintEliminateAllocations -XX:-PrintCompilation -XX:CompileCommand=quiet  Example2b
 2 class Example2b {
 3     public String _cache;
 4 
 5     public String foo(boolean cond) {
 6         String x = null;
 7 
 8         if (cond) {
 9             x = new String("hello");
10         }
11 
12         return x;
13     }
14     public String foo2(boolean cond) {
15         String x = null;
16 
17         if (cond) {
18             x = new String("hello");
19             _cache = x;       // object 'x' escapes
20         }
21 
22         return x;
23     }
24 
25     public String foo3(String str, boolean cond) {
26         if (str == null) {
27             str = new String("hello");
28         }
29         if (cond) {
30             _cache = str;
31         }
32         return str;
33     }
34     public static void main(String[] args)  {
35         Example2b kase = new Example2b();
36         int iterations = 0;
37         try {
38             while (true) {
39                 boolean cond = 0 == (iterations & 0xf);
40                 String s = kase.foo(cond);
41                 if (s != null && !s.equals("hello")) {
42                     throw new RuntimeException("e");
43                 }
44 
45                 kase.foo2(cond);
46                 if (kase._cache != null && !kase._cache.equals("hello")) {
47                     throw new RuntimeException("e2");
48                 }
49 
50                 if (0 == (iterations % 2)) {
51                     s = kase.foo3(null, cond);
52                 } else {
53                     s= kase.foo3(kase._cache, cond);
54                 }
55 
56                 if (s != null && !s.equals("hello")) {
57                     throw new RuntimeException("e3");
58                 }
59 
60                 iterations++;
61             }
62         } finally {
63             System.err.println("Epsilon Test: " + iterations);
64         }
65     }
66 }