I have several applications that run at regular intervals. To track OutOfMemoryError, I decided to turn on HeapDumpOnOutOfMemoryError, and before that I decided to do some research. In some applications, the maximum heap size is 2 GB, so generating multiple heap heaps in quick succession can eat up all of the disk space.
I wrote a small script to check how it will work.
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<Object> list = new LinkedList<Object>(); while (true){ list.add(new Object()); } } catch (Throwable e){ System.out.println(e); } try { Thread.sleep(1000); } catch (InterruptedException ignored) { } } } }
And here is the result
$ 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
It works as I would like, but I would like to know why.
Looking at openjdk6 source code, I found the following
void report_java_out_of_memory(const char* message) { static jint out_of_memory_reported = 0;
How does the first if statement work?
EDIT: It seems that heapdump should be created every time a message is displayed, but this does not happen. Why is this so?
source share