title: Why getSum does not get inlined by hotspot jvm?
body: <p>Here's the example I tried to reproduce from <a href="https://books.google.com/books?id=aIhUAwAAQBAJ&amp;lpg=PP1&amp;pg=PA97#v=onepage&amp;q&amp;f=false" rel="noreferrer"><strong>Java Performance: The Definitive Guide, Page 97</strong></a> on the topic of <em>Escape Analysis</em>. This is probably what should happen:</p>
  
  <ol>
  <li><code>getSum()</code> must get hot enough and with appropriate JVM parameters it must be inlined into the caller <code>main()</code>.</li>
  <li>As both <code>list</code> and <code>sum</code> variables do not escape from <code>main()</code> method they could be marked as <code>NoEscape</code> hence JVM could use stack-allocation for them instead of heap-allocation.</li>
  </ol>
  
  <p>But I ran it through <a href="https://github.com/AdoptOpenJDK/jitwatch/" rel="noreferrer">jitwatch</a> and the result showed that <code>getSum()</code> compiles into native-assembly and doesn't get inlined into <code>main()</code>. Not to mention consequently stack-allocation didn't happen either.</p>
  
  <p>What am I doing wrong in here ? (I have put the whole code and hotspot log <a href="https://github.com/nimatrueway/jit-hotspot-analysis-sample" rel="noreferrer">here</a>.)</p>
  
  <p>Here's the code:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.math.BigInteger;
  import java.util.ArrayList;
  import java.util.stream.IntStream;
  
  public class EscapeAnalysisTest {
  
      private static class Sum {
  
          private BigInteger sum;
          private int n;
  
          Sum(int n) {
              this.n = n;
          }
  
          synchronized final BigInteger getSum() {
              if (sum == null) {
                  sum = BigInteger.ZERO;
                  for (int i = 0; i &lt; n; i++) {
                      sum = sum.add(BigInteger.valueOf(i));
                  }
              }
              return sum;
          }
  
      }
  
      public static void main(String[] args) {
          ArrayList&lt;BigInteger&gt; list = new ArrayList&lt;&gt;();
          for (int i = 1; i &lt; 1000; i++) {
              Sum sum = new Sum(i);
              list.add(sum.getSum());
          }
          System.out.println(list.get(list.size() - 1));
      }
  
  }
  </code></pre>
  
  <p>JVM parameters I used:</p>
  
  <pre class="lang-bash prettyprint-override"><code>-server
  -verbose:gc
  -XX:+UnlockDiagnosticVMOptions
  -XX:+TraceClassLoading
  -XX:MaxInlineSize=60
  -XX:+PrintAssembly
  -XX:+LogCompilation
  </code></pre>