詳情參考jmap 官方文檔html
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 的依賴)java
訪問 http://localhost:8080/nonheapgit
Exception in thread "main" java.lang.OutOfMemoryError: Metaspace Exception in thread "ContainerBackgroundProcessor[StandardEngine[Tomcat]]" java.lang.OutOfMemoryError: Metaspace
內存溢出自動導出github
java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to ./java_pid660.hprof ...
MAT下載地址:http://www.eclipse.org/mat/spring
找開上述導出的內存溢出文件便可進行分析,以下圖的溢出源頭分析:oracle
詳情參考 jstack 官方文檔app
jstack <pid>eclipse
可查看其中包含java.lang.Thread.State: WAITING (parking),JAVA 線程包含的狀態有:maven
NEW:線程還沒有啓動oop
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.