常用jvm命令

張濤5250發表於2020-11-02

常用jvm命令

jstat

檢視java程式的gc狀況

/ # jstat -gc 11
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
26112.0 26112.0  0.0   23597.8 209920.0 10723.2   524288.0   160828.1  94592.0 90638.6 10880.0 10134.0  14083  364.899  585   161.727  526.626
/ # jstat -gcutil 11
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
 83.26   0.00  20.78  33.24  95.82  93.14  14084  364.926   585  161.727  526.652

注:結果中涉及到jvm堆記憶體的相關知識,這裡不做詳細說明,感興趣的可以搜尋jvm堆記憶體相關的文章學習研究一下

jstack

檢視jvm中棧的資訊(執行緒)

直接檢視前50行棧的資訊

/ # jstack 11 | head -50
2019-09-09 06:50:10
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode):

"ActiveMQ InactivityMonitor Worker" #11371 daemon prio=5 os_prio=0 tid=0x00007ff4d0006800 nid=0x2d88 waiting on condition [0x00007ff4b4fd0000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000d338aec0> (a java.util.concurrent.SynchronousQueue$TransferStack)
        at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
        at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
        at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362)
        at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:941)
        at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1073)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

"Attach Listener" #11349 daemon prio=9 os_prio=0 tid=0x00007ff51800d000 nid=0x2cb0 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"http-nio-8402-exec-12" #3103 daemon prio=5 os_prio=0 tid=0x00007ff5147d7000 nid=0xc29 waiting on condition [0x00007ff4b50d1000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000d31e44f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
        at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
        at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
        at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
        at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

"http-nio-8402-exec-11" #3102 daemon prio=5 os_prio=0 tid=0x00007ff5143a4800 nid=0xc28 waiting on condition [0x00007ff4b51d2000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000d31e44f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
        at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
        at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
        at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
        at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

將所有的棧資訊備份到指定檔案中

/ # jstack 11 >> jstack20190909

按照狀態彙總並展示堆疊資訊

/ # jstack 11 | grep "java.lang.Thread.State" | sort -nr | uniq -c
     11    java.lang.Thread.State: WAITING (parking)
      2    java.lang.Thread.State: WAITING (on object monitor)
      3    java.lang.Thread.State: TIMED_WAITING (sleeping)
      4    java.lang.Thread.State: TIMED_WAITING (parking)
      3    java.lang.Thread.State: TIMED_WAITING (on object monitor)
     11    java.lang.Thread.State: RUNNABLE
/ #

jmap

檢視堆記憶體中存活的物件例項資訊

/ # jmap -histo:live 11 | head -9

 num     #instances         #bytes  class name
----------------------------------------------
   1:        901174       47357792  [C
   2:        898439       21562536  java.lang.String
   3:         58585        5155480  java.lang.reflect.Method
   4:          7803        4511848  [B
   5:        117765        3768480  java.util.concurrent.ConcurrentHashMap$Node
   6:        136149        3267576  java.util.Date

-histo:live只顯示存活的物件, 其中11表示程式id, head -9 顯示前9個

使用jmap備份堆記憶體資訊

/ # jmap -dump:live,format=b,file=jmap20190909 11
Dumping heap to /jmap20190909 ...
Heap dump file created

-dump 選項可以將當前的heap資訊備份下來,live 僅備份存活的物件, format=b 備份成二進位制檔案, file=[filedir] 備份的檔名稱, 11 java程式id

使用jhat分析jmap備份的記憶體資料,並啟動一個web服務進行檢視

/ # jhat -port 8899 jmap20190909
Reading from jmap20190909...
Dump file created Mon Sep 09 14:31:18 CST 2019
Snapshot read, resolving...
Resolving 3111305 objects...
Chasing references, expect 622 dots..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 8899
Server is ready.

jmap備份後的資訊,可以使用jhat進行分析,並啟動一個web伺服器,方便檢視詳細的heap資訊
-port 指定啟動web服務的埠,預設埠號7000, jmap20190909 為上一步jmap備份的檔名

常用的jvm啟動引數

java  -jar -Xms256m -Xmx512m -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=2 -XX:SurvivorRatio=8 -Xloggc:/app-gc.log app.jar

-Xms256m 指定最小的堆記憶體

-Xmx512m 指定最大的堆記憶體: 動態擴充套件最大到512m

-XX:+PrintGCDateStamps 列印gc日誌,並輸出時間戳

-XX:NewRatio=2 指定老年代與年輕代的記憶體分配比例為 2:1, 即老年代佔2/3, 年輕代佔1/3

-XX:SurvivorRatio=8 指定年輕代中eden區記憶體與survivor區記憶體的比例為 8:1:1

-Xloggc:/app-gc.log 指定gc日誌輸出的檔案

gc日誌內容

Java HotSpot(TM) 64-Bit Server VM (25.171-b11) for linux-amd64 JRE (1.8.0_171-b11), built on Mar 28 2018 17:07:08 by "java_re" with gcc 4.3.0 20080428 (Red Hat 4.3.0-8)
Memory: 4k page, physical 8008884k(1129856k free), swap 0k(0k free)
CommandLine flags: -XX:InitialHeapSize=536870912 -XX:MaxHeapSize=805306368 -XX:NewRatio=2 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=8 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
2019-08-30T04:07:01.186+0000: 0.709: [GC (Allocation Failure) [PSYoungGen: 139776K->3815K(157184K)] 139776K->3823K(506880K), 0.0158590 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2019-08-30T04:07:01.716+0000: 1.239: [GC (Allocation Failure) [PSYoungGen: 143591K->4274K(244736K)] 143599K->4354K(594432K), 0.0062708 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2019-08-30T04:07:02.356+0000: 1.880: [GC (Allocation Failure) [PSYoungGen: 231602K->5464K(244736K)] 231682K->5552K(594432K), 0.0057272 secs] [Times: user=0.01 sys=0.01, real=0.01 secs]
2019-08-30T04:07:02.704+0000: 2.228: [GC (Allocation Failure) [PSYoungGen: 232792K->6139K(244736K)] 232880K->6235K(594432K), 0.0057959 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
2019-08-30T04:07:03.009+0000: 2.533: [GC (Allocation Failure) [PSYoungGen: 233467K->8151K(235520K)] 233563K->8255K(585216K), 0.0096851 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
2019-08-30T04:07:03.187+0000: 2.711: [GC (Metadata GC Threshold) [PSYoungGen: 103694K->6879K(236032K)] 103798K->6991K(585728K), 0.0078126 secs] [Times: user=0.02 sys=0.01, real=0.01 secs]
2019-08-30T04:07:03.195+0000: 2.719: [Full GC (Metadata GC Threshold) [PSYoungGen: 6879K->0K(236032K)] [ParOldGen: 112K->6743K(349696K)] 6991K->6743K(585728K), [Metaspace: 20923K->20923K(1069056K)], 0.0290297 secs] [Times: user=0.06 sys=0.01, real=0.03 secs]
2019-08-30T04:07:03.557+0000: 3.081: [GC (Allocation Failure) [PSYoungGen: 227328K->2371K(236544K)] 234071K->9122K(586240K), 0.0031362 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2019-08-30T04:07:03.859+0000: 3.383: [GC (Allocation Failure) [PSYoungGen: 229699K->2796K(252928K)] 236450K->9547K(602624K), 0.0091977 secs] [Times: user=0.03 sys=0.01, real=0.01 secs]
2019-08-30T04:07:04.485+0000: 4.008: [GC (Allocation Failure) [PSYoungGen: 246508K->6477K(250368K)] 253259K->13236K(600064K), 0.0081053 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2019-08-30T04:07:04.882+0000: 4.406: [GC (Allocation Failure) [PSYoungGen: 250189K->5376K(252928K)] 256948K->14055K(602624K), 0.0108411 secs] [Times: user=0.04 sys=0.01, real=0.01 secs]
2019-08-30T04:07:05.401+0000: 4.925: [GC (Allocation Failure) [PSYoungGen: 248576K->2195K(252416K)] 257255K->14982K(602112K), 0.0113168 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2019-08-30T04:07:06.627+0000: 6.151: [GC (Allocation Failure) [PSYoungGen: 245395K->6632K(251904K)] 258182K->20196K(601600K), 0.0089046 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
2019-08-30T04:07:06.955+0000: 6.478: [GC (Allocation Failure) [PSYoungGen: 249320K->3013K(252416K)] 262884K->19340K(602112K), 0.0156316 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
2019-08-30T04:07:07.220+0000: 6.743: [GC (Allocation Failure) [PSYoungGen: 245701K->1897K(252416K)] 262028K->19481K(602112K), 0.0045420 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2019-08-30T04:07:07.346+0000: 6.870: [GC (Metadata GC Threshold) [PSYoungGen: 105178K->1275K(252416K)] 122761K->19666K(602112K), 0.0042970 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]

相關文章