public class HeapDumper extends Object
Commented out because it uses access-restricted classes and some IDEs won't build the project without relaxing the access restriction.
Usage: HeapDumper.dumpHeap("/tmp/foo.hprof");
If the file exists already, it will be replaced.
Courtesy: https://blogs.oracle.com/sundararajan/entry/programmatically_dumping_heap_from_java
See https://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/HotSpotDiagnosticMXBean.html#dumpHeap-java.lang.String-boolean-