運用jvm自帶的命令能夠方便的在生產監控和打印堆棧的日誌信息幫忙咱們來定位問題!雖然jvm調優成熟的工具已經有不少:jconsole、大名鼎鼎的VisualVM,IBM的Memory Analyzer等等,可是在生產環境出現問題的時候,一方面工具的使用會有所限制,另外一方面喜歡裝X的咱們,總喜歡在出現問題的時候在終端輸入一些命令來解決。全部的工具幾乎都是依賴於jdk的接口和底層的這些命令,研究這些命令的使用也讓咱們更能瞭解jvm構成和特性。java
jps是jdk提供的一個查看當前java進程的小工具, 能夠看作是JavaVirtual Machine Process Status Tool的縮寫。很是簡單實用。spring
jps [options ] [ hostid ]
複製代碼
options 參數apache
C:\Users\Administrator>jps -l
11204
3876 sun.tools.jps.Jps
6324 org.jetbrains.idea.maven.server.RemoteMavenServer
14680 org.jetbrains.jps.cmdline.Launcher
6488 com.tianshen.mini.apps.stepsns.StepSnsApplication
8988 org.jetbrains.kotlin.daemon.KotlinCompileDaemon
C:\Users\Administrator>jps -q
14240
11204
6324
14680
6488
8988
C:\Users\Administrator>jps -m
11204
6324 RemoteMavenServer
14680 Launcher D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/maven-builder-support-3.3.9.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/netty-transport-4.1.32.Final.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/aether-connector-basic-1.1.0.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/aether-dependency-resolver.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/jps-model.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/maven-artifact-3.3.9.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/netty-common-4.1.32.Final.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/idea_rt.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/annotations.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/plexus-utils-3.0.22.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/maven-model-3.3.9.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/netty-codec-4.1.32.Final.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/aether-transport-http-1.1.0.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/li
6488 StepSnsApplication
15932 Jps -m
8988 KotlinCompileDaemon --daemon-runFilesPath C:\Users\Administrator\AppData\Local\kotlin\daemon --daemon-autoshutdownIdleSeconds=7200 --daemon-compilerClasspath D:\workpath\JetBrains\IntelliJ IDEA 2019.1\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;D:\workpath\java\jdk1.8.0_201\lib\tools.jar
C:\Users\Administrator>jps -v
16304 Jps -Denv.class.path=.;D:\workpath\java\jdk1.8.0_201\lib\dt.jar;D:\workpath\java\jdk1.8.0_201\lib\tools.jar; -Dapplication.home=D:\workpath\java\jdk1.8.0_201 -Xms8m
11204 -Xms128m -Xmx750m -XX:ReservedCodeCacheSize=240m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Dsun.io.useCanonCaches=false -Djava.net.preferIPv4Stack=true -Djdk.http.auth.tunneling.disabledSchemes="" -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -javaagent:D:\workpath\JetBrains\IntelliJ IDEA 2019.1\bin\JetbrainsIdesCrack-4.2.jar -Djb.vmOptionsFile=D:\workpath\JetBrains\IntelliJ IDEA 2019.1\bin\idea64.exe.vmoptions -Didea.jre.check=true -Dide.native.launcher=true -Didea.paths.selector=IntelliJIdea2019.1 -XX:ErrorFile=C:\Users\Administrator\java_error_in_idea_%p.log -XX:HeapDumpPath=C:\Users\Administrator\java_error_in_idea.hprof
6324 RemoteMavenServer -Djava.awt.headless=true -Didea.version==2019.1 -Xmx768m -Didea.maven.embedder.version=3.3.9 -Dfile.encoding=GBK
14680 Launcher -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs="" -Djdt.compiler.useSingleThread=true -Dpreload.project.path=D:/workcode/step-trade -Dpreload.config.path=C:/Users/Administrator/.IntelliJIdea2019.1/config/options -Dexternal.project.config=C:\Users\Administrator\.IntelliJIdea2019.1\system\external_build_system\step-trade.a2d39cdb -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djava.net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=-7273493625106742150 -Dfile.encoding=GBK -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2019.1 -Didea.home.path=D:\workpath\JetBrains\IntelliJ IDEA 2019.1 -Didea.config.path=C:\Users\Administrator\.IntelliJIdea2019.1\config -Didea.plugins.path=C:\Users\Administrator\.IntelliJIdea2019.1\config\plugins -Djps.log.dir=C:/Users/Administrator/.IntelliJIdea2019.1/system/log/build-log -Djps.fallback.jdk.home=D:/workpath/JetBrains/IntelliJ IDEA 2019.1/jre64 -Djps.fallback.jdk.version=1.8.0_202-release -Dio.netty.noUnsafe=true -Djava.io.tmp
6488 StepSnsApplication -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53869,suspend=y,server=n -XX:TieredStopAtLevel=1 -Xverify:none -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:C:\Users\Administrator\.IntelliJIdea2019.1\system\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8
8988 KotlinCompileDaemon -Djava.awt.headless=true -Djava.rmi.server.hostname=127.0.0.1 -Xmx700m -Dkotlin.incremental.compilation=true -Dkotlin.incremental.compilation.js=true
C:\Users\Administrator>jps -l -m
11204
6324 org.jetbrains.idea.maven.server.RemoteMavenServer
14680 org.jetbrains.jps.cmdline.Launcher D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/maven-builder-support-3.3.9.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/netty-transport-4.1.32.Final.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/aether-connector-basic-1.1.0.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/aether-dependency-resolver.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/jps-model.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/maven-artifact-3.3.9.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/netty-common-4.1.32.Final.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/idea_rt.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/annotations.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/plexus-utils-3.0.22.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/maven-model-3.3.9.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/netty-codec-4.1.32.Final.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/lib/aether-transport-http-1.1.0.jar;D:/workpath/JetBrains/IntelliJ IDEA 2019.1/li
6488 com.tianshen.mini.apps.stepsns.StepSnsApplication
3660 sun.tools.jps.Jps -l -m
8988 org.jetbrains.kotlin.daemon.KotlinCompileDaemon --daemon-runFilesPath C:\Users\Administrator\AppData\Local\kotlin\daemon --daemon-autoshutdownIdleSeconds=7200 --daemon-compilerClasspath D:\workpath\JetBrains\IntelliJ IDEA 2019.1\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;D:\workpath\java\jdk1.8.0_201\lib\tools.jar
C:\Users\Administrator>
複製代碼
jstat(JVM statistics Monitoring)是用於監視虛擬機運行時狀態信息的命令,它能夠顯示出虛擬機進程中的類裝載、內存、垃圾收集、JIT編譯等運行數據。bash
jstat [option] LVMID [interval] [count]
複製代碼
option參數的總覽app
option | 示意 |
---|---|
class | class loader的行爲統計 |
compiler | HotSpt JIT編譯器行爲統計 |
gc | 垃圾回收堆的行爲統計 |
gccapacity | 各個垃圾回收代容量(young,old,perm)和他們相應的空間統計 |
gcutil | 垃圾收集統計概述(同-gcutil),附加最近兩次垃圾回收事件的緣由 |
gcnew | 新生代行爲統計 |
gcnewcapacity | 新生代與其相應的內存空間的統計 |
gcold | 年老代和永生代行爲統計 |
gcoldcapacity | 年老代行爲統計 |
gcpermcapacity | 永生代行爲統計 |
printcompilation | HotSpot編譯方法統計 |
-classless
監視類裝載、卸載數量、總空間以及耗費的時間jvm
注意:19492就是上面的6488 的LVMId
C:\Users\Administrator>jstat -class 19492
Loaded Bytes Unloaded Bytes Time
13563 24983.9 36 56.2 10.02
複製代碼
-compilersocket
輸出JIT編譯過的方法數量耗時等maven
C:\Users\Administrator>jstat -compiler 19492
Compiled Failed Invalid Time FailedType FailedMethod
9825 9 0 2.60 1 shaded/org/apache/http/client/utils/URLEncodedUtils parse
複製代碼
-gcide
垃圾回收堆的行爲統計,經常使用命令
C:\Users\Administrator>jstat -gc 19492
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
25600.0 25600.0 672.0 0.0 637440.0 361147.5 131584.0 91293.3 72700.0 69810.0 9472.0 8851.6 18 0.242 3 0.441 0.683
複製代碼
S0C: survivor0區的總容量
S1C:survivor1區的總容量
S0U:survivor0區已使用的容量
S1U:survivor1區已使用的容量
EC: Eden區的總容量
EU:Eden區已使用的容量
OC:Old區的總容量
OU:Old區已使用的容量
MC:
MU:
CCSC:
CCSU :
YGC :新生代垃圾回收次數
YGCT: 新生代垃圾回收時間
FGC:老年代垃圾回收次數
FGCT:老年代垃圾回收時間
GCT:垃圾回收總消耗時間
$ jstat -gc 1262 2000 20
這個命令意思就是每隔2000ms輸出1262的gc狀況,一共輸出20次
複製代碼
-gccapacity
同-gc,不過還會輸出Java堆各區域使用到的最大、最小空間
C:\Users\Administrator>jstat -gccapacity 19492
NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
43520.0 688640.0 688640.0 25600.0 25600.0 637440.0 87552.0 1377792.0 131584.0 131584.0 0.0 1112064.0 72700.0 0.0 1048576.0 9472.0 18 3
複製代碼
C即Capacity 總容量,U即Used 已使用的容量
-gcutil
同-gc,不過輸出的是已使用空間佔總空間的百分比
C:\Users\Administrator>jstat -gcutil 19492
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
2.62 0.00 59.88 69.38 96.02 93.45 18 0.242 3 0.441 0.683
複製代碼
-gccause
垃圾收集統計概述(同-gcutil),附加最近兩次垃圾回收事件的緣由
C:\Users\Administrator>jstat -gccause 19492
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT LGCC GCC
2.62 0.00 60.44 69.38 96.02 93.45 18 0.242 3 0.441 0.683 Allocation Failure No GC
複製代碼
-gcnew
統計新生代的行爲
C:\Users\Administrator>jstat -gcnew 19492
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
25600.0 25600.0 672.0 0.0 3 15 25600.0 637440.0 387013.9 18 0.242
複製代碼
-gcnewcapacity
新生代與其相應的內存空間的統計
C:\Users\Administrator>jstat -gcnewcapacity 19492
NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC
43520.0 688640.0 640000.0 229376.0 1024.0 229376.0 25088.0 687616.0 580608.0 20 3
複製代碼
-gcold
統計舊生代的行爲
-gcoldcapacity
統計舊生代的大小和空間
-gcpermcapacity
永生代行爲統計