Poison

HotSpotDiagnosticMXBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public final class HeapDumpUtil {

private HeapDumpUtil() {
throw new AssertionError();
}

public static File heapDump() {
String uniqueFileName = OssUtil.generateUniqueFileName(LocalDateTime.now(), "heap_dump.hprof");
String dumpFilePath = Constants.TMP_DIR_PATH + File.separator + uniqueFileName;

try {
ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class).dumpHeap(dumpFilePath, true);
} catch (IOException e) {
throw new RuntimeException(e);
}

return new File(dumpFilePath);
}

}
References

HotSpotDiagnosticMXBean (Monitoring and Management Interface for the Java Platform )
Using the Platform MBean Server and Platform MXBeans - JavaSE Monitoring and Management Guide