JVM工具

jps

JPS 查看当前运行java进程
JVM Process Status Tool,显示指定系统内所有的HotSpot虚拟机进程。

用法

1
2
3
4
jps -lvm 用于查看当前机器上运行的java进程。 

命令格式 jps [options] [hostid]
注:如果不指定hostid就默认为当前主机或服务器。

参数

常用命令行参数选项说明如下:

-q  不输出类名、Jar名和传入main方法的参数

-m 输出传入main方法的参数

-l   输出main类或Jar的全限名

-v  输出传入JVM的参数

-V 输出通过标记的文件传递给JVM的参数(.hotspotrc文件,或者是通过参数-XX:Flags=指定的文件)。

-J 用于传递jvm选项到由javac调用的java加载器中,例如,“-J-Xms48m”将把启动内存设置为48M,使用-J选项可以非常方便的向基于Java的开发的底层虚拟机应用程序传递参数。

jps有问题, 使用 jps -J-Djps.debug=true -J-Djps.printStackTrace=true 可以获得Jps错误详细信息

示例:

jps -lvm

1
2
3
4
C:\Users\tomgs>jps -lvm
6116 org.apache.catalina.startup.Bootstrap start -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:63138 -Djava.net.preferIPv4Stack=true -Dcatalina.base=E:\apache-tomcat-7.0.65 -Dcatalina.home=E:\apache-tomcat-7.0.65 -Dwtp.deploy=E:\apache-tomcat-7.0.65\me-webapps -Djava.endorsed.dirs=E:\apache-tomcat-7.0.65\endorsed -Dfile.encoding=UTF-8
6000 sun.tools.jps.Jps -lvm -Denv.class.path=.;D:\apps\Java\jdk1.7\bin;D:\apps\Java\jdk1.7\lib\dt.jar;D:\apps\Java\jdk1.7\lib\tools.jar -Dapplication.home=D:\apps\Java\jdk1.7 -Xms8m
12892 C:\Users\tomgs\AppData\Local\Temp\pul9953.tmp\PULSEI~1.JAR -os win32 -ws win32 -arch x86_64 -showsplash -launcher D:\apps\MyEclipse 2015 CI\myeclipse.exe -name Myeclipse --launcher.library D:\apps\MyEclipse 2015 CI\plugins/org.eclipse.equinox.launcher.i18n.win32.win32.x86_64_3.2.0.v201103301700\eclipse_3215.dll -startup D:\apps\MyEclipse 2015 CI\plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar -exitdata 8ac_f4 -install D:\apps\MyEclipse 2015 CI -vm D:\apps\MyEclipse 2015 CI\binary/com.sun.java.jdk7.win32.x86_64_1.7.0.u45/bin/javaw.exe -vmargs -Xmx2048m -Xms512m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m -Dosgi.nls.warnings=ignore -Dfile.encoding=UTF-8 -jar C:\Users\tomgs\AppData\Local\Temp\pul9953.tmp\PULSEI~1.JAR -Xmx2048m -Xms512m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m -Dosgi.nls.warnings=ignore -Dfile.encoding=UTF-8

jps -l

1
2
3
4
C:\Users\tomgs>jps -l
6116 org.apache.catalina.startup.Bootstrap
12124 sun.tools.jps.Jps
12892 C:\Users\tomgs\AppData\Local\Temp\pul9953.tmp\PULSEI~1.JAR

jps -V

1
2
3
4
C:\Users\tomgs>jps -V
13108 Jps
6116 Bootstrap
12892 PULSEI~1.JAR

jinfo

该命令可以打印出java进程的配置信息:包括jvm参数,系统属性等

用法:

1
2
3
jinfo [ option ] pid
jinfo [ option ] executable core
jinfo [ option ] [server-id@]remote-hostname-or-IP

参数:

1
2
3
4
5
pid   进程号
executable 产生core dump的java executable
core core file
remote-hostname-or-IP 主机名或ip
server-id 远程主机上的debug server的唯一id

选项:

1
2
3
4
no option  打印命令行参数和系统属性
-flags 打印命令行参数
-sysprops 打印系统属性
-h 帮助

示例:

jinfo pid

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
C:\Users\tomgs>jinfo 6116
Attaching to process ID 6116, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.79-b02
Java System Properties:

java.vendor = Oracle Corporation
sun.java.launcher = SUN_STANDARD
catalina.base = E:\apache-tomcat-7.0.65
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
catalina.useNaming = true
os.name = Windows 8.1

......

wtp.deploy = E:\apache-tomcat-7.0.65\me-webapps
PAYMENT_LOG_PATH = E:\apache-tomcat-7.0.65\me-webapps\z_test_payment-gateway\logs
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
file.encoding = UTF-8
java.specification.version = 1.7

VM Flags: //JVM参数

-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:63138 -Djava.net.preferIPv4Stack=true -Dcatalina.base=E:\apache-tomcat-7.0.65 -Dcatalina.home=E:\apache-tomcat-7.0.65 -Dwtp.deploy=E:\apache-tomcat-7.0.65\me-webapps -Djava.endorsed.dirs=E:\apache-tomcat-7.0.65\endorsed -Dfile.encoding=UTF-8

jinfo -flags pid

1
2
3
4
5
6
7
C:\Users\tomgs>jinfo -flags 6116
Attaching to process ID 6116, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.79-b02

-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:63138 -Djava.net.preferIPv4Stack=true -Dcatalina.base=E:\apache-tomcat-7.0.65 -Dcatalina.home=E:\apache-tomcat-7.0.65 -Dwtp.deploy=E:\apache-tomcat-7.0.65\me-webapps -Djava.endorsed.dirs=E:\apache-tomcat-7.0.65\endorsed -Dfile.encoding=UTF-8

jstack

打印线程信息

用法

1
jstack [ option ] pid

选项

1
2
3
-l 打印锁信息
-m 打印java和native的帧信息
-F 强制dump,当jstack没有响应时使用

示例

将线程pid为120的线程dump到c盘的a.txt文件中

1
jstack 1200 >> C:\a.txt

分析另起文章说明。

jstat

jstat(JVM statistics Monitoring)是用于监视虚拟机运行时状态信息的命令,它可以显示出虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据。

详细请见官方文档介绍:https://docs.oracle.com/javase/7/docs/technotes/tools/share/jstat.html

用法

jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

选项

option - 选项,我们一般使用 -gcutil 查看gc情况

vmid - VM的进程号,即当前运行的java进程号

interval - 间隔时间,单位为秒或者毫秒

count - 打印次数,如果缺省则打印无数次

option

Option Displays
class Statistics on the behavior of the class loader.
compiler Statistics of the behavior of the HotSpot Just-in-Time compiler.
gc Statistics of the behavior of the garbage collected heap.
gccapacity Statistics of the capacities of the generations and their corresponding spaces.
gccause Summary of garbage collection statistics (same as -gcutil), with the cause of the last and current (if applicable) garbage collection events.
gcnew Statistics of the behavior of the new generation.
gcnewcapacity Statistics of the sizes of the new generations and its corresponding spaces.
gcold Statistics of the behavior of the old and permanent generations.
gcoldcapacity Statistics of the sizes of the old generation.
gcpermcapacity Statistics of the sizes of the permanent generation.
gcutil Summary of garbage collection statistics.
printcompilation HotSpot compilation method statistics.

示例

-gc

jstat -gc pid [interval] [count]
1
2
3
4
5
6
7
8
9
10
11
12
C:\Users\tomgs>jstat -gc 12760
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
512.0 512.0 0.0 184.4 3072.0 1438.0 7168.0 605.4 21504.0 2572.2 121 0.163 0 0.000 0.163

C:\Users\tomgs>jstat -gc 12760 250 6
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
512.0 512.0 208.3 0.0 3072.0 1262.1 7168.0 605.4 21504.0 2572.5 166 0.229 0 0.000 0.229
512.0 512.0 0.0 256.0 3072.0 2376.6 7168.0 605.4 21504.0 2572.5 167 0.231 0 0.000 0.231
512.0 512.0 0.0 240.3 3072.0 0.0 7168.0 605.4 21504.0 2572.5 169 0.233 0 0.000 0.233
512.0 512.0 192.0 0.0 3072.0 315.4 7168.0 605.4 21504.0 2572.5 170 0.234 0 0.000 0.234
512.0 512.0 0.0 240.3 3072.0 1689.4 7168.0 605.4 21504.0 2572.7 171 0.236 0 0.000 0.236
512.0 512.0 0.0 160.0 3072.0 0.0 7168.0 605.4 21504.0 2572.7 173 0.238 0 0.000 0.238

说明:

C 即Capacity 总容量,U 即Used 已使用的容量

S0C : survivor0区的总容量

S1C : survivor1区的总容量

S0U : survivor0区已使用的容量

S1C : survivor1区已使用的容量

EC : Eden区的总容量

EU : Eden区已使用的容量

OC : Old区的总容量

OU : Old区已使用的容量

MC :方法区大小

MU :方法区使用大小

CCSC : 压缩类空间大小

CCSU : 压缩类空间使用大小

YGC : 新生代垃圾回收次数

YGCT : 新生代垃圾回收时间

FGC : 老年代垃圾回收次数

FGCT : 老年代垃圾回收时间

GCT : 垃圾回收总消耗时间

-gcutil

jstat -gcutil pid [interval] [count]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ jstat -gcutil 21891 250 7

S0 S1 E O P YGC YGCT FGC FGCT GCT

12.44 0.00 27.20 9.49 96.70 78 0.176 5 0.495 0.672

12.44 0.00 62.16 9.49 96.70 78 0.176 5 0.495 0.672

12.44 0.00 83.97 9.49 96.70 78 0.176 5 0.495 0.672

0.00 7.74 0.00 9.51 96.70 79 0.177 5 0.495 0.673

0.00 7.74 23.37 9.51 96.70 79 0.177 5 0.495 0.673

0.00 7.74 43.82 9.51 96.70 79 0.177 5 0.495 0.673

0.00 7.74 58.11 9.51 96.71 79 0.177 5 0.495 0.673

The output of this example shows that a young generation collection occurred between the 3rd and 4th sample. The collection took 0.001 seconds and promoted objects from the eden space (E) to the old space (O), resulting in an increase of old space utilization from 9.49% to 9.51%. Before the collection, the survivor space was 12.44% utilized, but after this collection it is only 7.74% utilized.

Column Description
S0 Survivor space 0 utilization as a percentage of the space’s current capacity.
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.
P Permanent space utilization as a percentage of the space’s current capacity.
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.

-gcnew

jstat -gcnew [outputOptions] pid [interval]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ jstat -gcnew -h3 21891 250

S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT

64.0 64.0 0.0 31.7 31 31 32.0 512.0 178.6 249 0.203

64.0 64.0 0.0 31.7 31 31 32.0 512.0 355.5 249 0.203

64.0 64.0 35.4 0.0 2 31 32.0 512.0 21.9 250 0.204

S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT

64.0 64.0 35.4 0.0 2 31 32.0 512.0 245.9 250 0.204

64.0 64.0 35.4 0.0 2 31 32.0 512.0 421.1 250 0.204

64.0 64.0 0.0 19.0 31 31 32.0 512.0 84.4 251 0.204

S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT

64.0 64.0 0.0 19.0 31 31 32.0 512.0 306.7 251 0.204

In addition to showing the repeating header string, this example shows that between the 2nd and 3rd samples, a young GC occurred. Its duration was 0.001 seconds. The collection found enough live data that the survivor space 0 utilization (S0U) would would have exceeded the desired survivor Size (DSS). As a result, objects were promoted to the old generation (not visible in this output), and the tenuring threshold (TT) was lowered from 31 to 2.

Another collection occurs between the 5th and 6th samples. This collection found very few survivors and returned the tenuring threshold to 31.

Column Description
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.

什么情况下新生代对象会晋升到老年代?

  1. 当对象大小超过了PretenureSizeThreshold设置的对象大小阀值时,对象直接在老年代分配空间。
  2. 当age从1开始的对象大小累计超过了Survivor区域的1/2(TargetSurvivorRatio所定义)时,会计算一个Thenuring Threshold,超过这个年龄的新生代对象会进入到老年代,即使这时候新生代还有很多的空间。注意MaxTenuringThreshold只是设置了最大的Thenuring Threshold,不是说只有大于Max Tenuring Threshold才会进入到老年代,而是只要超过了计算出来的Tenuring Threshold就会进入老年代,MaxTenuringThreshold规定了Tenuring Threshold的最大值而已。Tenuring Threshold这个值在每一轮GC后都会动态计算,它与TargetSurvivorRatio以及Survivor区的大小有关系,TargetSurivivor默认是50即Survivor的1/2, 会计算出一个Desired Survivor Size,当age从1开始的对象大小累计超过了这个Desired Survivor Size,那么这个age就是Tenuring Threshold的值。

jmap

JVM Memory Map命令用于生成heap dump文件,如果不使用这个命令,还可以使用-XX:+HeapDumpOnOutOfMemoryError参数来让虚拟机出现OOM的时候自动生成dump文件。 jmap不仅能生成dump文件,还可以查询finalize执行队列、Java堆和永久代的详细信息,如当前使用率、当前使用的是哪种收集器等。

jmap命令官方文档:https://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html

用法

jmap [option] pid

具体说明可以使用 jmap -help 查看

选项说明

-dump:[live,]format=b,file=<filename> : Dumps the Java heap in hprof binary format to filename. The live suboption is optional. If specified, only the live objects in the heap are dumped. To browse the heap dump, you can use jhat (Java Heap Analysis Tool) to read the generated file.

-finalizerinfo : 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象

-heap : 显示Java堆详细信息

-histo[:live] : 显示堆中对象的统计信息,如果指定了live子选项,则只计数活动对象。

-permstat : to print permanent generation statistics

-F : 当-dump没有响应时,强制生成dump快照

-J<flag> :
Passes to the Java virtual machine on which jmap is run

示例

heap

先用jps找出相关Java线程的pid,然后使用此命令。

$ jmap -heap pid
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
48
49
50
51
52
C:\Users\tomgs>jmap -heap 13812
Attaching to process ID 13812, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.79-b02

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

Heap Configuration: //堆内存初始化配置
MinHeapFreeRatio = 0//对应jvm启动参数-XX:MinHeapFreeRatio设置JVM堆最小空闲比率(default 40)
MaxHeapFreeRatio = 100//对应jvm启动参数 -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率(default 70)
MaxHeapSize = 1040187392 (992.0MB) //对应jvm启动参数-XX:MaxHeapSize=设置JVM堆的最大大小
NewSize = 21495808 (20.5MB)//对应jvm启动参数-XX:NewSize=设置JVM堆的‘新生代’的默认大小
MaxNewSize = 346554368 (330.5MB)//对应jvm启动参数-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
OldSize = 43515904 (41.5MB)//对应jvm启动参数-XX:OldSize=<value>:设置JVM堆的‘老生代’的大小
NewRatio = 2//对应jvm启动参数-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
SurvivorRatio = 8//对应jvm启动参数-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值

PermSize = 21757952 (20.75MB)
MaxPermSize = 85983232 (82.0MB)
G1HeapRegionSize = 0 (0.0MB)

Heap Usage: //堆内存使用情况
PS Young Generation
Eden Space:
capacity = 5242880 (5.0MB)
used = 3618584 (3.4509506225585938MB)
free = 1624296 (1.5490493774414062MB)
69.01901245117188% used
From Space:
capacity = 524288 (0.5MB)
used = 229376 (0.21875MB)
free = 294912 (0.28125MB)
43.75% used
To Space:
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
PS Old Generation
capacity = 7340032 (7.0MB)
used = 688224 (0.656341552734375MB)
free = 6651808 (6.343658447265625MB)
9.376307896205358% used
PS Perm Generation
capacity = 22020096 (21.0MB)
used = 2633520 (2.5115203857421875MB)
free = 19386576 (18.488479614257812MB)
11.959620884486608% used

1522 interned Strings occupying 140184 bytes.

histo[:live]

打印堆的对象统计,包括对象数、内存大小等等。jmap -histo:live 这个命令执行,JVM会先触发Full GC,然后再统计信息。带live 子选项的只会统计活对象。

$ jmap -histo[:live] pid
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
C:\Users\tomgs> jmap -histo:live 13812 | more

num #instances #bytes class name
----------------------------------------------
1: 5833 752160 <methodKlass>
2: 5833 676232 <constMethodKlass>
3: 389 467400 <constantPoolKlass>
4: 1924 288568 [C
5: 352 280928 <constantPoolCacheKlass>
6: 389 266520 <instanceKlassKlass>
7: 545 91448 [B
8: 451 44296 java.lang.Class
9: 1775 42600 java.lang.String
10: 636 42400 [[I
11: 593 36448 [S
12: 785 31400 java.util.TreeMap$Entry
13: 45 24480 <objArrayKlassKlass>
14: 39 15752 <methodDataKlass>
15: 314 13352 [Ljava.lang.Object;
16: 187 7640 [Ljava.lang.String;
17: 8 4352 <typeArrayKlassKlass>
18: 93 3720 java.lang.ref.SoftReference
19: 114 3648 java.util.Hashtable$Entry
20: 37 2368 java.net.URL
21: 11 2288 <klassKlass>
22: 38 1824 sun.util.locale.LocaleObjectCache$CacheEntry
23: 55 1760 java.util.concurrent.ConcurrentHashMap$HashEntry
24: 44 1760 java.util.concurrent.ConcurrentHashMap$Segment
25: 38 1520 java.util.LinkedHashMap$Entry
26: 44 1408 java.util.concurrent.locks.ReentrantLock$NonfairSync
27: 44 1184 [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
28: 13 1168 [Ljava.util.HashMap$Entry;
29: 3 1112 [I
30: 1 1040 [[C
31: 6 992 [Ljava.util.Hashtable$Entry;
32: 36 864 java.io.ExpiringCache$Entry
33: 26 832 java.util.HashMap$Entry
34: 17 816 java.util.HashMap
35: 19 760 sun.util.locale.BaseLocale$Key
36: 19 760 java.io.ObjectStreamField
37: 10 720 java.lang.reflect.Constructor
38: 8 640 [Ljava.util.concurrent.ConcurrentHashMap$Segment;
39: 6 624 java.lang.Thread
40: 19 608 sun.util.locale.BaseLocale
...

jmap -histo:live 24971 | java.lang.String 查询类名包含java.lang.String的信息

jmap -histo:live 24971 | grep java.lang.String > histo.txt 保存信息到histo.txt文件

permstat

打印Java堆内存的永久区的类加载器的智能统计信息。对于每个类加载器而言,它的名称、活跃度、地址、父类加载器、它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。

使用方法:

$ jmap -permstat pid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\Users\tomgs>jmap -permstat 13812
Attaching to process ID 13812, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.79-b02
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness........done.
class_loader classes bytes parent_loader alive? type

<bootstrap> 388 2581896 null live <internal>
0x00000000fec39b28 8 102232 0x00000000fec3a1c8 live sun/misc/Launcher$AppClassLoader@0x00000000f9c3b030
0x00000000fec3a1c8 0 0 null live sun/misc/Launcher$ExtClassLoader@0x00000000f9be6128

total = 3 396 2684128 N/A alive=3, dead=0 N/A

dump

dump堆到文件,format指定输出格式,live指明是活着的对象,file指定文件名

$ jmap -dump[:live],format=b,file=[filename].hprof pid    

示例:

1
2
3
C:\Users\tomgs>jmap -dump:live,format=b,file=D:/dump.hprof 13812
Dumping heap to D:\dump.hprof ...
Heap dump file created

使用jmap来dump内存时,系统会被暂停,它会先做一次Full GC再dump内存。