常用命令
JDK提供了一系列的用於監控、診斷Java進程的工具,它們在JDK安裝目錄``bin``目錄下,我們該如何使用它去得到有用的信息並分析系統問題以及性能瓶頸呢?下面詳細介紹。1、jps:JVM進程狀態工具
在linux下我們肯定使用過``ps -ef | grep java``命令查看過正在運行的Java進程,而在java中也提供了類似的命令,此命令可以列出正在運行的虛擬機進程,並顯示虛擬機執行主類名稱以及進程的唯一ID。usage: jps [-help] jps [-q] [-mlvV] [<hostid>]Definitions: <hostid>: <hostname>[:<port>]執行案例:
jps -l1228 sun.tools.jps.Jpsoption參數說明:
-m:輸出啟動時傳遞給主類main()方法的參數。-l:輸出全類名,如果執行的是JAR包,會輸出JAR包路徑。
2、jstat:JVM統計信息監視工具
此命令可以查看JVM運行狀態。可以顯示本地或者遠程JVM進程中的類加載、內存、垃圾收集、即時編譯等運行時數據。jstat幫助文檔:
invalid argument countUsage: jstat -help|-options jstat -<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 target Java virtual machine, typically a process id; <hostname> is the name of the host running the target Java virtual machine; and <port> is the port number for the rmiregistry on the target host. See the jvmstat documentation for a more complete description 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 as milliseconds("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.執行案例:
//查看垃圾收集等信息jstat -gc 1441 S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 10752.0 10752.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.000option參數說明:
-class:監視類加載、卸載數量、總空間以及類裝載的耗時。-compiler:即時編譯器編譯過的方法、耗時等信息。-gc:監視Java堆狀態,包括Eden區、兩個Survivor區、老年代、永久代的容量、已用空間、垃圾收集時間合計等信息。-gccapacity:內容與gc基本類似,但輸出的主要是關注Java堆各個區域使用的最大、最小空間。-gccause:與gcutil功能一樣,但是會額外輸出上一次導致垃圾收集產生的原因。-gcmetacapacity:JDK8下元數據空間統計。-gcnewcapacity:與gcnew類似,輸出使用到的最大、最小空間。-gcoldcapacity:與gcold類似,輸出使用到的最大、最小空間。-gcutil:監視內容與gc類似,輸出已使用空間佔總空間的百分比。-printcompilation:輸出已經被即時編譯的方法。3、jinfo:Java配置信息工具
可以用來查看正在運行的Java程序的擴展參數,包括Java system屬性和JVM命令行參數;也可以動態修改正在運行的JVM參數。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執行案例:
jinfo -flags 18378Attaching to process ID 18378, please wait...Debugger attached successfully.Server compiler detected.JVM version is 25.261-b12Non-default VM 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:+UseParallelGCoption參數說明:
-flag [+|-]name 開啟或者關閉對應名稱的參數。-flag name=value 設定對應名稱的參數。4、jmap:Java內存映像工具
jamp命令用於生成堆轉儲快照。還可以查詢finalize執行隊列、Java堆和方法區的詳細信息,例如空間使用率、當前使用的是哪一種收集器等。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 format dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> -F force. Use with -dump:<dump-options> <pid> or -histo to force a heap dump or histogram when <pid> does not respond. The "live" suboption is not supported in this mode. -h | -help to print this help message -J<flag> to pass <flag> directly to the runtime system執行案例:
jmap -dump:format=b,file=test.bin 1713Dumping heap to /Users/handexing/test.bin ...Heap dump file createdoption參數說明:
-dump:生成Java堆轉儲快照。格式為-dump:[live,]format=b,file=<filename>,其中live參數說明是否只dump出存活對象。-finalization:顯示在F-queue中等待Finalizer線程執行finalize方法的對象。-heap:顯示Java堆詳細信息。例如哪種回收器、參數配置、分代情況等。linux平臺下有效。-histo:顯示堆中對象統計信息,包括類、實例數量等。-permstat:以classloader為統計口徑顯示永久代內存狀態。linux平臺下有效。-F:當JVM對-dump沒有響應時,可以使用-F強制生成dump快照,linux平臺下有效。5、jhat:JVM堆轉儲快照分析工具
與jmap命令搭配使用,用來分析jmap生成的堆轉儲快照。jhat內置了一個微型http/web伺服器,生成分析後的結果,可以在瀏覽器中查看(話說回來,實際情況中除非是真沒別的工具可用了,否則大多數人是不會直接用jhat來分析快照的)。也不建議在伺服器上使用jhat,會損耗伺服器性能。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. For example, -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 should be excluded from the reachableFrom query.-baseline <file>: Specify a baseline object dump. Objects in both heap dumps with the same ID and same class will be marked as not being "new".-debug <int>: Set debug level. 0: No debug output 1: Debug hprof file parsing 2: 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"執行案例:
jhat test.bin Reading 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可以查看分析結果。
6、jstack:Java堆棧跟蹤工具
用於生成JVM當時的線程快照。線程快照就是當前JVM內每一條線程正在執行的方法堆棧集合。生成快照主要用於定位線程長時間停頓的原因,例如死鎖、死循環、請求資源導致長時間掛起等。就可以排查到沒有相應的線程到底在幹什麼。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執行案例:
jstack -l 17132021-02-08 20:16:16Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.261-b12 mixed mode):"DestroyJavaVM" #53 prio=5 os_prio=31 tid=0x00007fa898808800 nid=0x1103 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:- None"http-nio-8080-AsyncTimeout" #51 daemon prio=5 os_prio=31 tid=0x00007fa898a4a000 nid=0x7903 waiting on condition [0x0000700007b0b000] java.lang.Thread.State: 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:- Noneoption參數說明:
-m:如果調用本地方法的話,顯示C/C++的堆棧。Java調試進階工具單
btrace:https://github.com/btraceio/btracearthas:https://arthas.aliyun.com/doc/JProfiler:https://www.ej-technologies.com/products/jprofiler/overview.html簡單介紹了JVM中常用的命令,想要熟練使用還要在日常實際開發調試中經常使用,熟能生巧。希望這篇文章能給大家帶來幫助。喜歡加關注,後續持續更新!!!