JVM工具
jps
JPS 查看当前运行java进程
JVM Process Status Tool,显示指定系统内所有的HotSpot虚拟机进程。
用法
1 | jps -lvm 用于查看当前机器上运行的java进程。 |
参数
常用命令行参数选项说明如下:
-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 -lvm1
2
3
4C:\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 -l1
2
3
4C:\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 -V1
2
3
4C:\Users\tomgs>jps -V
13108 Jps
6116 Bootstrap
12892 PULSEI~1.JAR
jinfo
该命令可以打印出java进程的配置信息:包括jvm参数,系统属性等
用法:
1 | jinfo [ option ] pid |
参数:
1 | pid 进程号 |
选项:
1 | no option 打印命令行参数和系统属性 |
示例:
jinfo pid1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25C:\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 pid1
2
3
4
5
6
7C:\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 | -l 打印锁信息 |
示例
将线程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 | C:\Users\tomgs>jstat -gc 12760 |
说明:
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 | $ jstat -gcutil 21891 250 7 |
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 | $ jstat -gcnew -h3 21891 250 |
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. |
什么情况下新生代对象会晋升到老年代?
- 当对象大小超过了PretenureSizeThreshold设置的对象大小阀值时,对象直接在老年代分配空间。
- 当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>
:
Passesto the Java virtual machine on which jmap is run
示例
heap
先用jps
找出相关Java线程的pid,然后使用此命令。
$ jmap -heap pid
1 | C:\Users\tomgs>jmap -heap 13812 |
histo[:live]
打印堆的对象统计,包括对象数、内存大小等等。jmap -histo:live 这个命令执行,JVM会先触发Full GC,然后再统计信息。带live
子选项的只会统计活对象。
$ jmap -histo[:live] pid
1 | C:\Users\tomgs> jmap -histo:live 13812 | more |
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 | C:\Users\tomgs>jmap -permstat 13812 |
dump
dump堆到文件,format指定输出格式,live指明是活着的对象,file指定文件名
$ jmap -dump[:live],format=b,file=[filename].hprof pid
示例:1
2
3C:\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内存。