Libraryless. Click here for Pure Java version (905L/7K/21K).
!7 import javax.management.MBeanServer; import java.lang.management.ManagementFactory; import com.sun.management.HotSpotDiagnosticMXBean; // 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 { new File(fileName).delete(); 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 (main.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); } } p { // default heap dump file name String fileName = prepareProgramFile("heap.bin").getAbsolutePath(); // 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); print("Saved: " + fileName + " (" + fileLength(fileName) + " bytes)"); print("If you have jhat, you can now run:"); print(" jhat " + bashQuote(fileName)); print("...and then go to localhost:7000 in the browser."); }
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1006298 |
Snippet name: | Dump Java Heap to heap.bin [works] |
Eternal ID of this version: | #1006298/4 |
Text MD5: | f4dc61bef691fb9973927bc26696df28 |
Transpilation MD5: | 24e3dfdc0e7e646b91430248de19d703 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-03-21 11:47:07 |
Source code size: | 2506 bytes / 83 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 502 / 575 |
Version history: | 3 change(s) |
Referenced in: | [show references] |