jmap+MAT
詳情參考jmap 官方文檔
Exception in thread "http-nio-8080-exec-2" Exception in thread "http-nio-8080-exec-1" java.lang.OutOfMemoryError: Java heap space java.lang.OutOfMemoryError: Java heap space
–XX:MetaspaceSize=32M –XX:MaxMetaspaceSize=32M(同時在 pom.xml 中加入 asm 的依賴)
訪問 http://localhost:8080/nonheap
Exception in thread "main" java.lang.OutOfMemoryError: Metaspace Exception in thread "ContainerBackgroundProcessor[StandardEngine[Tomcat]]" java.lang.OutOfMemoryError: Metaspace
內存溢出自動導出
java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to ./java_pid660.hprof ...

MAT下載地址:http://www.eclipse.org/mat/
找開上述導出的內存溢出文件即可進行分析,如下圖的溢出源頭分析:
jstack
詳情參考 jstack 官方文檔
jstack <pid>
可查看其中包含java.lang.Thread.State: WAITING (parking),JAVA 線程包含的狀態有:
NEW:線程尚未啟動
RUNNABLE:線程正在 JVM 中執行
BLOCKED:線程在等待監控鎖(monitor lock)
WAITING:線程在等待另一個線程進行特定操作(時間不確定)
TIMED_WAITING:線程等待另一個線程進行限時操作
TERMINATED:線程已退出
monitor_tuning中新增CpuController.java
mvn clean package -Dmaven.test.skip
mvn 打包提速參考 CSDN
此時會生成一個monitor_tuning-0.0.1-SNAPSHOT.jar的 jar包,為避免本地的 CPU 消耗過多導致死機,建議上傳上傳到虛擬機進行測試
nohup java -jar monitor_tuning-0.0.1-SNAPSHOT.jar &
訪問 http://xx.xx.xx.xx:12345/loop(端口12345在application.properties文件中定義)
top -p <pid> -H可以查看線程及 CPU 消耗情況
使用 jstack <pid>可以導出追蹤文件,文件中 PID 在 jstack 中顯示的對應 nid 為十六進制(命令行可執行 printf ‘%x’ <pid>可以進行轉化,如1640對應的十六進制為668)
"http-nio-12345-exec-3" #18 daemon prio=5 os_prio=0 tid=0x00007f10003fb000 nid=0x668 runnable [0x00007f0fcf8f9000] java.lang.Thread.State: RUNNABLE at org.alanhou.monitor_tuning.chapter2.CpuController.getPartneridsFromJson(CpuController.java:77)
訪問http://xx.xx.xx.xx:12345/deadlock(如上jstack <pid>導出追蹤記錄會發現如下這樣的記錄)
Java stack information for the threads listed above: =================================================== "Thread-5": at org.alanhou.monitor_tuning.chapter2.CpuController.lambda$deadlock$1(CpuController.java:41) - waiting to lock <0x00000000edcf3470> (a java.lang.Object) - locked <0x00000000edcf3480> (a java.lang.Object) at org.alanhou.monitor_tuning.chapter2.CpuController$$Lambda$337/547045985.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) "Thread-4": at org.alanhou.monitor_tuning.chapter2.CpuController.lambda$deadlock$0(CpuController.java:33) - waiting to lock <0x00000000edcf3480> (a java.lang.Object) - locked <0x00000000edcf3470> (a java.lang.Object) at org.alanhou.monitor_tuning.chapter2.CpuController$$Lambda$336/1704575158.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) Found 1 deadlock.