title: Empty methods noticeably slower in Java 11 than Java 8
body: <p>I was comparing the performance of JDK 8 and 11 using <a href="https://openjdk.java.net/projects/code-tools/jmh/" rel="noreferrer">jmh</a> 1.21 when I ran across some surprising numbers:</p>
  
  <pre class="lang-none prettyprint-override"><code>Java version: 1.8.0_192, vendor: Oracle Corporation
  
  Benchmark                Mode  Cnt  Score   Error  Units
  MyBenchmark.emptyMethod  avgt   25  0.362 ± 0.001  ns/op
  
  
  Java version: 9.0.4, vendor: Oracle Corporation
  
  Benchmark                Mode  Cnt  Score    Error  Units
  MyBenchmark.emptyMethod  avgt   25  0.362 ±  0.001  ns/op
  
  
  Java version: 10.0.2, vendor: Oracle Corporation
  
  Benchmark                Mode  Cnt  Score   Error  Units
  MyBenchmark.emptyMethod  avgt   25  0.723 ± 0.001  ns/op
  
  
  Java version: 11.0.1, vendor: Oracle Corporation
  
  Benchmark                Mode  Cnt  Score   Error  Units
  MyBenchmark.emptyMethod  avgt   25  0.724 ± 0.002  ns/op
  </code></pre>
  
  <p>OpenJDK 11 and 12 perform similar to OracleJDK 11. I have omitted their numbers for the sake of brevity.</p>
  
  <p>I understand that microbenchmarks do not indicate the performance behavior of real-life applications. Still, I'm curious where this difference is coming from. <strong>Any ideas?</strong></p>
  
  <hr>
  
  <p>Here is the benchmark in its entirety:</p>
  
  <p><strong>pom.xml</strong>:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;jmh&lt;/groupId&gt;
      &lt;artifactId&gt;empty-method&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;name&gt;JMH benchmark sample: Java&lt;/name&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.openjdk.jmh&lt;/groupId&gt;
              &lt;artifactId&gt;jmh-core&lt;/artifactId&gt;
              &lt;version&gt;${jmh.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.openjdk.jmh&lt;/groupId&gt;
              &lt;artifactId&gt;jmh-generator-annprocess&lt;/artifactId&gt;
              &lt;version&gt;${jmh.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;jmh.version&gt;1.21&lt;/jmh.version&gt;
          &lt;javac.target&gt;1.8&lt;/javac.target&gt;
          &lt;uberjar.name&gt;benchmarks&lt;/uberjar.name&gt;
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.4.1&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;enforce-versions&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;enforce&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;rules&gt;
                                  &lt;requireMavenVersion&gt;
                                      &lt;version&gt;3.0&lt;/version&gt;
                                  &lt;/requireMavenVersion&gt;
                              &lt;/rules&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.8.0&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;compilerVersion&gt;${javac.target}&lt;/compilerVersion&gt;
                      &lt;source&gt;${javac.target}&lt;/source&gt;
                      &lt;target&gt;${javac.target}&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.2.1&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;shade&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;finalName&gt;${uberjar.name}&lt;/finalName&gt;
                              &lt;transformers&gt;
                                  &lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
                                      &lt;mainClass&gt;org.openjdk.jmh.Main&lt;/mainClass&gt;
                                  &lt;/transformer&gt;
                              &lt;/transformers&gt;
                              &lt;filters&gt;
                                  &lt;filter&gt;
                                      &lt;!--
                                              Shading signed JARs will fail without this.
                                              http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                                      --&gt;
                                      &lt;artifact&gt;*:*&lt;/artifact&gt;
                                      &lt;excludes&gt;
                                          &lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;
                                          &lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;
                                          &lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;
                                      &lt;/excludes&gt;
                                  &lt;/filter&gt;
                              &lt;/filters&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
          &lt;pluginManagement&gt;
              &lt;plugins&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-clean-plugin&lt;/artifactId&gt;
                      &lt;version&gt;2.6.1&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-deploy-plugin&lt;/artifactId&gt;
                      &lt;version&gt;2.8.2&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-install-plugin&lt;/artifactId&gt;
                      &lt;version&gt;2.5.2&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.1.0&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-javadoc-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.0.0&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.1.0&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-site-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.7.1&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-source-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.0.1&lt;/version&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                      &lt;version&gt;2.22.0&lt;/version&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/pluginManagement&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>src/main/java/jmh/MyBenchmark.java</strong>:</p>
  
  
  
  <pre><code>package jmh;
  
  import org.openjdk.jmh.annotations.Benchmark;
  import org.openjdk.jmh.annotations.BenchmarkMode;
  import org.openjdk.jmh.annotations.Mode;
  import org.openjdk.jmh.annotations.OutputTimeUnit;
  
  import java.util.concurrent.TimeUnit;
  
  @BenchmarkMode(Mode.AverageTime)
  @OutputTimeUnit(TimeUnit.NANOSECONDS)
  public class MyBenchmark
  {
      @Benchmark
      public void emptyMethod()
      {
      }
  }
  </code></pre>
  
  <p>Here is the Windows-specific script I use. It should be trivial to translate it to other platforms:</p>
  
  <pre><code>set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_192
  call mvn -V -Djavac.target=1.8 clean install
  "%JAVA_HOME%\bin\java" -jar target\benchmarks.jar
  
  set JAVA_HOME=C:\Program Files\Java\jdk-9.0.4
  call mvn -V -Djavac.target=9 clean install
  "%JAVA_HOME%\bin\java" -jar target\benchmarks.jar
  
  set JAVA_HOME=C:\Program Files\Java\jdk-10.0.2
  call mvn -V -Djavac.target=10 clean install
  "%JAVA_HOME%\bin\java" -jar target\benchmarks.jar
  
  set JAVA_HOME=C:\Program Files\Java\oracle-11.0.1
  call mvn -V -Djavac.target=11 clean install
  "%JAVA_HOME%\bin\java" -jar target\benchmarks.jar
  </code></pre>
  
  <p>My runtime environment is:</p>
  
  <pre class="lang-none prettyprint-override"><code>Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T14:41:47-04:00)
  Maven home: C:\Program Files\apache-maven-3.6.0\bin\..
  Default locale: en_CA, platform encoding: Cp1252
  OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
  </code></pre>
  
  <p>More specifically, I am running <code>Microsoft Windows [Version 10.0.17763.195]</code>.</p>