forked from corener/JavaPassDump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumpHeap.jsp
74 lines (69 loc) · 3 KB
/
DumpHeap.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<%@ page import="java.util.*,javax.crypto.*,javax.crypto.spec.*"%>
<%@ page import="com.sun.management.HotSpotDiagnosticMXBean" %>
<%@ page import="java.security.AccessController" %>
<%@ page import="java.security.PrivilegedExceptionAction" %>
<%@ page import="javax.management.MBeanServer" %>
<%@ page import="java.lang.management.ManagementFactory" %>
<%@ page import="javax.management.ObjectName" %>
<%@ page import="java.io.StringWriter" %>
<%@ page import="java.io.PrintWriter" %>
<%!
public static class DumpHeap {
private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
private static volatile HotSpotDiagnosticMXBean hotspotMBean;
public static void dumpHeap(String fileName, boolean live) {
initHotspotMBean();
try {
hotspotMBean.dumpHeap(fileName, live);
} catch (RuntimeException re) {
throw re;
} catch (Exception exp) {
throw new RuntimeException(exp);
}
}
private static void initHotspotMBean() {
if (hotspotMBean == null) {
synchronized (DumpHeap.class) {
if (hotspotMBean == null) {
hotspotMBean = getHotspotMBean();
}
}
}
}
private static HotSpotDiagnosticMXBean getHotspotMBean() {
try {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<HotSpotDiagnosticMXBean>() {
//@Override
public HotSpotDiagnosticMXBean run() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> s = server.queryNames(new ObjectName(HOTSPOT_BEAN_NAME), null);
Iterator<ObjectName> itr = s.iterator();
if (itr.hasNext()) {
ObjectName name = itr.next();
HotSpotDiagnosticMXBean bean =
ManagementFactory.newPlatformMXBeanProxy(server,
name.toString(), HotSpotDiagnosticMXBean.class);
return bean;
} else {
return null;
}
}
});
} catch (Exception exp) {
throw new UnsupportedOperationException(exp);
}
}
}
%><%
try {
String fileName = "/tmp/dumpHeap.hprof";
boolean live = true;
DumpHeap.dumpHeap(fileName, live);
}catch (Exception e){
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
out.println(errors.toString());
}
out.print("done");
%>