Skip to content

Commit fe3bf74

Browse files
authored
Merge pull request eugenp#5248 from asturcon/heapDump
BAEL-2171 - Different Ways to Capture Java Heap Dumps
2 parents 8cf4000 + a48f385 commit fe3bf74

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.heapdump;
2+
3+
import com.sun.management.HotSpotDiagnosticMXBean;
4+
5+
import javax.management.MBeanServer;
6+
7+
import java.io.IOException;
8+
import java.lang.management.ManagementFactory;
9+
import java.nio.file.Paths;
10+
11+
public class HeapDump {
12+
13+
public static void dumpHeap(String filePath, boolean live) throws IOException {
14+
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
15+
HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(
16+
server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
17+
mxBean.dumpHeap(filePath, live);
18+
}
19+
20+
public static void main(String[] args) throws IOException {
21+
String file = Paths.get("dump.hprof").toFile().getPath();
22+
23+
dumpHeap(file, true);
24+
}
25+
}

0 commit comments

Comments
 (0)