1. jps

作用:查询 java 进程

jps [-q] [-mlvV] [<hostid>]

15653 Launcher
15574
15654 Application
536
15805 Jps
  • q: 只输出进程号
  • m: 输出main method的参数
  • l: 输出完全的包名,应用主类名,jar的完全路径名
  • v: jvm 参数
  • V: 输出通过 flag 文件传递到JVM中的参数(.hotspotrc文件或-XX:Flags=所指定的文件)
  • jps <hostname>[:<port>]:查看别的机器

2. jstat

2.1 简介

作用:用于监控 jvm 内存及 gc 活动的工具

jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
  • t: 在第一列展示一个程序启动以来时间戳,单位 s。
  • -h<lines>:每 lines 行展示一下标题头,没啥用。
  • vmid: 通过 jps 查询出来的 java 进程号。远程机器 [protocol:][//]lvmid[@hostname[:port][/servername]
  • interval:每次输出的间隔时间,单位默认毫秒。
  • count:要采集的样本数量,默认无穷大。

2.2 option

2.2.1 class

作用:显示加载class的数量,及所占空间等信息。(Class loader statistics.)

$ jstat -class 15654

Loaded  Bytes     Unloaded  Bytes     Time
16277   31353.0   43        39.8      5.81
  • Loaded: 被加载的 class 数量(Number of classes loaded.)
  • Bytes: 加载了多少 KB(Number of KB loaded.)
  • Unloaded:卸载的 class 数量(Number of classes unloaded.)
  • Bytes: 卸载的 Kb(Number of KB unloaded.)
  • Time: 卸载和加载共用时长(Time spent performing class loading and unloading operations.)

2.2.2 compiler

作用:显示VM实时编译的数量等信息。(Java HotSpot VM Just-in-Time compiler statistics.)

$ jstat -compiler 15654

Compiled   Failed  Invalid   Time    FailedType  FailedMethod
11945      0       0         3.38    0
  • Compiled: 执行编译的任务数量(Number of compilation tasks performed.)
  • Failed: 编译失败的数量(Number of compilations tasks failed.)
  • Invalid: 无效的任务数量(Number of compilation tasks that were invalidated.)
  • Time: 编译花费时间(Time spent performing compilation tasks.)
  • FailedType: 上次编译失败的类型(Compile type of the last failed compilation.)
  • FailedMethod: 上次失败的方法名 (Class name and method of the last failed compilation.)

2.2.3 gc

作用:显示gc的信息,查看gc的次数,及时间。(Garbage collected heap statistics.)

$ jstat -gc 15654

S0C         S1C         S0U         S1U     EC           EU           OC            OU         MC         MU          CCSC      CCSU        YGC    YGCT       FGC    FGCT     CGC    CGCT       GCT
29696.0     33792.0     29692.2     0.0     484352.0     257326.0     441344.0      40195.0    83712.0    78306.5     11264.0   10256.5     12     0.103      3      0.178     -     -          0.281
  • S0C: Current survivor space 0 capacity (KB).
  • S1C: Current survivor space 1 capacity (KB).
  • S0U: Survivor space 0 utilization (KB). 已用
  • S1U: Survivor space 1 utilization (KB).
  • EC: Current eden space capacity (KB).
  • EU: Eden space utilization (KB).
  • OC: Current old space capacity (KB).
  • OU: Old space utilization (KB).
  • MC: Metaspace Committed Size (KB).
  • MU: Metaspace utilization (KB).
  • CCSC: Compressed class committed size (KB). 压缩类空间大小
  • CCSU: Compressed class space used (KB).
  • YGC: Number of young generation garbage collection (GC) events. 年轻代垃圾回收次数
  • YGCT: Young generation garbage collection time. 年轻代垃圾回收消耗时间
  • FGC: Number of full GC events.老年代垃圾回收次数
  • FGCT: Full garbage collection time.
  • GCT: Total garbage collection time.垃圾回收消耗总时间

2.2.4 gccapacity

作用:VM 分代内存的使用和占用大小。 Memory pool generation and space capacities.

$ jstat -gccapacity 15654

NGCMN        NGCMX         NGC          S0C        S1C         EC           OGCMN       OGCMX         OGC          OC          MCMN       MCMX         MC            CCSMN     CCSMX       CCSC       YGC     FGC  CGC
174592.0    2796032.0     621568.0     29696.0     33792.0     484352.0     349696.0    5592576.0     441344.0     441344.0    0.0        1122304.0    83712.0       0.0       1048576.0   11264.0     12     3     -
  • NGCMN: Minimum new generation capacity (KB). 新生代最小容量
  • NGCMX: Maximum new generation capacity (KB).
  • NGC: Current new generation capacity (KB). 当前新生代容量
  • S0C: Current survivor space 0 capacity (KB).
  • S1C: Current survivor space 1 capacity (KB).
  • EC: Current eden space capacity (KB). 伊甸园区的大小
  • OGCMN: Minimum old generation capacity (KB). 老年代最小容量
  • OGCMX: Maximum old generation capacity (KB).
  • OGC: Current old generation capacity (KB). 当前老年代大小
  • OC: Current old space capacity (KB). 当前老年代大小
  • MCMN: Minimum metaspace capacity (KB).最小元数据容量
  • MCMX: Maximum metaspace capacity (KB).
  • MC: Metaspace Committed Size (KB). 当前元数据空间大小
  • CCSMN: Compressed class space minimum capacity (KB). 最小压缩类空间大小
  • CCSMX: Compressed class space maximum capacity (KB).
  • CCSC: Compressed class committed size (KB). 当前压缩类空间大小
  • YGC: Number of young generation GC events. 年轻代gc次数
  • FGC: Number of full GC events. 老年代GC次数

2.2.5 gccause

作用:和 -gcutil 相似,但是包含了上次 gc 事件和当前 gc 事件。

$ jstat -gccause 15654

S0      S1     E       O      M      CCS       YGC    YGCT     FGC    FGCT     CGC    CGCT     GCT     LGCC                 GCC
99.99   0.00   63.49   9.11   93.54  91.06     12     0.103    3      0.178     -       -      0.281   Allocation Failure   No GC
  • LGCC: Cause of last garbage collection 上次 gc 原因
  • GCC: Cause of current garbage collection

2.2.6 gcnew

作用:年轻代对象的信息。 New generation statistics.

$ jstat -gcnew 15654

S0C         S1C         S0U         S1U     TT   MTT     DSS        EC           EU           YGC    YGCT
29696.0     33792.0     29692.2     0.0     9    15     33792.0     484352.0     281485.7     12     0.103
  • S0C: Current survivor space 0 capacity (KB).
  • S1C: Current survivor space 1 capacity (KB).
  • S0U: Survivor space 0 utilization (KB). 第一个幸存区的使用大小
  • S1U: Survivor space 1 utilization (KB).
  • TT: Tenuring threshold. 对象在新生代存活的次数
  • MTT: Maximum tenuring threshold. 对象在新生代存活的最大次数
  • DSS: Desired survivor size (KB). 期望的幸存区大小
  • EC: Current eden space capacity (KB). 伊甸园区的大小
  • EU: Eden space utilization (KB). 伊甸园区的使用大小
  • YGC: Number of young generation GC events.
  • YGCT: Young generation garbage collection time.2.2.7 gcnewcapacity

2.2.7 gcnewcapacity

作用:年轻代对象的信息及其占用量。 New generation space size statistics.

$ jstat -gcnewcapacity 15654

NGCMN        NGCMX         NGC         S0CMX        S0C        S1CMX        S1C         ECMX          EC           YGC    FGC   CGC
174592.0     2796032.0     621568.0    931840.0     29696.0    931840.0     33792.0     2795008.0     484352.0     12     3     -
  • NGCMN: Minimum new generation capacity (KB).
  • NGCMX: Maximum new generation capacity (KB).
  • NGC: Current new generation capacity (KB).
  • S0CMX: Maximum survivor space 0 capacity (KB).
  • S0C: Current survivor space 0 capacity (KB).
  • S1CMX: Maximum survivor space 1 capacity (KB).
  • S1C: Current survivor space 1 capacity (KB).
  • ECMX: Maximum eden space capacity (KB).
  • EC: Current eden space capacity (KB).
  • YGC: Number of young generation GC events.
  • FGC: Number of full GC events.

2.2.8 gcold

作用:old 代对象的信息。 Old generation size statistics.

$ jstat -gcold 15654

MC         MU        CCSC      CCSU         OC            OU          YGC    FGC    FGCT     CGC    CGCT      GCT
83712.0    78306.5   11264.0   10256.5      441344.0      40195.0     12     3      0.178     -       -       0.281
  • MC: Metaspace Committed Size (KB).
  • MU: Metaspace utilization (KB).
  • CCSC: Compressed class committed size (KB).
  • CCSU: Compressed class space used (KB).
  • OC: Current old space capacity (KB).
  • OU: Old space utilization (KB).
  • YGC: Number of young generation GC events.
  • FGC: Number of full GC events.
  • FGCT: Full garbage collection time.
  • GCT: Total garbage collection time.

2.2.9 gcoldcapacity

作用:old 代对象的信息。Old generation statistics.

$ jstat -gcoldcapacity 15654

OGCMN        OGCMX         OGC           OC           YGC    FGC    FGCT     CGC    CGCT     GCT
349696.0     5592576.0     441344.0      441344.0     12     3      0.178    -      -        0.281
  • OGCMN: Minimum old generation capacity (KB).
  • OGCMX: Maximum old generation capacity (KB).
  • OGC: Current old generation capacity (KB).
  • OC: Current old space capacity (KB).
  • YGC: Number of young generation GC events.
  • FGC: Number of full GC events.
  • FGCT: Full garbage collection time.
  • GCT: Total garbage collection time.

2.2.10 gcmetacapacity

作用:metaspace 的信息及其占用量。 Metaspace size statistics.

$ jstat -gcmetacapacity 15654

MCMN       MCMX         MC        CCSMN     CCSMX       CCSC        YGC    FGC   FGCT     CGC    CGCT       GCT
0.0        1122304.0    83712.0   0.0       1048576.0   11264.0     12     3     0.178    -      -          0.281
  • MCMN: Minimum metaspace capacity (KB).
  • MCMX: Maximum metaspace capacity (KB).
  • MC: Metaspace Committed Size (KB).
  • CCSMN: Compressed class space minimum capacity (KB).
  • CCSMX: Compressed class space maximum capacity (KB).
  • CCSC: 当前压缩类空间大小
  • YGC: Number of young generation GC events.
  • FGC: Number of full GC events.
  • FGCT: Full garbage collection time.
  • GCT: Total garbage collection time.

2.2.11 gcutil

作用:统计 gc 信息统计。 Summary of garbage collection statistics.

$ jstat -gcutil 15654

S0      S1    E       O     M      CCS       YGC    YGCT      FGC   FGCT     CGC    CGCT       GCT
99.99   0.00  57.25   9.11  93.54  91.06     12     0.103     3     0.178    -       -         0.281
  • S0: Survivor space 0 utilization as a percentage of the space’s current capacity. 幸存0区当前使用比例
  • S1: Survivor space 1 utilization as a percentage of the space’s current capacity.
  • E: Eden space utilization as a percentage of the space’s current capacity. 伊甸园区使用比例
  • O: Old space utilization as a percentage of the space’s current capacity.
  • M: Metaspace utilization as a percentage of the space’s current capacity.
  • CCS: Compressed class space utilization as a percentage. 压缩使用比例
  • YGC: Number of young generation GC events. 年轻代垃圾回收次数
  • YGCT: Young generation garbage collection time.
  • FGC: Number of full GC events.
  • FGCT: Full garbage collection time.
  • GCT: Total garbage collection time.

2.2.12 printcompilation

作用:当前VM执行的信息。 Java HotSpot VM compiler method statistics.

$ jstat -printcompilation 15654

Compiled  Size  Type  Method
12125     150   1     org/apache/http/impl/conn/DefaultClientConnection close
  • Compiled: Number of compilation tasks performed by the most recently compiled method. 最近编译任务数。
  • Size: Number of bytes of byte code of the most recently compiled method. 最近编译大小
  • Type: Compilation type of the most recently compiled method. 最近被编译的方法类型
  • Method: Class name and method name identifying the most recently compiled method. 最近编译的方法类型和名字

3. jmap

作用:打印进程的详细信息,可以生成 heap dump 文件。

jmap [options] pid

3.1 DebuggerException

参考:彻底解决Jmap在mac版本无法使用的问题

jdk 版本>8 ,且项目和命令 jdk 版本一致

3.2 clstats

作用:打印 java 堆中类加载器的信息

$ jmap -clstats 20570

ClassLoader         Parent              CLD*               Classes   ChunkSz   BlockSz  Type
0x00000008000094d8  0x0000000800084090  0x00006000011adcc0       1       512       390  jdk.internal.reflect.DelegatingClassLoader
0x00000008000094d8  0x0000000000000000  0x00006000011a40a0       1       384       242  jdk.internal.reflect.DelegatingClassLoader
0x00000008000094d8  0x0000000800084090  0x00006000011a77a0       1       512       429  jdk.internal.reflect.DelegatingClassLoader

3.3 finalizerinfo

作用:显示在 F-Queue 中等待 Finalizer 线程执行 finalize 方法的对象

$ jmap -finalizerinfo 4562

3.4 histo

作用:显示堆中对象的统计信息,包括类、有多少个实例,合计容量等

$ jmap -histo 20570

 num     #instances         #bytes  class name (module)
-------------------------------------------------------
   1:        191188       24239224  [B (java.base@17.0.3.1)
   2:         17581        5925416  [I (java.base@17.0.3.1)
   3:        145879        3501096  java.lang.String (java.base@17.0.3.1)
   4:         25970        2942520  [Ljava.lang.Object; (java.base@17.0.3.1)
   5:         29503        2596264  java.lang.reflect.Method (java.base@17.0.3.1)
   6:         71868        2299776  java.util.concurrent.ConcurrentHashMap$Node (java.base@17.0.3.1)
   7:         14127        1690728  java.lang.Class (java.base@17.0.3.1)
   8:         12094         961224  [Ljava.util.HashMap$Node; (java.base@17.0.3.1)
   9:         23927         957080  java.util.LinkedHashMap$Entry (java.base@17.0.3.1)
  10:         17479         838992  org.aspectj.weaver.reflect.ShadowMatchImpl
  • -histo:live: 只显示存活的

3.5 dump

作用:生成 java 堆快照。

jmap -dump:live,format=b,file=heap.hprof pid
  • live: 存活的对象,忽略为 all
  • format: 二进制,可不写
  • file: 输出的文件名

3.6 heap

作用:显示堆详细信息,如使用哪种回收器、参数配置、分代状况等

高版本 jdk 在 jhsdb 下。

$ jhsdb jmap --heap --pid 20570
Attaching to process ID 20570, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 17.0.3.1+2-LTS-6

using thread-local object allocation.
Garbage-First (G1) GC with 13 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 8589934592 (8192.0MB)
   NewSize                  = 1363144 (1.2999954223632812MB)
   MaxNewSize               = 5150605312 (4912.0MB)
   OldSize                  = 5452592 (5.1999969482421875MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 22020096 (21.0MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 4194304 (4.0MB)

Heap Usage:
G1 Heap:
   regions  = 2048
   capacity = 8589934592 (8192.0MB)
   used     = 60972752 (58.14814758300781MB)
   free     = 8528961840 (8133.851852416992MB)
   0.7098162546753883% used
G1 Young Generation:
Eden Space:
   regions  = 1
   capacity = 134217728 (128.0MB)
   used     = 4194304 (4.0MB)
   free     = 130023424 (124.0MB)
   3.125% used
Survivor Space:
   regions  = 0
   capacity = 4194304 (4.0MB)
   used     = 1567440 (1.4948272705078125MB)
   free     = 2626864 (2.5051727294921875MB)
   37.37068176269531% used
G1 Old Generation:
   regions  = 15
   capacity = 100663296 (96.0MB)
   used     = 55211008 (52.6533203125MB)
   free     = 45452288 (43.3466796875MB)
   54.847208658854164% used

4. MAT

参考:MAT内存分析工具-独立版安装教程及实战教学

5. Jstatd

了解内容

监视 Java 虚拟机 (JVM),并使远程监视工具能够连接到 JVM.

jstatd -[option]
  • -nr: 当找不到现有的 RMI 注册表时,不尝试使用 jstatd 进程创建一个内部的 RMI 注册表。
  • -p port : 在指定的端口查找 RMI 注册表。如果没有找到,并且没有指定 -nr 选项,则在该端口自行创建一个内部的 RMI 注册表。
  • -n rminame : RMI 注册表中绑定的 RMI 远程对象的名称。默认的名称为 JStatRemoteHost。如果多个 jstatd 服务器在同一主机上运行,你可以通过指定该选项来让每个服务器导出的 RMI 对象具有唯一的名称。不管如何,这样做需要将唯一的服务器名称包含进监控客户端的 hostid 和 vmid 字符串中。
  • -Joption: 将选项参数传递给被 javac 调用的 java 启动程序。例如,-J-Xms48m 设置启动内存为 48 MB。使用 -J 将选项参数传递给执行 Java 应用程序的底层虚拟机,这是一种常见惯例。

6. jstack

参考:jstack 命令解读

作用:打印线程信息,可以进一步定位线程出现长时间停顿的原因,如线程间死锁、 死循环、 请求外部资源导致的长时间等待等。

jstack [options] pid

$ jstack  20570
2022-06-14 16:36:28
Full thread dump Java HotSpot(TM) 64-Bit Server VM (17.0.3.1+2-LTS-6 mixed mode, emulated-client, sharing):

Threads class SMR info:
_java_thread_list=0x00006000026561e0, length=29, elements={
0x00007fc960819400, 0x00007fc91f8be400, 0x00007fc91f8c2800, 0x00007fc93f811a00,
0x00007fc93f812000, 0x00007fc93f816800, 0x00007fc93f81ee00, 0x00007fc960832a00,
0x00007fc948c33200, 0x00007fc948c08c00, 0x00007fc92880aa00, 0x00007fc93fa02200,
0x00007fc96086ba00, 0x00007fc960873600, 0x00007fc960899600, 0x00007fc960898c00,
0x00007fc96089d400, 0x00007fc96089da00, 0x00007fc91fa6ac00, 0x00007fc91fa6ca00,
0x00007fc94fa58e00, 0x00007fc91fa23c00, 0x00007fc95fc03a00, 0x00007fc96089e000,
0x00007fc948c53c00, 0x00007fc95fbfe800, 0x00007fc91fa6fc00, 0x00007fc91fa12c00,
0x00007fc91fa18400
}

"Reference Handler" #2 daemon prio=10 os_prio=31 cpu=2.56ms elapsed=6072.05s tid=0x00007fc960819400 nid=0x4603 waiting on condition  [0x000070000597e000]
   java.lang.Thread.State: RUNNABLE
	at java.lang.ref.Reference.waitForReferencePendingList(java.base@17.0.3.1/Native Method)
	at java.lang.ref.Reference.processPendingReferences(java.base@17.0.3.1/Reference.java:253)
	at java.lang.ref.Reference$ReferenceHandler.run(java.base@17.0.3.1/Reference.java:215)

"Finalizer" #3 daemon prio=8 os_prio=31 cpu=0.32ms elapsed=6072.05s tid=0x00007fc91f8be400 nid=0x4203 in Object.wait()  [0x0000700005a81000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(java.base@17.0.3.1/Native Method)
	- waiting on <no object reference available>
	at java.lang.ref.ReferenceQueue.remove(java.base@17.0.3.1/ReferenceQueue.java:155)
	- locked <0x0000000600a12a28> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(java.base@17.0.3.1/ReferenceQueue.java:176)
	at java.lang.ref.Finalizer$FinalizerThread.run(java.base@17.0.3.1/Finalizer.java:172)
  • -l : (long listing)长列表模式. 额外打印关于锁的信息
  • -m: (mix)混合模式,可以打印 Java 栈和本地方法栈