文章目錄
基於JDK命令行工具的監控
JVM的參數類型
-
標準參數
- -help
- -server -client
- -version -showversion
- -cp -classpath
-
X參數:非標準化參數
- -Xint: 完全解釋執行,不生成JIT本地代碼
- -Xcomp:第一次使用就編譯成 本地代碼
- -Xmixed:混合模式,JVM自己來決定是否編譯成 本地代碼
-
XX參數:非標準化參數,相對不穩定,主要用於JVM調優和Debug
-
boolean類型
-
格式:-XX:[+-]<name>表示啓動或者禁用name屬性 比如:-XX:+UseConcMarkSweepGC -XX:+UseG1GC
-
非Boolean類型
-
格式:-XX:<name>=<value>表示name屬性的值是value 比如:-XX:MaxGCPauseMillis=500 -XX:GCTimeRatio=19
-
-Xms等價於-XX:InitialHeapSize 初始化堆大小
-
-Xmx等價於-XX:MaxHeapSize 最大堆大小
-
運行時JVM參數查看:jinfo -flags pid
-
-XX:+PrintFlagsInitial:java -XX:+PrintFlagsInitial -version
-
=表示默認值
-
:=被用戶或者JVM修改後的值
-
jinfo舉例: 查看最大內存:jinfo -flag MaxHeapSize pid 查看垃圾回收器: jinfo -flag UseConcMarkSweepGC pid jinfo -flag UseG1GC pid jinfo -flag UseParallelGC pid
-
-
-XX:+PrintFlagsFinal
-
-XX:UnlockExperimentalVMOptions 解鎖實驗參數
-
-XX:UnlockDiagnosticVMOptions 解鎖診斷參數
-
-XX:+PrintCommandLineFlags 打印命令行參數
jstat查看虛擬機統計信息
-
類加載
-
jstat -class pid 1000(毫秒) 10(輸出10行)
-
-class option Class loader statistics. Loaded: Number of classes loaded. Bytes: Number of kBs loaded. Unloaded: Number of classes unloaded. Bytes: Number of Kbytes unloaded. Time: Time spent performing class loading and unloading operations.
-
-
垃圾收集:-gc,-gcutil,-gccause,-gcnew,-gcold
-
jstat -gc pid
-
-gc option Garbage-collected heap statistics. S0C: Current survivor space 0 capacity (kB). S0,S1的總量 S1C: Current survivor space 1 capacity (kB). S0U: Survivor space 0 utilization (kB).S0,S1的使用量 S1U: Survivor space 1 utilization (kB). EC: Current eden space capacity (kB).Eden區的總量 EU: Eden space utilization (kB).Eden區的使用量 OC: Current old space capacity (kB). Old區總量 OU: Old space utilization (kB).Old區使用量 MC: Metaspace capacity (kB).Metaspace區總量 MU: Metacspace utilization (kB).Metaspace區使用量 CCSC: Compressed class space capacity (kB).壓縮類空間總量 CCSU: Compressed class space used (kB).壓縮類空間使用量 YGC: Number of young generation garbage collection events.YoungGC的次數 YGCT: Young generation garbage collection time.YoungGC的時間 FGC: Number of full GC events.FullGC的次數 FGCT: Full garbage collection time.FullGC的時間 GCT: Total garbage collection time.總的GC時間
-
3. JIT編譯:-compiler,-printcompilation
-
jstat -compiler pid
-
-compiler option Java HotSpot VM Just-in-Time compiler statistics. Compiled: Number of compilation tasks performed. Failed: Number of compilations tasks failed. Invalid: Number of compilation tasks that were invalidated. Time: Time spent performing compilation tasks. FailedType: Compile type of the last failed compilation. FailedMethod: Class name and method of the last failed compilation.
jmap+MAT實戰內存溢出
-
堆區溢出
/** * -Xmx32M -Xms32M **/ while(true){ list.add(); }
-
非堆區溢出
/**asm自動創建class類 * -XX:MetaspaceSize=32M -XX:MaxMetaspaceSize=32M **/ while(true){ list.addAll(createClass()) }
-
導出內存映像文件分析內存溢出原因:hprof文件
-
內存溢出自動導出:-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./ -
使用jmap命令手動導出
jmap -heap pid:可以查看各個區的詳細情況 jmap -dump:format=b,file=heap.hprof pid
-
-
MAT分析內存溢出 下載MAT
下載之後導入hprof文件,使用MAT文件分析
jstack查看死循環與死鎖
jstack簡介
-
JAVA線程狀態
NEW The thread has not yet started. RUNNABLE The thread is executing in the JVM. BLOCKED The thread is blocked waiting for a monitor lock. WAITING The thread is waiting indefinitely for another thread to perform a particular action. TIMED_WAITING The thread is waiting for another thread to perform an action for up to a specified waiting time. TERMINATED The thread has exited. -
導出文件
jstack pid >pid.txt 結果導出到pid.txt文件 sz pid.txt 下載txt文件然後查看 print "%x" pid 轉化爲16進制,在pid.txt文件中具體查看 tail -f nohup.out 啓動jar日誌查看
死循環導致CPU飆高
- 死循環導致CPU飆高無法訪問:轉換16進制查看線程狀態:RUNNABLE狀態
- 死鎖的情況:線程調用1之後等待1秒調用2,線程調用2之後等待1秒調用1,造成死鎖