很多开发人员都不是很了解,jdk在安装的时候在bin目录下有很多方便我们调试的工具,有的工具是非常好用的,下面介绍一下jdk自带的调优工具和一些常见的非自带的工具:
名称 |
作用 |
jps |
JVM Process Status Tool,现实指定系统内所有的HotSpot虚拟机进程 |
jstat |
JVM Statistics Monitoring Tool,用于收集Hotspot虚拟机各个方面的运行参数 |
jinfo |
Configuration Info for Java,现实虚拟机配置信息 |
jmap |
Memory map for java,生成虚拟机的内存转储快照 |
jhat |
JVM heap Dunp Browser,用于分析heapdump文件,他会建立一个HTTP/HTML服务,让用户可通过浏览器查看 |
jstack |
Stack Track for java ,显示虚拟机线程快照 |
1.jps
jps(Java Virtual Machine Process Status Tool)
jps主要用来输出JVM中运行的进程状态信息。语法格式如下:
jps [options] [hostid]
如果不指定hostid就默认为当前主机或服务器。
命令行参数选项说明如下:
-q 不输出类名、Jar名和传入main方法的参数
-m 输出传入main方法的参数
-l 输出main类或Jar的全限名
-v 输出传入JVM的参数
568 Bootstrap -Djava.util.logging.config.file=/usr/local/tomcat/flybus/tomcat-flybus-admin/conf/logging.properties -Xms256m -Xmx512m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true -Djmagick.systemclassloader=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/flybus/tomcat-flybus-admin/endorsed -Dcatalina.base=/usr/local/tomcat/flybus/tomcat-flybus-admin -Dcatalina.home=/usr/local/tomcat/flybus/tomcat-flybus-admin -Djava.io.tmpdir=/usr/local/tomcat/flybus/tomcat-flybus-admin/temp
[root@develop117 bin]# ./jps -v localhost
例子命令在上面
./jps -v localhost,放下面
2.jstat
jstat [ generalOption | outputOptions vmid [interval[s|ms] [count]] ]
vmid是Java虚拟机ID,在Linux/Unix系统上一般就是进程ID。interval是采样时间间隔。count是采样数目。比如下面输出的是GC信息,采样时间间隔为500ms,采样数为10:
各个参数解释如下表
S0C |
新生代中Survivor space中S0当前容量的大小(KB) |
S1C |
新生代中Survivor space中S1当前容量的大小(KB) |
S0U |
新生代中Survivor space中S0容量使用的大小(KB) |
S1U |
新生代中Survivor space中S1容量使用的大小(KB) |
EC |
Eden space当前容量的大小(KB) |
EU |
Eden space容量使用的大小(KB) |
OC |
Old space当前容量的大小(KB) |
OU |
Old space使用容量的大小(KB) |
PC |
Permanent space当前容量的大小(KB) |
PU |
Permanent space使用容量的大小(KB) |
YGC |
从应用程序启动到采样时发生 Young GC 的次数 |
YGCT |
从应用程序启动到采样时 Young GC 所用的时间(秒) |
FGC |
从应用程序启动到采样时发生 Full GC 的次数 |
FGCT |
从应用程序启动到采样时 Full GC 所用的时间(秒) |
GCT |
T从应用程序启动到采样时用于垃圾回收的总时间(单位秒),它的值等于YGC+FGC
|
堆内存 = 年轻代 + 年老代 + 永久代
年轻代 = Eden区 + 两个Survivor区(From和To)
3.jinfo
jinfo可以输出并修改运行时的java 进程的opts。用处比较简单,用于输出JAVA系统参数及命令行参数。
命令格式:
jinfo [option] pid
[root@develop117 bin]# ./jinfo 568
Attaching to process ID 568, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.45-b01
Java System Properties:
java.runtime.name = Java(TM) SE Runtime Environment
sun.rmi.transport.tcp.responseTimeout = 3000000
sun.boot.library.path = /usr/local/java/jre/lib/amd64
java.vm.version = 20.45-b01
shared.loader =
java.vm.vendor = Sun Microsystems Inc.
java.vendor.url = http://java.sun.com/
path.separator = :
tomcat.util.buf.StringCache.byte.enabled = true
java.util.logging.config.file = /usr/local/tomcat/flybus/tomcat-flybus-admin/conf/logging.properties
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg = sun.io
sun.java.launcher = SUN_STANDARD
user.country = CN
sun.os.patch.level = unknown
java.vm.specification.name = Java Virtual Machine Specification
user.dir = /root
java.runtime.version = 1.6.0_45-b06
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
java.endorsed.dirs = /usr/local/tomcat/flybus/tomcat-flybus-admin/endorsed
os.arch = amd64
java.io.tmpdir = /usr/local/tomcat/flybus/tomcat-flybus-admin/temp
line.separator =
java.vm.specification.vendor = Sun Microsystems Inc.
java.naming.factory.url.pkgs = org.apache.naming
java.util.logging.manager = org.apache.juli.ClassLoaderLogManager
os.name = Linux
sun.jnu.encoding = UTF-8
java.library.path = /usr/local/java/jre/lib/amd64/server:/usr/local/java/jre/lib/amd64:/usr/local/java/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.specification.name = Java Platform API Specification
java.class.version = 50.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
flybus-admin.root = /project/flybus/flybus-admin/
os.version = 3.8.13-44.1.1.el6uek.x86_64
user.home = /root
catalina.useNaming = true
user.timezone = Asia/Shanghai
java.awt.printerjob = sun.print.PSPrinterJob
file.encoding = UTF-8
java.specification.version = 1.6
catalina.home = /usr/local/tomcat/flybus/tomcat-flybus-admin
java.class.path = /usr/local/tomcat/flybus/tomcat-flybus-admin/bin/bootstrap.jar
user.name = root
java.naming.factory.initial = org.apache.naming.java.javaURLContextFactory
package.definition = sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
java.vm.specification.version = 1.0
sun.java.command = org.apache.catalina.startup.Bootstrap start
java.home = /usr/local/java/jre
sun.arch.data.model = 64
user.language = zh
java.specification.vendor = Sun Microsystems Inc.
java.vm.info = mixed mode
java.version = 1.6.0_45
java.ext.dirs = /usr/local/java/jre/lib/ext:/usr/java/packages/lib/ext
jmagick.systemclassloader = false
sun.boot.class.path = /usr/local/java/jre/lib/resources.jar:/usr/local/java/jre/lib/rt.jar:/usr/local/java/jre/lib/sunrsasign.jar:/usr/local/java/jre/lib/jsse.jar:/usr/local/java/jre/lib/jce.jar:/usr/local/java/jre/lib/charsets.jar:/usr/local/java/jre/lib/modules/jdk.boot.jar:/usr/local/java/jre/classes
server.loader =
java.awt.headless = true
java.vendor = Sun Microsystems Inc.
catalina.base = /usr/local/tomcat/flybus/tomcat-flybus-admin
file.separator = /
java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgi
common.loader = ${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
package.access = sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.,sun.beans.
sun.cpu.isalist =
VM Flags:
-Djava.util.logging.config.file=/usr/local/tomcat/flybus/tomcat-flybus-admin/conf/logging.properties -Xms256m -Xmx512m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true -Djmagick.systemclassloader=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/flybus/tomcat-flybus-admin/endorsed -Dcatalina.base=/usr/local/tomcat/flybus/tomcat-flybus-admin -Dcatalina.home=/usr/local/tomcat/flybus/tomcat-flybus-admin -Djava.io.tmpdir=/usr/local/tomcat/flybus/tomcat-flybus-admin/temp
上面的日志是一个使用tomcat服务器的web服务,是不是有点晕。
4.jmap
jmap用于生成堆转储快照(一般称为heapdump或者dump文件)。当然也可其他方法比如加参数-XX:+HeapDumpOnOutOfMemoryError参数,在虚拟机OOM异常的之后自动生成dump文件,也可以通过-XX:+HeapDumpOnCtrlBreak参数则可以使用Ctrl+Break键让虚拟机生成dump文件。在前文《 JAVA虚拟机之3:CMS垃圾收集器》测试中就有生成。dump文件生成后可借助jha、MAT( Eclipse Memory Analyzer tool)、IBM HeapAnalyzer来对dump分析。jmap不仅能获取dump还可以查询finalize执行队列,java堆和永久代详细信息,空间使用率,当前用的是什么收集器等。
jmap语法格式如下:
jmap [option] pid
jmap [option] executable core
jmap [option] [server-id@]remote-hostname-or-ip
如果运行在64位JVM上,可能需要指定-J-d64命令选项参数。
jmap -J-d64 -heap pid
命令格式:
jmap [ option ] pid
参数说明:
-dump:[live,]format=b,file= 使用hprof二进制形式,输出jvm的heap内容到文件=. live子选项是可选的,假如指定live选项,那么只输出活的对象到文件.
-finalizerinfo 打印正等候回收的对象的信息.
-heap 打印heap的概要信息,GC使用的算法,heap的配置及wise heap的使用情况.
-histo[:live] 打印每个class的实例数目,内存占用,类全名信息. VM的内部类名字开头会加上前缀”*”. 如果live子参数加上后,只统计活的对象数量.
-permstat 打印classload和jvm heap长久层的信息. 包含每个classloader的名字,活泼性,地址,父classloader和加载的class数量. 另外,内部String的数量和占用内存数也会打印出来.
-F 强迫.在pid没有相应的时候使用-dump或者-histo参数. 在这个模式下,live子参数无效..
使用样例:
A.使用jmap -heap pid查看进程堆内存使用情况,包括使用的GC算法、堆配置参数和各代中堆内存使用情况。比如下面的例子:
[root@develop117 bin]# ./jmap -heap 568
Attaching to process ID 568, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.45-b01
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 536870912 (512.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 268435456 (256.0MB)
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 67108864 (64.0MB)
MaxPermSize = 134217728 (128.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 88801280 (84.6875MB)
used = 22372792 (21.33635711669922MB)
free = 66428488 (63.35114288330078MB)
25.194222425622694% used
From Space:
capacity = 327680 (0.3125MB)
used = 0 (0.0MB)
free = 327680 (0.3125MB)
0.0% used
To Space:
capacity = 327680 (0.3125MB)
used = 0 (0.0MB)
free = 327680 (0.3125MB)
0.0% used
PS Old Generation
capacity = 178978816 (170.6875MB)
used = 14848184 (14.160331726074219MB)
free = 164130632 (156.52716827392578MB)
8.296056668516568% used
PS Perm Generation
capacity = 67371008 (64.25MB)
used = 48607200 (46.355438232421875MB)
free = 18763808 (17.894561767578125MB)
72.14854199598736% used
B.jmap -permstat pid
打印进程的类加载器和类加载器加载的持久代对象信息,输出:类加载器名称、对象是否存活(不可靠)、对象地址、父类加载器、已加载的类大小等信息,如下:
[root@develop117 bin]# ./jmap -permstat 568
Attaching to process ID 568, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.45-b01
16140 intern Strings occupying 1783912 bytes.
finding class loader instances ..Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
done.
computing per loader stat ..done.
please wait.. computing liveness............................liveness analysis may be inaccurate ...
class_loader classes bytes parent_loader alive? type
2493 14855744 null live
0x00000000e0773800 1 1944 0x00000000e01c99b8 dead sun/reflect/DelegatingClassLoader@0x00000000d8067830
total = 207 7595 48487296 N/A alive=26, dead=181 N/A
C.使用jmap -heap pid查看进程堆内存使用情况,包括使用的GC算法、堆配置参数和各代中堆内存使用情况。比如下面的例子:
[root@develop117 bin]# ./jmap -heap 568
Attaching to process ID 568, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.45-b01
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 536870912 (512.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 268435456 (256.0MB)
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 67108864 (64.0MB)
MaxPermSize = 134217728 (128.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 88145920 (84.0625MB)
used = 1621408 (1.546295166015625MB)
free = 86524512 (82.51620483398438MB)
1.8394589335501859% used
From Space:
capacity = 327680 (0.3125MB)
used = 0 (0.0MB)
free = 327680 (0.3125MB)
0.0% used
To Space:
capacity = 655360 (0.625MB)
used = 0 (0.0MB)
free = 655360 (0.625MB)
0.0% used
PS Old Generation
capacity = 178978816 (170.6875MB)
used = 15780800 (15.04974365234375MB)
free = 163198016 (155.63775634765625MB)
8.817132861131453% used
PS Perm Generation
capacity = 67371008 (64.25MB)
used = 48657272 (46.40319061279297MB)
free = 18713736 (17.84680938720703MB)
72.22286476699294% used
[root@develop117 bin]#
D.使用jmap -histo[:live] pid查看堆内存中的对象数目、大小统计直方图,如果带上live则只统计活对象,如下:
[root@develop117 bin]# ./jmap -histo:live 568 | more
num #instances #bytes class name
----------------------------------------------
1: 66979 10485224
2: 66979 9119528
3: 6324 7213368
4: 106075 6052696
5: 44944 5260600 [C
6: 6324 4773896
7: 5526 4452336
8: 9718 3255512 [B
9: 49626 1588032 java.lang.String
10: 1985 1329544
11: 6876 715104 java.lang.Class
12: 8684 615896 [S
13: 6854 603264 [Ljava.util.HashMap$Entry;
14: 18715 598880 java.util.HashMap$Entry
15: 10192 507944 [[I
16: 6619 467656 [I
17: 5120 450560 java.lang.reflect.Method
18: 8701 434216 [Ljava.lang.Object;
19: 10402 416080 java.util.LinkedHashMap$Entry
20: 7728 309120 java.util.concurrent.ConcurrentHashMap$Segment
21: 523 305432
22: 7921 253472 java.util.concurrent.locks.ReentrantLock$NonfairSync
23: 7728 242408 [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
24: 5494 219760 java.lang.ref.SoftReference
25: 3660 175680 java.util.HashMap
26: 4740 151680 java.lang.ref.WeakReference
27: 4728 148752 [Ljava.lang.String;
28: 2662 127776 org.apache.catalina.loader.ResourceEntry
29: 5275 126600 java.util.ArrayList
30: 1407 101304 java.lang.reflect.Constructor
E.用jmap把进程内存使用情况dump到文件中,再用jhat分析查看。jmap进行dump命令格式如下:
jmap -dump:format=b,file=dumpFileName pid
./jmap -dump:format=b,file=/usr/local/my.bin
[root@tools138 ~]#./ jmap -dump:format=b,file=my.bin 568
Dumping heap to /root/my.bin ...
Heap dump file created
jhat -port 9998 /usr/local/my.bin,端口默认是7000
5.jhat
jhat是sun提供的dump分析工具,上面讲过分析dump的工具还有MAT( Eclipse Memory Analyzer tool)、IBM HeapAnalyzer等,一般这个命令不太用到,是因为分析dump是个既耗时又耗机器资源的过程,第二个原因是这个工具比较简陋,没有MAT( Eclipse Memory Analyzer tool)、IBM HeapAnalyzer这些专业和强大。
命令格式:
jhat file
使用样例:
防止影响服务,所以在本地windows下使用,先导出dump文件
./jmap -dump:format=b,file=my.bin 568
Dumping heap to my.bin ...
Heap dump file created
然后分析:
./jhat my.bin
Reading from my.bin...
Dump file created Thu Feb 18 16:20:49 CST 2016
Snapshot read, resolving...
Resolving 2532914 objects..
Chasing references, expect 1 dots.
Eliminating duplicate references.
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.
载在浏览器中输入localhost:7000查看结果,如下图
6.jstack
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使用方式只支持以下的这种方式:
如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程序何处发生问题。另外,jstack工具还可以附属到正在运行的java程序中,看到当时运行的java程序的java stack和native stack的信息, 如果现在运行的java程序呈现hung的状态,jstack是非常有用的。
命令格式 :
jstack [ option ] pid
参数说明:
-F当’jstack [-l] pid’没有相应的时候强制打印栈信息
-l长列表. 打印关于锁的附加信息,例如属于java.util.concurrent的ownable synchronizers列表.
-m打印java和native c/c++框架的所有栈信息.
使用样例:
[root@tts217 ~]# jstack -l 21742
root@develop117 bin]# printf "%x\n" 21742
238
[root@develop117 bin]# ./jstack 21742|grep 238
- waiting on <0x00000000e085f238> (a java.lang.ref.ReferenceQueue$Lock)
- locked <0x00000000e085f238> (a java.lang.ref.ReferenceQueue$Lock)
[root@develop117 bin]# printf "%x\n" 568
jstack主要用来查看某个Java进程内的线程堆栈信息。语法格式如下:
jstack [option] pid
jstack [option] executable core
jstack [option] [server-id@]remote-hostname-or-ip
命令行参数选项说明如下:
-l long listings,会打印出额外的锁信息,在发生死锁时可以用jstack -l pid来观察锁持有情况
-m mixed mode,不仅会输出Java堆栈信息,还会输出C/C++堆栈信息(比如Native方法)
jstack可以定位到线程堆栈,根据堆栈信息我们可以定位到具体代码,所以它在JVM性能调优中使用得非常多。下面我们来一个实例找出某个Java进程中最耗费CPU的Java线程并定位堆栈信息,用到的命令有ps、top、printf、jstack、grep。
第一步先找出Java进程ID,我部署在服务器上的Java应用名称为my:
得到进程ID为21711,第二步找出该进程内最耗费CPU的线程,可以使用ps -Lfp pid或者ps -mp pid -o THREAD, tid, time或者top -Hp pid,我这里用第三个,输出如下
TIME列就是各个Java线程耗费的CPU时间,CPU时间最长的是线程ID为21742的线程,用
printf "%x\n" 21742
得到21742的十六进制值为54ee,下面会用到。
OK,下一步终于轮到jstack上场了,它用来输出进程21711的堆栈信息,然后根据线程ID的十六进制值grep,如下:
./jstack 21711 | grep 54ee
"PollIntervalRetrySchedulerThread" prio=10 tid=0x00007f950043e000 nid=0x54ee in Object.wait()
死锁代码如下:
// 线程处于无线等待中
getLog().info("Thread [" + getName() + "] is idle waiting...");
schedulerThreadState = PollTaskSchedulerThreadState.IdleWaiting;
long now = System.currentTimeMillis();
long waitTime = now + getIdleWaitTime();
long timeUntilContinue = waitTime - now;
synchronized(sigLock) {
try {
if(!halted.get()) {
sigLock.wait(timeUntilContinue);
}
}
catch (InterruptedException ignore) {
}
}
7.hprof(Heap/CPU Profiling Tool)
hprof能够展现CPU使用率,统计堆内存使用情况,语法格式如下:
java -agentlib:hprof[=options] ToBeProfiledClass
java -Xrunprof[:options] ToBeProfiledClass
javac -J-agentlib:hprof[=options] ToBeProfiledClass
完整的命令选项如下:
Option Name and Value Description Default
--------------------- ----------- -------
heap=dump|sites|all heap profiling all
cpu=samples|times|old CPU usage off
monitor=y|n monitor contention n
format=a|b text(txt) or binary output a
file= write data to file java.hprof[.txt]
net=: send data over a socket off
depth= stack trace depth 4
interval= sample interval in ms 10
cutoff= output cutoff point 0.0001
lineno=y|n line number in traces? y
thread=y|n thread in traces? n
doe=y|n dump on exit? y
msa=y|n Solaris micro state accounting n
force=y|n force output to y
verbose=y|n print messages about dumps y
来几个官方指南上的实例。
CPU Usage Sampling Profiling(cpu=samples)的例子:
java -agentlib:hprof=cpu=samples,interval=20,depth=3 Hello
上面每隔20毫秒采样CPU消耗信息,堆栈深度为3,生成的profile文件名称是java.hprof.txt,在当前目录。
CPU Usage Times Profiling(cpu=times)的例子,它相对于CPU Usage Sampling Profile能够获得更加细粒度的CPU消耗信息,能够细到每个方法调用的开始和结束,它的实现使用了字节码注入技术(BCI):
javac -J-agentlib:hprof=cpu=times Hello.java
Heap Allocation Profiling(heap=sites)的例子:
javac -J-agentlib:hprof=heap=sites Hello.java
Heap Dump(heap=dump)的例子,它比上面的Heap Allocation Profiling能生成更详细的Heap Dump信息:
javac -J-agentlib:hprof=heap=dump Hello.java
虽然在JVM启动参数中加入-Xrunprof:heap=sites参数可以生成CPU/Heap Profile文件,但对JVM性能影响非常大,不建议在线上服务器环境使用。
本文思想来之与网上博客,经过写者总结