title: Why does JMH run different forks?
body: <p>I am using the JMH benchmarking framework (<a href="http://openjdk.java.net/projects/code-tools/jmh/">http://openjdk.java.net/projects/code-tools/jmh/</a>) to run benchmarks on my code. My understanding is that JMH forks the JVM multiple times during benchmarking in order to discard any profiles built up by the just-in-time (JIT) profiling performed by the JVM during execution.</p>
  
  <p>I understand why this is useful in some cases such as the below (copied verbatim from <a href="http://java-performance.info/jmh/">http://java-performance.info/jmh/</a>):</p>
  
  <blockquote>
    <p>By default JHM forks a new java process for each trial (set of iterations). This is required to defend the test from previously collected “profiles” – information about other loaded classes and their execution information. For example, if you have 2 classes implementing the same interface and test the performance of both of them, then the first implementation (in order of testing) is likely to be faster than the second one (in the same JVM), because JIT replaces direct method calls to the first implementation with interface method calls after discovering the second implementation.</p>
  </blockquote>
  
  <p>However, in the case where you are benchmarking the same code repeatedly is there any advantage to running say 10 forks of 20 iterations each instead of 1 fork with 200 iterations?</p>
  
  <p>Many Thanks,</p>
  
  <p>Danny</p>