title: What happens when there's insufficient memory to throw an OutOfMemoryError?
body: <p>I am aware that every object requires heap memory and every primitive/reference on the stack requires stack memory.</p>
  
  <p>When I attempt to create an object on the heap and there's insufficient memory to do so, the JVM creates an <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html">java.lang.OutOfMemoryError</a> on the heap and throws it to me.</p>
  
  <p>So implicitly, this means that there is some memory reserved by the JVM on startup.</p>
  
  <p>What happens when this reserved memory is used up (it would definitely be used up, read discussion below) and the JVM does not have enough memory on the heap to create an instance of <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html">java.lang.OutOfMemoryError</a>?</p>
  
  <p>Does it just hang? Or would he throw me a <code>null</code> since there's no memory to <code>new</code> an instance of OOM ?</p>
  
  <pre><code>try {
      Object o = new Object();
      // and operations which require memory (well.. that's like everything)
  } catch (java.lang.OutOfMemoryError e) {
      // JVM had insufficient memory to create an instance of java.lang.OutOfMemoryError to throw to us
      // what next? hangs here, stuck forever?
      // or would the machine decide to throw us a "null" ? (since it doesn't have memory to throw us anything more useful than a null)
      e.printStackTrace(); // e.printStackTrace() requires memory too.. =X
  }
  </code></pre>
  
  <p>==</p>
  
  <p><strong>Why couldn't the JVM reserve sufficient memory?</strong></p>
  
  <p>No matter how much memory is reserved, it is still possible for that memory to be used up if the JVM does not have a way to "reclaim" that memory:</p>
  
  <pre><code>try {
      Object o = new Object();
  } catch (java.lang.OutOfMemoryError e) {
      // JVM had 100 units of "spare memory". 1 is used to create this OOM.
      try {
          e.printStackTrace();
      } catch (java.lang.OutOfMemoryError e2) {
          // JVM had 99 units of "spare memory". 1 is used to create this OOM.
          try {
              e.printStackTrace();
          } catch (java.lang.OutOfMemoryError e3) {
              // JVM had 98 units of "spare memory". 1 is used to create this OOM.
              try {
                  e.printStackTrace();
              } catch (java.lang.OutOfMemoryError e4) {
                  // JVM had 97 units of "spare memory". 1 is used to create this OOM.
                  try {
                      e.printStackTrace();
                  } catch (java.lang.OutOfMemoryError e5) {
                      // JVM had 96 units of "spare memory". 1 is used to create this OOM.
                      try {
                          e.printStackTrace();
                      } catch (java.lang.OutOfMemoryError e6) {
                          // JVM had 95 units of "spare memory". 1 is used to create this OOM.
                          e.printStackTrace();
                          //........the JVM can't have infinite reserved memory, he's going to run out in the end
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Or more concisely:</p>
  
  <pre><code>private void OnOOM(java.lang.OutOfMemoryError e) {
      try {
          e.printStackTrace();
      } catch (java.lang.OutOfMemoryError e2) {
          OnOOM(e2);
      }
  }
  </code></pre>