原文 http://itindex.net/detail/46666-jvm-fgc-dump
一.需求
有時候web應用經常會發生FGC,我們想知道FGC把那些對象給回收了,思路很簡單就是看看FGC之前內存中有那些實例,FGC之后內存中又有那些實例,通過前后的比較,我們就能很容易知道FGC回收了那些實例,當然我們可以手工去dump內存,在FGC發生之前dump一下內存,再在FGC發生之后dump一下內存,但是這dump的時間點不好把握,能否讓JVM自動去dump就更好了。
二.手工dump內存的方法
jmap -dump:format=b,file=/home/admin/xxx.bin PID
其中PID是java的進程ID
三.讓JVM自己dump內存
在JVM啟動的時候增加下面兩個參數-XX:+HeapDumpBeforeFullGC -XX:+HeapDumpAfterFullGC就可以在FGC的前后dump一下內存。不過增加這兩個參數會在每次FGC的時候dump內存,dump內存本身對應用有影響,但是不會導致JVM停機。特別是在web應用啟動的時候,就會不斷的去dump內存,因為web應用啟動的時候會頻繁發生FGC。
四.使用Java代碼來dump內存
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer; import com.sun.management.HotSpotDiagnosticMXBean; public class HeapDumper { // This is the name of the HotSpot Diagnostic MBean private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; // field to store the hotspot diagnostic MBean private static volatile HotSpotDiagnosticMXBean hotspotMBean; /* * Call this method from your application whenever you * want to dump the heap snapshot into a file. * * @param fileName name of the heap dump file * @param live flag that tells whether to dump * only the live objects */ static void dumpHeap(String fileName, boolean live) { // initialize hotspot diagnostic MBean initHotspotMBean(); try { hotspotMBean.dumpHeap(fileName, live); } catch (RuntimeException re) { throw re; } catch (Exception exp) { throw new RuntimeException(exp); } } // initialize the hotspot diagnostic MBean field private static void initHotspotMBean() { if (hotspotMBean == null) { synchronized (HeapDumper.class) { if (hotspotMBean == null) { hotspotMBean = getHotspotMBean(); } } } } // get the hotspot diagnostic MBean from the // platform MBean server private static HotSpotDiagnosticMXBean getHotspotMBean() { try { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); return bean; } catch (RuntimeException re) { throw re; } catch (Exception exp) { throw new RuntimeException(exp); } } public static void main(String[] args) { // default heap dump file name String fileName = "heap.bin"; // by default dump only the live objects boolean live = true; // simple command line options switch (args.length) { case 2: live = args[1].equals("true"); case 1: fileName = args[0]; } // dump the heap dumpHeap(fileName, live); } }
