title: JVM insufficient memory crash
body: <p>I have been experiencing Java VM crashes using the G1 garbage collector. We get hs_err_pid.log files generated with the following signatures:</p>
  
  <pre><code>#
  # There is insufficient memory for the Java Runtime Environment to continue.
  # Native memory allocation (malloc) failed to allocate 32744 bytes for ChunkPool::allocate
  # Possible reasons:
  #   The system is out of physical RAM or swap space
  #   In 32 bit mode, the process size limit was hit
  # Possible solutions:
  #   Reduce memory load on the system
  #   Increase physical memory or swap space
  #   Check if swap backing store is full
  #   Use 64 bit Java on a 64 bit OS
  #   Decrease Java heap size (-Xmx/-Xms)
  #   Decrease number of Java threads
  #   Decrease Java thread stack sizes (-Xss)
  #   Set larger code cache with -XX:ReservedCodeCacheSize=
  # This output file may be truncated or incomplete.
  </code></pre>
  
  <p>We are currently monitoring the memory availability and preempting any out of memory errors by using Runtime.maxMemory, freeMemory and totalMemory. The idea is that we can pause operations and warn the user that they need to allocate more memory. But we are seeing the above JVM crashes even when the Runtime.freeMemory is reporting lots of free memory.</p>
  
  <p>Is there any way as a java desktop application we can avoid this happening and insulate ourselves from memory load on the system.
  For example is there any combination of start up options that we can provide i.e. would setting -Xms and -Xmx to the same value help us here? At the moment we only set -Xmx.</p>
  
  <p>I am keen to avoid the poor user experience of the Jvm silently crashing. Ideally we would like to be able to detect when the JVM is getting close to getting out of memory and take appropriate action.</p>
  
  <p>Here is a bit more information taken from the hs_err_pid.log for one example crash. This was using -Xmx4g, total physical memory 12gb, free physical memory 1.79gb.</p>
  
  <pre><code>Native memory allocation (malloc) failed to allocate 32744 bytes for ChunkPool::allocate
  
  Heap:
   garbage-first heap   total 4194304K, used 3140260K [0x00000006c0000000, 0x00000006c0108000, 0x00000007c0000000)
    region size 1024K, 1526 young (1562624K), 26 survivors (26624K)
   Metaspace       used 78244K, capacity 95308K, committed 96328K, reserved 1122304K
    class space    used 11319K, capacity 22311K, committed 23112K, reserved 1048576K
  
  Memory: 4k page, physical 12269248k(1790928k free), swap 37973052k(362096k free)
  </code></pre>