Heap Dump before/after full GC

Question: How to make a dump before and after a full GC?

JDk already support our requirement. What we need is just enable below red flag. We have two way to chane the VM flags in runtime by jinfo and jconsole.

 

-XX:+HeapDumpAfterFullGC                   Creates heap dump file after full GC
-XX:+HeapDumpBeforeFullGC                Creates heap dump file before full GC
-XX:+HeapDumpOnOutOfMemoryError  Creates heap dump in out-of-memory condition
-XX:HeapDumpPath=<path>                 Specifies path to save heap dumps

 

By jinfo (the 29167 is the java process id)

jinfo  -flag  +HeapDumpBeforeFullGC  29167

jinfo  -flag  -HeapDumpBeforeFullGC   29167

 

By jconsole

The p0 is HeapDumpBeforeFullGC  and p1 is true/false

 

 

Run below test code with VM arguments:  -XX:+PrintGCDetails  -XX:HeapDumpPath="D:\App\svn\dump"  -XX:-DoEscapeAnalysis

You will get two heap files in the specified folder.

import java.io.IOException;
import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;

import com.sun.management.HotSpotDiagnosticMXBean;

public class DumpBeforeFullGC {
    
    public static void main(String[] args) throws IOException{
        
        String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
        bean.setVMOption("HeapDumpBeforeFullGC", "true");
        bean.setVMOption("HeapDumpAfterFullGC", "true");
        System.gc();
        bean.setVMOption("HeapDumpBeforeFullGC", "false");
        bean.setVMOption("HeapDumpAfterFullGC", "false");
        
    }
}
posted @ 2016-03-07 14:57  princessd8251  阅读(3824)  评论(0)    收藏  举报