查找对应的进程id(PID)
linux下使用
ps aux|grep java
找出PID后
输出快照
jmap -dump:format=b,file=/usr/local test1.hprof 2778
jmap -dump:format=b,file=/usr/local test1.hprof 2778
Attaching to core 2778 from executable test1.hprof, please wait...
Error attaching to core file: cannot open binary file
sun.jvm.hotspot.debugger.DebuggerException: cannot open binary file
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach0(Native Method)
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach(LinuxDebuggerLocal.java:286)
at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:673)
at sun.jvm.hotspot.HotSpotAgent.setupDebuggerLinux(HotSpotAgent.java:611)
at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:337)
at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:156)
at sun.jvm.hotspot.tools.Tool.start(Tool.java:191)
at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
at sun.jvm.hotspot.tools.HeapDumper.main(HeapDumper.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.tools.jmap.JMap.runTool(JMap.java:201)
at sun.tools.jmap.JMap.main(JMap.java:130)
找到JDK安装路径
在使用jmap、jstack、jinfo指令时,可能会碰到“Error attaching to core file: Can’t attach to the core file”错误,实际上这个错误很简单,JVM要求你必须使用对应JDK版本jmap、jstack指令来执行。
如果直接使用jmap或者jstack指令,如果要避免这种错误,那么就要到jdk1.8.0_261安装的bin目录下再执行jmap或jinfo指令。
找到jdk路径
/usr/local/jdk1.8.0_261
再次运行
jmap -dump:format=b,file=/usr/local/jdk1.8.0_261/bin/test1.hprof 2778
或者进到JDK/bin下
jmap -dump:format=b,file=head.hprof 2778
无需进到bin下只需要路径正确即可。
jmap -dump:format=b,file=/usr/local/java-project/test11.hprof 22223
查看
jhat -port 9998 jhat -port 9998 /usr/local/jdk1.8.0_261/bin/test1.hprof
#浏览器访问
http://localhost:9998