JVM调试排查工具详解

2024/3/6 Java虚拟机【JVM】

# 1、JDK自带调优工具

# 1.1、Java VisualVM

位置:JDK/bin下面

查看界面

可安装插件: Visual GC插件,当然还有其他的一些插件,在工具中选择安装即可。插件下载地址 (opens new window)

# 1.2、jps

jps(Java Virtual Machine Process Status Tool) 主要用来输出JVM中运行的进程状态信息。

命令

jps [options] [hostid]

-q 不输出类名、Jar名和传入main方法的参数
-m 输出传入main方法的参数
-l 输出main类或Jar的全限名
-v 输出传入JVM的参数
1
2
3
4
5
6

案例

C:\Program Files\Java\jdk1.8.0_331\bin>jps -m -l
11152 com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.gf.alm.business.test.TestA,test
17440 org.jetbrains.jps.cmdline.Launcher C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/plugins/java/lib/jps-builders.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/plugins/java/lib/jps-builders-6.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/plugins/java/lib/jps-javac-extension.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/util-8.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/util_rt.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/platform-loader.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/annotations.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/protobuf.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/jps-model.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/plugins/java/lib/javac2.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/forms_rt.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/lib/util.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2023.3.3/plugins/java/lib/aether-dependency-resolver.jar;C:/Program F
12948 org.jetbrains.idea.maven.server.RemoteMavenServer36
14420 org/netbeans/Main --branding visualvm --cachedir C:\Users\Tang\AppData\Local\VisualVM\Cache/8u131
9908
20172 sun.tools.jps.Jps -m -l
1
2
3
4
5
6
7

# 1.3、jstack

jstack主要用来查看某个Java进程内的线程堆栈信息。

命令

jstack [option] [pid]

-l 长列表. 打印关于锁的附加信息,例如属于java.util.concurrent 的 ownable synchronizers列表.
-F 当’jstack [-l] pid’没有相应的时候强制打印栈信息
-m 打印java和native c/c++框架的所有栈信息.
-h | -help 打印帮助信息
1
2
3
4
5
6

案例:就用Java VisualVM图中的11152的pid线程

C:\Program Files\Java\jdk1.8.0_331\bin>jstack -m 11152
Attaching to process ID 11152, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.331-b09
Deadlock Detection:

No deadlocks found.

----------------- 0 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 1 -----------------
0x00007ff8a76cfec4      ntdll!ZwWaitForMultipleObjects + 0x14
----------------- 2 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 3 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 4 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 5 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 6 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 7 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 8 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 9 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14
----------------- 10 -----------------
0x00007ff8a76cf3f4      ntdll!NtWaitForSingleObject + 0x14

...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 1.4、jinfo

jinfo 是 JDK 自带的命令,可以用来查看正在运行的 java 应用程序的扩展参数,包括Java System属性和JVM命令行参数;也可以动态的修改正在运行的 JVM 一些参数。当系统崩溃时,jinfo可以从core文件里面知道崩溃的Java应用程序的配置信息

命令

jinfo [options] [pid]

no option 输出全部的参数和系统属性
-flag name 输出对应名称的参数
-flag [+|-]name 开启或者关闭对应名称的参数
-flag name=value 设定对应名称的参数
-flags 输出全部的参数
-sysprops 输出系统属性
1
2
3
4
5
6
7
8

案例

C:\Program Files\Java\jdk1.8.0_331\bin>jinfo -flags 11152
Attaching to process ID 11152, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.331-b09
Non-default VM flags: -XX:CICompilerCount=4 -XX:InitialHeapSize=264241152 -XX:MaxHeapSize=4223664128 -XX:MaxNewSize=1407713280 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=88080384 -XX:OldSize=176160768 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
Command line:  -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.3\lib\idea_rt.jar=57143:C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.3\bin -Dfile.encoding=UTF-8
1
2
3
4
5
6
7

# 1.5、jmap

jmap是一个多功能的命令。它可以生成 java 程序的 dump 文件, 也可以查看堆内对象示例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。

命令

jmap [options] [pid]

no option: 查看进程的内存映像信息,类似 Solaris pmap 命令。
heap: 显示Java堆详细信息
histo[:live]: 显示堆中对象的统计信息
clstats:打印类加载器信息
finalizerinfo: 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
dump:<dump-options>:生成堆转储快照
F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
help:打印帮助信息
J<flag>:指定传递给运行jmap的JVM的参数
1
2
3
4
5
6
7
8
9
10
11

案例

C:\Program Files\Java\jdk1.8.0_331\bin>jmap -heap 11152
Attaching to process ID 11152, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.331-b09

using thread-local object allocation.
Parallel GC with 8 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 4223664128 (4028.0MB)
   NewSize                  = 88080384 (84.0MB)
   MaxNewSize               = 1407713280 (1342.5MB)
   OldSize                  = 176160768 (168.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 31981568 (30.5MB)
   used     = 27633272 (26.35314178466797MB)
   free     = 4348296 (4.146858215332031MB)
   86.40374355628842% used
From Space:
   capacity = 524288 (0.5MB)
   used     = 163840 (0.15625MB)
   free     = 360448 (0.34375MB)
   31.25% used
To Space:
   capacity = 524288 (0.5MB)
   used     = 0 (0.0MB)
   free     = 524288 (0.5MB)
   0.0% used
PS Old Generation
   capacity = 176160768 (168.0MB)
   used     = 6912992 (6.592742919921875MB)
   free     = 169247776 (161.40725708007812MB)
   3.924251738048735% used

6874 interned Strings occupying 555136 bytes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 1.6、jstat

jstat(JVM统计监测工具)查看各个区内存和GC的情况

命令

jstat [ generalOption | outputOptions vmid [interval[s|ms] [count]] ]

vmid是Java虚拟机ID,在Linux/Unix系统上一般就是进程ID。interval是采样时间间隔(单位默认毫秒)。count是采样数目。

记住这两个命令就够:
jstat -gc [pid] [interval]
jstat -gcutil [pid] [interval]
1
2
3
4
5
6
7

案例

C:\Program Files\Java\jdk1.8.0_331\bin>jstat -gcutil 11152 250
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
  0.00  43.75  64.25   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  67.34   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  68.71   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  70.98   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  72.80   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  74.16   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  76.27   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  77.54   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  79.65   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  82.03   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  83.15   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  85.39   4.13  94.19  85.42    241    0.272     0    0.000    0.272
  0.00  43.75  86.49   4.13  94.19  85.42    241    0.272     0    0.000    0.272
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 1.7、jconsole

用于对jvm的内存,线程,类 的监控,是一个基于 jmx 的 GUI 性能监控工具。打开方式:java 安装目录 bin目录下 直接启动 jconsole.exe 就行

启动界面

监控页面

# 2、进阶版调优工具

# 2.1、阿里巴巴Arthas(阿尔萨斯)

重点掌握、重点掌握、重点掌握

Java应用诊断利器:Arthas官网 (opens new window)

参数:JVM调优教程 (opens new window)