Java jmap与jcmd命令dump内存heap堆
作者:互联网
//获取当前Java程序的进程id
long pid = ProcessHandle.current().pid();
//jmap与jcmd两种命令方式效果相同,均为dump内存
String[] cmd1 = {"jmap", "-dump:live,formt=b,file=D:/dump/jmap.hprof", String.valueOf(pid)};
String[] cmd2 = {"jcmd", String.valueOf(pid), "GC.heap_dump", "D:/dump/jcmd.hprof"};
try {
Runtime.getRuntime().exec(cmd1);
Runtime.getRuntime().exec(cmd2);
} catch (IOException e) {
e.printStackTrace();
}
try {
//等待输入任意按钮结束程序。如果没有这一段代码,程序很快返回后,dump内存来不及处理,没有日志输出。
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
标签:Java,String,dump,pid,jcmd,内存,jmap 来源: https://blog.csdn.net/zhangphil/article/details/120983283