title: JVM Out of Memory Causes
body: <p>Questions are based on Oracle Hotspot JDK8.</p>
  <p>When applicaton come across <code>java.lang.OutOfMemory: Java heap space</code> exception, I suppose, where are two possible reasons.</p>
  <ol>
  <li>Allocated JVM heap size reaches <code>-Xmx</code> specified size and GC system cann't squeeze out enough space.</li>
  <li>Allocated JVM heap doesn't reach <code>-Xmx</code>, but there are not enough physical memory for JVM heap to grow. Suppose <code>-Xms</code> &lt; <code>-Xmx</code>.</li>
  </ol>
  <p>I know <code>1</code> is a reason for JVM to throw out <code>java.lang.OutOfMemory: Java heap space</code> exception. <strong>Is <code>2</code> a reasonable one cause?</strong></p>
  <p>I find <a href="https://www.ibm.com/mysupport/s/question/0D50z000062kD0w/what-is-the-difference-between-native-heap-memory-and-java-heap-memory?language=en_US" rel="nofollow noreferrer">some articles</a> mentioned <code>java.lang.OutOfMemoryError: native memory exhausted</code>, but they are all limited to IBM website. <strong>Is this expetion limited to IBM implemented JVM or it is a standard expetion in JVM Specification?</strong></p>
  <hr />
  <p>I did some experiments with code supplied by @Eugene in answer. As @Holger noted the result varies in different environments. I tested in on both CentOS x64 and Win7 x64, with Hotspot JDK8 x64.
  <strong>For simplicity swap and virtual memory are disabled.</strong></p>
  <p>I increase memory bound (-Xmx and -Xms) step by step.</p>
  <p>I. -Xmx &lt; <em>available logic memory</em></p>
  <ul>
  <li>On both CentOS and Windows it shows <strong>OutOfMemoryError: Java heap space</strong></li>
  </ul>
  <p>II. <em>available logic memory</em> &lt; -Xmx &lt; <em>max physical memory</em></p>
  <ul>
  <li>CentOS: The GC try to Full GC serveral times, with <em>Allocation Failure</em>, and process be killed by system, leaving a message <strong>Killed</strong>.</li>
  <li>Windows: The GC try to Full GC serveral times, with <em>Allocation Failure</em>, and throw out  <strong>OutOfMemoryError: Java heap space</strong></li>
  </ul>
  <p>III. -Xmx &gt; <em>max physical memory</em></p>
  <ul>
  <li>CentOS: same as in II</li>
  <li>Windows: same as in II</li>
  </ul>
  <p>IV. -Xms &gt; <em>max physical memory</em></p>
  <ul>
  <li>CentOS: JVM seems fail to start. Error message is like:</li>
  </ul>
  <blockquote>
  <p>Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000e62a0000, 349569024, 0) failed; error='Cannot allocate memory' (errno=12)</p>
  </blockquote>
  <ul>
  <li>Windows: JVM failed to start. Error message is like:</li>
  </ul>
  <blockquote>
  <p>Error occurred during initialization of VM
  Could not reserve enough space for object heap</p>
  </blockquote>
  <hr />
  <p>So, the same JVM behave differently in different OS.</p>
  <ul>
  <li>On windows, the OS doesn't kill JVM.  And JVM always throw out <strong>OutOfMemoryError: Java heap space</strong> when memory usage grow exceeds.</li>
  <li>On Linux, the OS kill processes when there is not enough memory.</li>
  <li>On both OS JVM failed to start when available memory doesn't satisfy JVM minimal requirement.</li>
  </ul>