1 // java -Xms16M -Xmx16M -XX:+AlwaysPreTouch -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -XX:-UseOnStackReplacement -XX:-UseTLAB -XX:CompileOnly='Example3_1.foo' -XX:+DoPartialEscapeAnalysis Example3_1 2 import java.util.ArrayList; 3 4 class Example3_1 { 5 public ArrayList<Integer> _cache; 6 public void foo(boolean cond) { 7 ArrayList<Integer> x = new ArrayList<Integer>(); 8 9 if (cond) { 10 _cache = x; 11 } 12 } 13 14 public void test1(boolean cond) { 15 foo(cond); 16 } 17 18 public static void main(String[] args) { 19 Example3_1 kase = new Example3_1(); 20 // Epsilon Test: 21 // By setting the maximal heap and use EpsilonGC, let's see how long and how many iterations the program can sustain. 22 // if PEA manages to reduce allocation rate, we expect the program to stay longer. 23 // Roman commented it with a resonable doubt: "or your code slow down the program..." 24 // That's why I suggest to observe iterations. It turns out not trivial because inner OOME will implode hotspot. We don't have a chance to execute the final statement... 25 long iterations = 0; 26 try { 27 while (true) { 28 kase.test1(0 == (iterations & 0xf)); 29 if (kase._cache != null) { 30 kase._cache.add(1); 31 } 32 iterations++; 33 } 34 } finally { 35 System.err.println("Epsilon Test: " + iterations); 36 } 37 } 38 }