16.徹底解決Jmap在mac版本無法使用的問題

盛開的太陽發表於2021-11-09

徹底解決Jmap在mac版本無法使用的問題

看了網上很多帖子,都說一半,說的都是大家說過的,根本沒有解決問題。說jdk8不行,換成jdk9或者jdk11,我都試了,還是不行,最後說是mac的問題。換成linux,崩潰!!!

一、在mac執行Jmap的問題

1. 我們執行jps查詢剛剛啟動的專案

16.徹底解決Jmap在mac版本無法使用的問題

然後執行Jmap查詢堆記憶體使用情況

Jmap -heap 1518

執行後報錯:

Attaching to process ID 1518, please wait...
ERROR: attach: task_for_pid(1518) failed: '(os/kern) failure' (5)
Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$BsdDebuggerLocalWorkerThread.execute(BsdDebuggerLocal.java:169)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach(BsdDebuggerLocal.java:287)
	at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:671)
	at sun.jvm.hotspot.HotSpotAgent.setupDebuggerDarwin(HotSpotAgent.java:659)
	at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:341)
	at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
	at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
	at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
	at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
	at sun.jvm.hotspot.tools.HeapSummary.main(HeapSummary.java:49)
	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)
Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach0(Native Method)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.access$100(BsdDebuggerLocal.java:65)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$1AttachTask.doit(BsdDebuggerLocal.java:278)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$BsdDebuggerLocalWorkerThread.run(BsdDebuggerLocal.java:144)

2. 百度這個異常

說mac環境jdk8不支援Jmap,需要替換成jdk11。於是在mac上安裝了jdk11。安裝的時候讓jdk8和jdk11共存,原因是,單位專案使用的是jdk8,因此不能刪。

二、Jmap在JDK11的問題

如何配置jdk8和jdk11共存切換,參考這篇文章:cnblogs.com/ITPower/p/15527478.html

1. 檢查當前jdk版本

16.徹底解決Jmap在mac版本無法使用的問題

我已經替換成了jdk11版本,需要注意的是,需要看一下我們啟動的專案的jdk版本是不是11。

2. 檢查idea專案中JDK版本

image

這裡需要注意的是,修改當前專案的jdk版本即可。不要修改系統的。修改當前專案,右擊專案名稱-->Open Module Settings-->SDKs。

3.執行Jmap命令

Error: -heap option used
Cannot connect to core dump or remote debug server. Use jhsdb jmap instead

看錯誤提示說讓替換Jamp為jhsdb jmap,百度其用法

jhsdb jmap --heap --pid 程式號

詳細jhsdb相關資訊檢視文件:https://www.jianshu.com/p/92931e6466b3

可是依然報錯

Attaching to process ID 1525, please wait...
ERROR: attach: task_for_pid(1525) failed: '(os/kern) failure' (5)
Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
	at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$BsdDebuggerLocalWorkerThread.execute(BsdDebuggerLocal.java:169)
	at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach(BsdDebuggerLocal.java:283)
	at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:672)
	at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.setupDebuggerDarwin(HotSpotAgent.java:660)
	at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:342)
	at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:305)
	at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:141)
	at jdk.hotspot.agent/sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
	at jdk.hotspot.agent/sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
	at jdk.hotspot.agent/sun.jvm.hotspot.tools.JMap.main(JMap.java:176)
	at jdk.hotspot.agent/sun.jvm.hotspot.SALauncher.runJMAP(SALauncher.java:326)
	at jdk.hotspot.agent/sun.jvm.hotspot.SALauncher.main(SALauncher.java:455)
Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
	at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach0(Native Method)
	at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$1AttachTask.doit(BsdDebuggerLocal.java:274)
	at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$BsdDebuggerLocalWorkerThread.run(BsdDebuggerLocal.java:144)

很鬧心。又折騰了半天,最後又說是許可權問題的,有說是專案使用jdk和jmap執行jdk版本不一致的。我兩個都試了。

許可權問題解決辦法,執行如下命令:

jhsdb jmap --heap --pid 2139

jdk版本問題,我們上面都設定了使用jdk11, idea也是jdk11. 但是沒有編譯,這裡重新編譯一下,再試。

最終執行專案,檢視到結果

Attaching to process ID 2139, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 11.0.2+9

using thread-local object allocation.
Garbage-First (G1) GC with 8 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 4294967296 (4096.0MB)
   NewSize                  = 1363144 (1.2999954223632812MB)
   MaxNewSize               = 2576351232 (2457.0MB)
   OldSize                  = 5452592 (5.1999969482421875MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 1048576 (1.0MB)

Heap Usage:
G1 Heap:
   regions  = 4096
   capacity = 4294967296 (4096.0MB)
   used     = 21654560 (20.651397705078125MB)
   free     = 4273312736 (4075.348602294922MB)
   0.5041845142841339% used
G1 Young Generation:
Eden Space:
   regions  = 15
   capacity = 52428800 (50.0MB)
   used     = 15728640 (15.0MB)
   free     = 36700160 (35.0MB)
   30.0% used
Survivor Space:
   regions  = 5
   capacity = 5242880 (5.0MB)
   used     = 5242880 (5.0MB)
   free     = 0 (0.0MB)
   100.0% used
G1 Old Generation:
   regions  = 1
   capacity = 210763776 (201.0MB)
   used     = 0 (0.0MB)
   free     = 210763776 (201.0MB)
   0.0% used


成功!

相關文章