title: Why do -Xmx and Runtime.maxMemory not agree
body: <p>When you add</p>
  
  <pre><code> -Xmx????m
  </code></pre>
  
  <p>to the command line, the JVM gives you a heap which is close to this value but can be out by  up to 14%.  The JVM can give you a figure much closer to what you want, but only through trial and error.</p>
  
  <pre><code> System.out.println(Runtime.getRuntime().maxMemory());
  </code></pre>
  
  <p>prints</p>
  
  <pre><code>-Xmx1000m -&gt;  932184064
  -Xmx1024m -Xmx1g -&gt;  954728448
  -Xmx1072m -&gt;  999292928
  -Xmx1073m -&gt; 1001390080
  </code></pre>
  
  <p>I am running HotSpot Java 8 update 5.</p>
  
  <p>Clearly, the heap can be something just above <code>1000000000</code> but why is this <code>-Xmx1073m</code> instead of say <code>-Xmx1000m</code>?</p>
  
  <p>BTW <code>1g</code> == <code>1024m</code> which suggests that <code>1g</code> should be 1024^3 which is 7% higher than 1000^3 but you get something 7% lower than 1000^3.</p>
  
  <hr>
  
  <p>Being off by so much suggests that I am missing something fundamental about how the heap works.  If I asked for -Xmx1000m and it was <code>1001390080</code> I wouldn't care, I would assume there is some allocation multiple it needs to adhere to, but to give you <code>932184064</code> suggests to me the heap is more complicated than I can imagine.</p>
  
  <hr>
  
  <p>EDIT  I have found that</p>
  
  <pre><code>-Xmx1152m gives 1073741824 which is exactly 1024^3
  </code></pre>
  
  <p>so it appears it is giving me exactly 128 MB less than I asked for in this case cf the maxMemory().</p>
  
  <hr>
  
  <p>BTW 128 is my favourite number.  I was in a conference today at street number <code>128</code> and the speaker quoted a book from page <code>128</code> ;)</p>