title: -XX:+ExitOnOutOfMemoryError ignored on 'java.lang.OutOfMemoryError: Direct buffer memory'
body: <p>If the <code>OutOfMemoryError</code> is caused by allocating a direct byte buffer than the JVM flag <code>-XX:+ExitOnOutOfMemoryError</code> is ignored.</p>
  
  <p>Checked on Oracle JDK and OpenJDK
  Java version "1.8.0_144"
  Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
  Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)</p>
  
  <p>OS: Ubuntu 16.04</p>
  
  <p>to reproduce run the following code</p>
  
  <pre><code>package com.company;
  import java.nio.ByteBuffer;
  public class Main {
  
      public static void main(String[] args) {
          // This should guarantee to throw:            
          try {
              ByteBuffer bb = ByteBuffer.allocateDirect(10485760*2);
              System.out.println("OOME not triggered");
          } catch (OutOfMemoryError err) {                
              System.out.println("OOME didn't terminate JVM!");
          }
      }
  }
  </code></pre>
  
  <p>jvm args: <code>-XX:MaxDirectMemorySize=10485760 -XX:+ExitOnOutOfMemoryError</code></p>
  
  <p>The program outputs "OOME didn't terminate JVM!" and terminates with exit code 0.
  I expected it to crash and return exit code greater than 0. It works in this way when <code>OutOfMemory</code> is thrown due to low heap space.</p>
  
  <p>If you run the following code with jvm args <code>-Xmx10485760 -XX:+ExitOnOutOfMemoryError</code> then the jvm process will crash with exit code 3:</p>
  
  <pre><code>try {        
      byte[] b = new byte[10485760*2];
      System.out.println("OOME not triggered");
  } catch (OutOfMemoryError err) {
      System.out.println("OOME didn't terminate JVM!");
  }
  </code></pre>
  
  <p>The output of the code above will be:
  <code>Terminating due to java.lang.OutOfMemoryError: Java heap space</code>
  And this is the expected behavior but that is not reproduced with the first example.</p>
  
  <p>How to make <code>ExitOnOutOfMemoryError</code> option to work for this case?
  Maybe there is an updated JDK or alternative JDK implementation that doesn't have such bug?</p>
  
  <p>I have this problem with druid.io. If real-time index task encounters  </p>
  
  <pre><code>java.lang.OutOfMemoryError: Direct buffer memory
  </code></pre>
  
  <p>it hangs and doesn't terminate.</p>