title: HeapDumpOnOutOfMemoryError works only once on periodical tasks
body: <p>I have a couple of applications that run in specified intervals. To monitor OutOfMemoryError i've decided to enable HeapDumpOnOutOfMemoryError, and before doing this i decided to do some research. Some of applications have maximum heap size of 2GB, so generating multiple heap dumps in rapid succession could eat up all disk space.</p>
  
  <p>I've written a small script to check how it will work.</p>
  
  <pre><code>import java.util.LinkedList;
  import java.util.List;
  
  public class Test implements Runnable{
  
          public static void main(String[] args) throws Exception {
                  new Thread(new Test()).start();
          }
  
          public void run() {
                  while (true) {
                          try{
                                  List&lt;Object&gt; list = new LinkedList&lt;Object&gt;();
                                  while (true){
                                          list.add(new Object());
                                  }
                          }
                          catch (Throwable e){
                                  System.out.println(e);
                          }
                          try {
                                  Thread.sleep(1000);
                          }
                          catch (InterruptedException ignored) {
  
                          }
                  }
          }
  }
  </code></pre>
  
  <p>And here is the result</p>
  
  <pre><code>$ java -XX:+HeapDumpOnOutOfMemoryError -Xmx2M Test                                                                                                                                                        
  java.lang.OutOfMemoryError: Java heap space
  Dumping heap to java_pid25711.hprof ...
  Heap dump file created [14694890 bytes in 0,101 secs]
  java.lang.OutOfMemoryError: Java heap space
  java.lang.OutOfMemoryError: Java heap space
  </code></pre>
  
  <p>It works as i would want it to, but i would like to know why.</p>
  
  <p>Looking at openjdk6 source code i've found the following </p>
  
  <pre><code>void report_java_out_of_memory(const char* message) {
    static jint out_of_memory_reported = 0;
  
    // A number of threads may attempt to report OutOfMemoryError at around the
    // same time. To avoid dumping the heap or executing the data collection
    // commands multiple times we just do it once when the first threads reports
    // the error.
    if (Atomic::cmpxchg(1, &amp;out_of_memory_reported, 0) == 0) {
      // create heap dump before OnOutOfMemoryError commands are executed
      if (HeapDumpOnOutOfMemoryError) {
        tty-&gt;print_cr("java.lang.OutOfMemoryError: %s", message);
        HeapDumper::dump_heap_from_oome();
      }   
  
      if (OnOutOfMemoryError &amp;&amp; OnOutOfMemoryError[0]) {
        VMError err(message);
        err.report_java_out_of_memory();
      }   
    }
  }
  </code></pre>
  
  <p>How does the first if statement work?</p>
  
  <p>EDIT: it seems that heapdump should be created every time message is printed, but it does not happen. Why is that so?</p>