Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

83
LINES

< > BotCompany Repo | #1006298 // Dump Java Heap to heap.bin [works]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (905L/7K/21K).

1  
!7
2  
3  
import javax.management.MBeanServer;
4  
import java.lang.management.ManagementFactory;
5  
import com.sun.management.HotSpotDiagnosticMXBean;
6  
7  
// This is the name of the HotSpot Diagnostic MBean
8  
private static final String HOTSPOT_BEAN_NAME =
9  
     "com.sun.management:type=HotSpotDiagnostic";
10  
11  
// field to store the hotspot diagnostic MBean 
12  
private static volatile HotSpotDiagnosticMXBean hotspotMBean;
13  
14  
/**
15  
 \* Call this method from your application whenever you 
16  
 \* want to dump the heap snapshot into a file.
17  
 \*
18  
 \* @param fileName name of the heap dump file
19  
 \* @param live flag that tells whether to dump
20  
 \*             only the live objects
21  
 \*/
22  
static void dumpHeap(String fileName, boolean live) {
23  
    // initialize hotspot diagnostic MBean
24  
    initHotspotMBean();
25  
    try {
26  
        new File(fileName).delete();
27  
        hotspotMBean.dumpHeap(fileName, live);
28  
    } catch (RuntimeException re) {
29  
        throw re;
30  
    } catch (Exception exp) {
31  
        throw new RuntimeException(exp);
32  
    }
33  
}
34  
35  
// initialize the hotspot diagnostic MBean field
36  
private static void initHotspotMBean() {
37  
    if (hotspotMBean == null) {
38  
        synchronized (main.class) {
39  
            if (hotspotMBean == null) {
40  
                hotspotMBean = getHotspotMBean();
41  
            }
42  
        }
43  
    }
44  
}
45  
46  
// get the hotspot diagnostic MBean from the
47  
// platform MBean server
48  
private static HotSpotDiagnosticMXBean getHotspotMBean() {
49  
    try {
50  
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
51  
        HotSpotDiagnosticMXBean bean = 
52  
            ManagementFactory.newPlatformMXBeanProxy(server,
53  
            HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
54  
        return bean;
55  
    } catch (RuntimeException re) {
56  
        throw re;
57  
    } catch (Exception exp) {
58  
        throw new RuntimeException(exp);
59  
    }
60  
}
61  
62  
p {
63  
  // default heap dump file name
64  
  String fileName = prepareProgramFile("heap.bin").getAbsolutePath();
65  
  // by default dump only the live objects
66  
  boolean live = true;
67  
68  
  // simple command line options
69  
  switch (args.length) {
70  
      case 2:
71  
          live = args[1].equals("true");
72  
      case 1:
73  
          fileName = args[0];
74  
  }
75  
76  
  // dump the heap
77  
  dumpHeap(fileName, live);
78  
  
79  
  print("Saved: " + fileName + " (" + fileLength(fileName) + " bytes)");
80  
  print("If you have jhat, you can now run:");
81  
  print("  jhat " + bashQuote(fileName));
82  
  print("...and then go to localhost:7000 in the browser.");
83  
}

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: 451 / 508
Version history: 3 change(s)
Referenced in: [show references]