title: How to make JVM use the max (all remain) memory of a server
body: <p>I have an DFS algorithm java console application, that runs faster when more memory is provided. Just a DFS algorithm application, with neither I/O nor other outer-JVM resource usage. It consumes only CPU and memory. The application can run with an 1GB memory, but run much more faster with 2 GB memory. More memory provided, faster the application can run. I haven't touch the speed limit as 12GB of memory provided. So I must use all remain memory of a server to speed it up. And the application need not parallel, one request only at one time.</p>
  
  <p>And I need to install the application on different server with different memory size.</p>
  
  <p>Is there a way to let JVM use the all remain memory of the server?</p>
  
  <pre><code>-XX:MaxRAMFraction=1
  </code></pre>
  
  <p>MaxRAMFraction is not EVERY server good, some server will result in start JVM failure as location memory failure, some works good.</p>
  
  <p>Use an wrapper application get system remain memory, and minus some memory usage other than Xmx, then start the real application with same Xms and Xms. The method will also result in JVM memory allocation error. Because the code below returns much more than memory we can use, not just a minus of Xss256m or some more non-heap JVM memory.</p>
  
  <pre><code>com.sun.management.OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
      ManagementFactory.getOperatingSystemMXBean();
  long size = mbean.getFreePhysicalMemorySize();
  </code></pre>
  
  <p>So is there a good way to let JVM use all remain memory of a server?</p>