Java:故障排查、JVM性能監(jiān)控工具單
常用命令
JDK提供了一系列的用于監(jiān)控、診斷Java進(jìn)程的工具,它們?cè)贘DK安裝目錄``bin``目錄下,我們?cè)撊绾问褂盟サ玫接杏玫男畔⒉⒎治鱿到y(tǒng)問題以及性能瓶頸呢?下面詳細(xì)介紹。1、jps:JVM進(jìn)程狀態(tài)工具
在linux下我們肯定使用過``ps -ef | grep java``命令查看過正在運(yùn)行的Java進(jìn)程,而在java中也提供了類似的命令,此命令可以列出正在運(yùn)行的虛擬機(jī)進(jìn)程,并顯示虛擬機(jī)執(zhí)行主類名稱以及進(jìn)程的唯一ID。jps幫助文檔:usage: jps [-help]jps [-q] [-mlvV] [<hostid>]Definitions:<hostid>: <hostname>[:<port>]
執(zhí)行案例:
jps -l1228 sun.tools.jps.Jps
option參數(shù)說明:
- -q:輸出本地虛擬機(jī)進(jìn)程的ID,省略類名稱。
- -m:輸出啟動(dòng)時(shí)傳遞給主類main()方法的參數(shù)。
- -l:輸出全類名,如果執(zhí)行的是JAR包,會(huì)輸出JAR包路徑。
- -v:輸出進(jìn)程的JVM配置參數(shù)。
2、jstat:JVM統(tǒng)計(jì)信息監(jiān)視工具
此命令可以查看JVM運(yùn)行狀態(tài)??梢燥@示本地或者遠(yuǎn)程JVM進(jìn)程中的類加載、內(nèi)存、垃圾收集、即時(shí)編譯等運(yùn)行時(shí)數(shù)據(jù)。jstat幫助文檔:
invalid argument countUsage: 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.
執(zhí)行案例:
//查看垃圾收集等信息jstat -gc 1441S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT10752.0 0.0 0.0 65536.0 9175.5 175104.0 0.0 4480.0 780.7 384.0 76.6 0 0.000 0 0.000 0.000
option參數(shù)說明:
- -class:監(jiān)視類加載、卸載數(shù)量、總空間以及類裝載的耗時(shí)。
- -compiler:即時(shí)編譯器編譯過的方法、耗時(shí)等信息。
- -gc:監(jiān)視Java堆狀態(tài),包括Eden區(qū)、兩個(gè)Survivor區(qū)、老年代、永久代的容量、已用空間、垃圾收集時(shí)間合計(jì)等信息。
- -gccapacity:內(nèi)容與gc基本類似,但輸出的主要是關(guān)注Java堆各個(gè)區(qū)域使用的最大、最小空間。
- -gccause:與gcutil功能一樣,但是會(huì)額外輸出上一次導(dǎo)致垃圾收集產(chǎn)生的原因。
- -gcmetacapacity:JDK8下元數(shù)據(jù)空間統(tǒng)計(jì)。
- -gcnew:監(jiān)視新生代垃圾收集信息。
- -gcnewcapacity:與gcnew類似,輸出使用到的最大、最小空間。
- -gcold:監(jiān)視老年代垃圾收集信息。
- -gcoldcapacity:與gcold類似,輸出使用到的最大、最小空間。
- -gcutil:監(jiān)視內(nèi)容與gc類似,輸出已使用空間占總空間的百分比。
- -printcompilation:輸出已經(jīng)被即時(shí)編譯的方法。
3、jinfo:Java配置信息工具
可以用來查看正在運(yùn)行的Java程序的擴(kuò)展參數(shù),包括Java system屬性和JVM命令行參數(shù);也可以動(dòng)態(tài)修改正在運(yùn)行的JVM參數(shù)。jinfo幫助文檔:
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
執(zhí)行案例:
jinfo -flags 18378Attaching to process ID 18378, please wait...Debugger attached successfully.Server compiler detected.JVM version is 25.261-b12VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=262144000 -XX:MaxHeapSize=4164943872 -XX:MaxNewSize=1388314624 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=87031808 -XX:OldSize=175112192 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
option參數(shù)說明:
- no option 輸出全部的參數(shù)和系統(tǒng)屬性。
- -flag name 輸出對(duì)應(yīng)名稱的參數(shù)。
- -flag [+|-]name 開啟或者關(guān)閉對(duì)應(yīng)名稱的參數(shù)。
- -flag name=value 設(shè)定對(duì)應(yīng)名稱的參數(shù)。
- -flags 輸出全部的參數(shù)。
- -sysprops 輸出系統(tǒng)屬性。
4、jmap:Java內(nèi)存映像工具
jamp命令用于生成堆轉(zhuǎn)儲(chǔ)快照。還可以查詢finalize執(zhí)行隊(duì)列、Java堆和方法區(qū)的詳細(xì)信息,例如空間使用率、當(dāng)前使用的是哪一種收集器等。jmap幫助文檔:
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 system
執(zhí)行案例:
jmap -dump:format=b,file=test.bin 1713Dumping heap to /Users/handexing/test.bin ...Heap dump file created
option參數(shù)說明:
- -dump:生成Java堆轉(zhuǎn)儲(chǔ)快照。格式為-dump:[live,]format=b,file=<filename>,其中l(wèi)ive參數(shù)說明是否只dump出存活對(duì)象。
- -finalization:顯示在F-queue中等待Finalizer線程執(zhí)行finalize方法的對(duì)象。
- -heap:顯示Java堆詳細(xì)信息。例如哪種回收器、參數(shù)配置、分代情況等。linux平臺(tái)下有效。
- -histo:顯示堆中對(duì)象統(tǒng)計(jì)信息,包括類、實(shí)例數(shù)量等。
- -permstat:以classloader為統(tǒng)計(jì)口徑顯示永久代內(nèi)存狀態(tài)。linux平臺(tái)下有效。
- -F:當(dāng)JVM對(duì)-dump沒有響應(yīng)時(shí),可以使用-F強(qiáng)制生成dump快照,linux平臺(tái)下有效。
5、jhat:JVM堆轉(zhuǎn)儲(chǔ)快照分析工具
與jmap命令搭配使用,用來分析jmap生成的堆轉(zhuǎn)儲(chǔ)快照。jhat內(nèi)置了一個(gè)微型http/web服務(wù)器,生成分析后的結(jié)果,可以在瀏覽器中查看(話說回來,實(shí)際情況中除非是真沒別的工具可用了,否則大多數(shù)人是不會(huì)直接用jhat來分析快照的)。也不建議在服務(wù)器上使用jhat,會(huì)損耗服務(wù)器性能。jhat幫助文檔:
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 fileby appending "#<number>" to the file name, i.e. "foo.hprof#3".All boolean options default to "true"
執(zhí)行案例:
jhat test.binReading from dump.bin...Dump file created Sat Feb 8 21:54:58 CST 2021Snapshot read, resolving...Resolving 271309 objects...Chasing references, expect 54 dots......................................................Eliminating duplicate references......................................................Snapshot resolved.Started HTTP server on port 7000Server is ready.
訪問localhost:7000可以查看分析結(jié)果。
6、jstack:Java堆棧跟蹤工具
用于生成JVM當(dāng)時(shí)的線程快照。線程快照就是當(dāng)前JVM內(nèi)每一條線程正在執(zhí)行的方法堆棧集合。生成快照主要用于定位線程長(zhǎng)時(shí)間停頓的原因,例如死鎖、死循環(huán)、請(qǐng)求資源導(dǎo)致長(zhǎng)時(shí)間掛起等。就可以排查到?jīng)]有相應(yīng)的線程到底在干什么。jstack幫助文檔: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 message
執(zhí)行案例:
jstack -l 171320:16:16Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.261-b12 mixed mode):#53 prio=5 os_prio=31 tid=0x00007fa898808800 nid=0x1103 waiting on condition [0x0000000000000000]: RUNNABLELocked ownable synchronizers:None#51 daemon prio=5 os_prio=31 tid=0x00007fa898a4a000 nid=0x7903 waiting on condition [0x0000700007b0b000]: TIMED_WAITING (sleeping)at java.lang.Thread.sleep(Native Method)at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1133)at java.lang.Thread.run(Thread.java:748)Locked ownable synchronizers:None
option參數(shù)說明:
- -F:正常輸入不被響應(yīng)時(shí),強(qiáng)制輸出堆棧。
- -m:如果調(diào)用本地方法的話,顯示C/C++的堆棧。
- -l:除堆棧外,顯示關(guān)于鎖的附加信息。
Java調(diào)試進(jìn)階工具單
- btrace:https://github.com/btraceio/btrace
- arthas:https://arthas.aliyun.com/doc/
- JProfiler:https://www.ej-technologies.com/products/jprofiler/overview.html
往期歷史 |相關(guān)推薦一文帶你了解RabbitMQ到底是個(gè)什么鬼!
太好了!總算有人把靜態(tài)代理、CGlib動(dòng)態(tài)代理、JDK動(dòng)態(tài)代理都說清楚了!
Java并發(fā)編程之線程池實(shí)踐及實(shí)現(xiàn)原理
評(píng)論
圖片
表情
