JVM系列學習(一):基於JDK命令行工具的監控

基於JDK命令行工具的監控

JVM的參數類型

  1. 標準參數

    • -help
    • -server -client
    • -version -showversion
    • -cp -classpath
  2. X參數:非標準化參數

    • -Xint: 完全解釋執行,不生成JIT本地代碼
    • -Xcomp:第一次使用就編譯成 本地代碼
    • -Xmixed:混合模式,JVM自己來決定是否編譯成 本地代碼
  3. 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

  1. -XX:+PrintFlagsInitial:java -XX:+PrintFlagsInitial -version

    • =表示默認值

    • :=被用戶或者JVM修改後的值

    • jinfo舉例:
      查看最大內存:jinfo -flag MaxHeapSize pid
      查看垃圾回收器: jinfo -flag UseConcMarkSweepGC pid
                    jinfo -flag UseG1GC pid
                    jinfo -flag UseParallelGC pid
      
  2. -XX:+PrintFlagsFinal

  3. -XX:UnlockExperimentalVMOptions 解鎖實驗參數

  4. -XX:UnlockDiagnosticVMOptions 解鎖診斷參數

  5. -XX:+PrintCommandLineFlags 打印命令行參數

jstat查看虛擬機統計信息

文檔查看

  1. 類加載

    • 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.
      
  2. 垃圾收集:-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時間
      

JVM內存結構
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實戰內存溢出

  1. 堆區溢出

    /**
     * -Xmx32M -Xms32M
     **/
    while(true){
        list.add();
    }
    
  2. 非堆區溢出

    /**asm自動創建class類
     * -XX:MetaspaceSize=32M  -XX:MaxMetaspaceSize=32M
     **/
    while(true){
        list.addAll(createClass())
    }
    
  3. 導出內存映像文件分析內存溢出原因:hprof文件

    1. 內存溢出自動導出:-XX:+HeapDumpOnOutOfMemoryError
      ​ -XX:HeapDumpPath=./

    2. 使用jmap命令手動導出

      jmap -heap pid:可以查看各個區的詳細情況
      jmap -dump:format=b,file=heap.hprof pid
      
  4. MAT分析內存溢出 下載MAT

    下載之後導入hprof文件,使用MAT文件分析

jstack查看死循環與死鎖

jstack簡介

  1. 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.

    線程狀態

  2. 導出文件

    jstack pid >pid.txt 結果導出到pid.txt文件
    sz pid.txt 下載txt文件然後查看
    print "%x" pid 轉化爲16進制,在pid.txt文件中具體查看
    tail -f nohup.out  啓動jar日誌查看
    

死循環導致CPU飆高

  1. 死循環導致CPU飆高無法訪問:轉換16進制查看線程狀態:RUNNABLE狀態
  2. 死鎖的情況:線程調用1之後等待1秒調用2,線程調用2之後等待1秒調用1,造成死鎖
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章