Java heap dump & shut down - what order?

前端 未结 5 1340
暖寄归人
暖寄归人 2021-02-07 08:35

I would like to detect an OutOfMemoryError, take a heap dump, and automatically exit the Java program. Say I have the following command-line arguments for my JVM:

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 08:46

    If you are using OpenJDK you can be sure when you are going to run the command set by -XX:OnOutOfMemoryError option.

    Code taken from the OpenJDK source code. See: debug.cpp

    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, &out_of_memory_reported, 0) == 0) {
        // create heap dump before OnOutOfMemoryError commands are executed
        if (HeapDumpOnOutOfMemoryError) {    
          tty->print_cr("java.lang.OutOfMemoryError: %s", message);
          HeapDumper::dump_heap_from_oome();
        }
    
        if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
          VMError err(message);
          err.report_java_out_of_memory();
        }
      }
    } 
    

    Just in case a short explanation:

    1. First of all check if the HeapDumpOnOutOfMemoryError option was set. In that case run dump_heap_from_oome()
    2. Sencondly if the OnOutOfMemoryError option was set, run report_java_out_of_memory()

    So, for sure if you are using OpenJDK your process will dump memory and then quit.

提交回复
热议问题