JVM-10虚拟机性能监控与故障处理工具之【JDK的命令行】
文章目錄
- 思維導(dǎo)圖
- 概述
- 命令行工具
- jps 虛擬機(jī)進(jìn)程狀況工具
- 概述
- 語法及使用
- jstat 虛擬機(jī)統(tǒng)計(jì)信息監(jiān)視工具
- 概述
- 語法及使用
- jinfo Java配置信息工具
- 概述
- 語法及使用
- jmap Java 內(nèi)存映射工具
- 概述
- 語法及使用
- jhat 虛擬機(jī)堆轉(zhuǎn)儲(chǔ)快照分析工具
- 概述
- 語法及使用
- jstack Java堆棧跟蹤工具
- 概述
- 語法及使用
- hsdis:jit生成代碼反匯編
思維導(dǎo)圖
概述
前面介紹了虛擬機(jī)的內(nèi)存分配和回收,大致有了一些理論基礎(chǔ),接下來我們從實(shí)踐的角度去了解虛擬機(jī)內(nèi)存管理。定位問題的時(shí)候,知識(shí)、經(jīng)驗(yàn)是關(guān)鍵基礎(chǔ),數(shù)據(jù)是依據(jù),工具是運(yùn)用知識(shí)處理數(shù)據(jù)的手段。
這里我們說的數(shù)據(jù)主要包括
-
運(yùn)行日志
-
異常堆棧
-
GC日志
-
線程快照(thread dump / javacore文件)
-
堆轉(zhuǎn)儲(chǔ)快照(heapdump/hprof文件)等
命令行工具
Java安裝目錄 bin目錄下有很多官方提供的命令行程序,今天我們倆了解下虛擬機(jī)性能監(jiān)控與故障處理工具
| jps | JVM Process Status Tool | 顯示指定系統(tǒng)內(nèi)所有的HotSPot虛擬機(jī)進(jìn)程 |
| jstat | JVM Statistics Monitoring Tool | 用于手機(jī)HotSpot虛擬機(jī)個(gè)方面的運(yùn)行數(shù)據(jù) |
| jinfo | Configuration Info for Java | 顯示虛擬機(jī)配置信息 |
| jmap | Memory Map for Java | 生成虛擬機(jī)的內(nèi)存轉(zhuǎn)儲(chǔ)快照(headump文件) |
| jhat | Java Heap Analysis Tool | 虛擬機(jī)堆轉(zhuǎn)儲(chǔ)快照分析工具 |
| jstack | Stack Trace For Java | java堆棧跟蹤工具,,顯示線程快照 |
jps 虛擬機(jī)進(jìn)程狀況工具
概述
Java Virtual Machine Process Status Tool
不僅名字像unix主機(jī)的ps命令,功能也相似,可以列舉出正在運(yùn)行的虛擬機(jī)進(jìn)程,并顯示虛擬機(jī)執(zhí)行主類(Main Class,main函數(shù)所在的類)名稱以及這些進(jìn)程的本地虛擬機(jī)唯一ID(Local Virtual Machine Identifier ,LVMID)。.
使用頻率最高的JDK命令行工具,以為內(nèi)其他的JDK工具大多需要輸入它查詢到LVMID來確定要監(jiān)控的是哪一個(gè)虛擬機(jī)進(jìn)程。 對(duì)于本地虛擬機(jī)進(jìn)程來講,LVMID和操作系統(tǒng)的進(jìn)程ID(Process Identifer ,PID) 一致。
語法及使用
usage: jps [-help]jps [-q] [-mlvV] [<hostid>]Definitions:<hostid>: <hostname>[:<port>]jps可以通過RMI協(xié)議查看開啟了RMI服務(wù)的遠(yuǎn)程虛擬機(jī)進(jìn)程狀態(tài)。,hostid為RMI注冊(cè)表中注冊(cè)的主機(jī)名。
jps的其他常用選項(xiàng)
| -q | 只輸出LVMID,省略主類的名稱 |
| -m | 輸出虛擬機(jī)進(jìn)程啟動(dòng)時(shí)傳遞給主類main函數(shù)的參數(shù) |
| -l | 輸出主類的全名,如果執(zhí)行的是jar包,輸出jar路徑 |
| -v | 輸出虛擬機(jī)進(jìn)程啟動(dòng)時(shí)jvm參數(shù) |
jstat 虛擬機(jī)統(tǒng)計(jì)信息監(jiān)視工具
概述
jstat (JVM Statistics Monitoring Tool)用于監(jiān)視虛擬機(jī)各種運(yùn)行狀態(tài)信息的命令行工具。 它可以顯示本地或者遠(yuǎn)程虛擬機(jī)進(jìn)程中的類裝載、內(nèi)存、垃圾會(huì)搜、JIT編譯等運(yùn)行數(shù)據(jù)。
在沒有圖形化界面的服務(wù)器上,它將是運(yùn)行期定位虛擬機(jī)性能問題的首選工具
語法及使用
Usage: jstat -help|-optionsjstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]Definitions:<option> An option reported by the -options option<vmid> Virtual Machine Identifier. A vmid takes the following form:<lvmid>[@<hostname>[:<port>]]Where <lvmid> is the local vm identifier for the targetJava virtual machine, typically a process id; <hostname> isthe name of the host running the target Java virtual machine;and <port> is the port number for the rmiregistry on thetarget host. See the jvmstat documentation for a more completedescription of the Virtual Machine Identifier.<lines> Number of samples between header lines.<interval> Sampling interval. The following forms are allowed:<n>["ms"|"s"]Where <n> is an integer and the suffix specifies the units asmilliseconds("ms") or seconds("s"). The default units are "ms".<count> Number of samples to take before terminating.-J<flag> Pass <flag> directly to the runtime system.-
對(duì)于命令格式中的VMID和LVMID,如果是本地虛擬機(jī)進(jìn)程,兩者一致。如果是遠(yuǎn)程虛擬機(jī)進(jìn)程,則VMID的格式為
[protocol:][//]lvmind[@hostname[:port]/servername] -
interval : 查詢間隔
-
count:查詢次數(shù) 如果省略interval和count,則只查詢一次
假設(shè)需要250毫秒查詢一次進(jìn)程3245的垃圾收集狀況,一共查詢20次
jstat -gc 3245 250 20- 選項(xiàng)option代表用戶希望查詢的虛擬機(jī)信息,主要分為3類:類裝載、垃圾收集、運(yùn)行期編譯狀況。
jstat的主要選項(xiàng)
| -class | 監(jiān)視類裝載、卸載數(shù)量,總空間以及類裝載鎖耗費(fèi)的時(shí)間 |
| -gc | 監(jiān)視Java堆狀況,包括eden區(qū)、兩個(gè)Survivor區(qū)、老年代、永久代等的容量、已使用空間、GC時(shí)間合計(jì)等信息 |
| -gccapacity | 監(jiān)視內(nèi)容與-gc基本相同,但輸出主要關(guān)注Java堆各個(gè)區(qū)域使用的最大、最小空間 |
| -gcutil | 監(jiān)視內(nèi)容與-gc基本相同,但輸出主要關(guān)注已使用空間占總空間的百分比 |
| -gccause | 與-gcutil功能一樣,但是會(huì)額外輸出導(dǎo)致上一次GC產(chǎn)生的原因 |
| -gcnew | 監(jiān)視新生代GC狀況 |
| -gcnewcapacity | 監(jiān)視內(nèi)容與-gcnew 基本相同,但輸出主要關(guān)注Java堆各個(gè)區(qū)域使用的最大、最小空間 |
| -gcold | 監(jiān)視老年代代GC狀況 |
| -gcoldcapacity | 監(jiān)視內(nèi)容與-gcold 基本相同,但輸出主要關(guān)注Java堆各個(gè)區(qū)域使用的最大、最小空間 |
| -gcpermcapacity | 輸出永久代使用的最大、最小空間 |
| -compiler | 輸出JIT編譯器編譯過的方法、耗時(shí)等信息 |
| -printcompilation | 輸出已經(jīng)被JIT編譯的方法 |
使用舉例
E:\Program Files\Java\jdk1.8.0_161\bin>jstat -gcutil 12888 1000 10 Warning: Unresolved Symbol: sun.gc.metaspace.capacity substituted NaN Warning: Unresolved Symbol: sun.gc.metaspace.used substituted NaN Warning: Unresolved Symbol: sun.gc.metaspace.capacity substituted NaN Warning: Unresolved Symbol: sun.gc.compressedclassspace.capacity substituted NaN Warning: Unresolved Symbol: sun.gc.compressedclassspace.used substituted NaN Warning: Unresolved Symbol: sun.gc.compressedclassspace.capacity substituted NaNS0 S1 E O M CCS YGC YGCT FGC FGCT GCT0.00 91.47 46.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.2150.00 91.47 48.39 9.12 - - 9 0.215 0 0.000 0.215-
S0: Survivor0是空的
-
S1:Survivor1使用了91.47的空間
-
E:Eden 使用了 48.39%的空間
-
O: 老年代使用了 9.12%的空間
-
M: 因?yàn)槲疫@里是使用了JDK8 ,沒有了永久代的概念,這里是元數(shù)據(jù)區(qū)
-
YGC: Minor GC表示程序啟動(dòng)以來,發(fā)生了9次Minor GC
-
YGCT:Young GC Time 耗時(shí)為0.215秒
-
FGC:Full GC 表示程序啟動(dòng)以來,發(fā)生了0次Minor GC
-
FGCT:Full GC Time 耗時(shí)為0秒
-
GCT:表示GC Time ,所有的GC總耗時(shí) 0.215秒
純文本狀態(tài)下監(jiān)視虛擬機(jī)的變化確實(shí)不如后面提到的VisualVM等可視化的建施工局直接用圖形的方式展現(xiàn)直觀,但直接在主機(jī)上使用jstat命令仍然是一種常用的監(jiān)控方式。
jinfo Java配置信息工具
概述
語法及使用
Usage:jinfo [option] <pid>(to connect to running process)jinfo [option] <executable <core>(to connect to a core file)jinfo [option] [server_id@]<remote server IP or hostname>(to connect to remote debug server)where <option> is one of:-flag <name> to print the value of the named VM flag-flag [+|-]<name> to enable or disable the named VM flag-flag <name>=<value> to set the named VM flag to the given value-flags to print VM flags-sysprops to print Java system properties<no option> to print both of the above-h | -help to print this help message使用舉例
E:\Program Files\Java\jdk1.8.0_161\bin>jinfo -flag MaxPermSize 12888 -XX:MaxPermSize=85983232jmap Java 內(nèi)存映射工具
概述
語法及使用
Usage:jmap [option] <pid>(to connect to running process)jmap [option] <executable <core>(to connect to a core file)jmap [option] [server_id@]<remote server IP or hostname>(to connect to remote debug server)where <option> is one of:<none> to print same info as Solaris pmap-heap to print java heap summary-histo[:live] to print histogram of java object heap; if the "live"suboption is specified, only count live objects-clstats to print class loader statistics-finalizerinfo to print information on objects awaiting finalization-dump:<dump-options> to dump java heap in hprof binary formatdump-options:live dump only live objects; if not specified,all objects in the heap are dumped.format=b binary formatfile=<file> dump heap to <file>Example: jmap -dump:live,format=b,file=heap.bin <pid>-F force. Use with -dump:<dump-options> <pid> or -histoto force a heap dump or histogram when <pid> does notrespond. The "live" suboption is not supportedin this mode.-h | -help to print this help message-J<flag> to pass <flag> directly to the runtime systemjmap的主要選項(xiàng)
| -dump | 生成java堆轉(zhuǎn)儲(chǔ)快照。 格式為 -dump:[live,] format=b,file=<filename> 其中l(wèi)ive子參數(shù)說明是否只dump出存活的對(duì)象 |
| -finalizeinfo | 顯示在F-Queue中等待Finalizer線程執(zhí)行finalize方法的對(duì)象。 只在linux/solaris平臺(tái)有效 |
| -heap | 只顯示Java堆詳細(xì)信息。如使用腦脹回收器、參數(shù)配置、分代狀況等。只在linux/solaris平臺(tái)有效 |
| -histo | 顯示堆中對(duì)象統(tǒng)計(jì)信息,包括類、實(shí)例數(shù)量、合計(jì)容量 |
| -permstat | 以ClassLoader為統(tǒng)計(jì)口徑顯示永久代內(nèi)存狀態(tài)。 只在linux/solaris平臺(tái)有效 |
| -F | 當(dāng)虛擬機(jī)進(jìn)程對(duì)-dump選項(xiàng)沒有響應(yīng)時(shí),可以使用這個(gè)選項(xiàng)強(qiáng)制生成dump快照。只在linux/solaris平臺(tái)有效 |
使用舉例, 對(duì)pid=12888的進(jìn)程輸出dump信息
E:\Program Files\Java\jdk1.8.0_161\bin>jmap -dump:format=b,file=test.bin 12888 Dumping heap to E:\Program Files\Java\jdk1.8.0_161\bin\test.bin ... Heap dump file createdjhat 虛擬機(jī)堆轉(zhuǎn)儲(chǔ)快照分析工具
概述
語法及使用
Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>-J<flag> Pass <flag> directly to the runtime system. Forexample, -J-mx512m to use a maximum heap size of 512MB-stack false: Turn off tracking object allocation call stack.-refs false: Turn off tracking of references to objects-port <port>: Set the port for the HTTP server. Defaults to 7000-exclude <file>: Specify a file that lists data members that shouldbe excluded from the reachableFrom query.-baseline <file>: Specify a baseline object dump. Objects inboth heap dumps with the same ID and same class willbe marked as not being "new".-debug <int>: Set debug level.0: No debug output1: Debug hprof file parsing2: Debug hprof file parsing, no server-version Report version number-h|-help Print this help and exit<file> The file to readFor a dump file that contains multiple heap dumps, you may specify which dump in the file by appending "#<number>" to the file name, i.e. "foo.hprof#3".All boolean options default to "true"用法舉例
E:\Program Files\Java\jdk1.8.0_161\bin>jmap -dump:format=b,file=test.bin 12888 Dumping heap to E:\Program Files\Java\jdk1.8.0_161\bin\test.bin ... File existsE:\Program Files\Java\jdk1.8.0_161\bin>jhat test.bin Reading from test.bin... Dump file created Mon Aug 06 20:49:25 CST 2018 Snapshot read, resolving... Resolving 244224 objects... Chasing references, expect 48 dots................................................ Eliminating duplicate references................................................ Snapshot resolved. Started HTTP server on port 7000 Server is ready.瀏覽器輸入 http://localhost:7000/
jstack Java堆棧跟蹤工具
概述
語法及使用
Usage:jstack [-l] <pid>(to connect to running process)jstack -F [-m] [-l] <pid>(to connect to a hung process)jstack [-m] [-l] <executable> <core>(to connect to a core file)jstack [-m] [-l] [server_id@]<remote server IP or hostname>(to connect to a remote debug server)Options:-F to force a thread dump. Use when jstack <pid> does not respond (process is hung)-m to print both java and native frames (mixed mode)-l long listing. Prints additional information about locks-h or -help to print this help messagejstack的主要選項(xiàng)
| -F | 當(dāng)正常輸出的請(qǐng)求不被響應(yīng)時(shí),強(qiáng)制輸出線程堆棧 |
| -m | 如果調(diào)用到本地方法的話,可以顯示C/C++的堆棧 |
| -l | 除了堆棧信息,顯示關(guān)于鎖的附件信息 |
用法舉例
E:\Program Files\Java\jdk1.8.0_161\bin>jstack -l 12888 2018-08-06 21:01:10 Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode):"DestroyJavaVM" prio=6 tid=0x000000000e1ab800 nid=0x2f80 waiting on condition [0x0000000000000000]java.lang.Thread.State: RUNNABLELocked ownable synchronizers:- None"DefaultQuartzScheduler_QuartzSchedulerThread" prio=6 tid=0x000000000e1a9800 nid=0xb5c in Object.wait() [0x000000000f59f000]java.lang.Thread.State: TIMED_WAITING (on object monitor)at java.lang.Object.wait(Native Method)at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:410)- locked <0x0000000762395018> (a java.lang.Object)Locked ownable synchronizers:- None ...... ....... ..........hsdis:jit生成代碼反匯編
Sun官方推薦的HotSpot虛擬機(jī)JIT編譯代碼的反匯編插件。了解即可。
總結(jié)
以上是生活随笔為你收集整理的JVM-10虚拟机性能监控与故障处理工具之【JDK的命令行】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_41【前端展示】
- 下一篇: JVM-11虚拟机性能监控与故障处理工具