java包中提供了很多监控JVM的工具类,作为java程序员必须得掌握常用的几个工具,下面是几个常用的JVM性能监控与故障处理工具的介绍与使用。
1、jstack
该命令用于生成当前时刻虚拟机的线程快照,可以用于定位线程出现长时间停顿的原因,包括死锁、死循环、超时等待等。
jstack命令格式:
jstack [option] vmid
vmid可以通过ps -ef|grep java命令得到,第二个参数的PID值就是vmid。
option的可选值:
-F 当正常输出的请求不被响应时,强制输出线程堆栈
-l 除堆栈外,显示关于锁的附加信息
-m 如果调用到本地方法的话,可显示C/C++的堆栈
查询示例:
1.1 jstack -l 18614
控制台输出的部分结果:
Full thread dump Java HotSpot(TM) Server VM (25.151-b12 mixed mode): "Attach Listener" # daemon prio= os_prio= tid=0xa58bd000 nid=0x6427 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:
- None "Abandoned connection cleanup thread" # daemon prio= os_prio= tid=0x09bc8800 nid=0x4932 in Object.wait() [0xa558c000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:)
- locked <0xad5b5730> (a java.lang.ref.ReferenceQueue$Lock)
at com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:) Locked ownable synchronizers:
- None "Tomcat JDBC Pool Cleaner[24630847:1546674815832]" # daemon prio= os_prio= tid=0x096f9000 nid=0x4931 in Object.wait() [0xa55dd000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.util.TimerThread.mainLoop(Timer.java:)
- locked <0xad2b10d0> (a java.util.TaskQueue)
at java.util.TimerThread.run(Timer.java:) Locked ownable synchronizers:
- None "ajp-nio-8009-AsyncTimeout" # daemon prio= os_prio= tid=0xa7eae000 nid=0x492f waiting on condition [0xa5cb8000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:)
at java.lang.Thread.run(Thread.java:) Locked ownable synchronizers:
- None "ajp-nio-8009-Acceptor-0" # daemon prio= os_prio= tid=0xa7eacc00 nid=0x492e runnable [0xa5d09000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:)
- locked <0xac53a438> (a java.lang.Object)
at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:)
at java.lang.Thread.run(Thread.java:) Locked ownable synchronizers:
- None
1.2 jstack -m 18614
下面的部分结果中的红色字说明没有死锁发生。
Attaching to process ID , please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Deadlock Detection: No deadlocks found. ----------------- -----------------
0xf7735430 ????????
0xa87b2938 NET_Timeout + 0x38
0xa87aecbe Java_java_net_PlainSocketImpl_socketAccept + 0x21e
0xe74fb646 * java.net.PlainSocketImpl.socketAccept(java.net.SocketImpl) bci: (Interpreted frame)
0xe74f1d4a * java.net.AbstractPlainSocketImpl.accept(java.net.SocketImpl) bci: line: (Interpreted frame)
0xe74f1d4a * java.net.ServerSocket.implAccept(java.net.Socket) bci: line: (Interpreted frame)
0xe74f1d4a * java.net.ServerSocket.accept() bci: line: (Interpreted frame)
0xe74f1b54 * org.apache.catalina.core.StandardServer.await() bci: line: (Interpreted frame)
0xe74f1d7f * org.apache.catalina.startup.Catalina.await() bci: line: (Interpreted frame)
0xe74f1d4a * org.apache.catalina.startup.Catalina.start() bci: line: (Interpreted frame)
0xe74ed499 <StubRoutines>
0xf6becb3f _ZN9JavaCalls11call_helperEP9JavaValueP12methodHandleP17JavaCallArgumentsP6Thread + 0x101f
0xf6e40489 _ZN2os20os_exception_wrapperEPFvP9JavaValueP12methodHandleP17JavaCallArgumentsP6ThreadES1_S3_S5_S7_ + 0x19
0xf6beb10f _ZN9JavaCalls4callEP9JavaValue12methodHandleP17JavaCallArgumentsP6Thread + 0x2f
0xf6eb2812 _ZN10Reflection6invokeE19instanceKlassHandle12methodHandle6Handleb14objArrayHandle9BasicTypeS3_bP6Thread + 0x4d2
0xf6eb5ba8 _ZN10Reflection13invoke_methodEP7oopDesc6Handle14objArrayHandleP6Thread + 0x148
0xf6c7f4ef JVM_InvokeMethod + 0x25f
0xf6629552 Java_sun_reflect_NativeMethodAccessorImpl_invoke0 + 0x32
0xe74fb646 * sun.reflect.NativeMethodAccessorImpl.invoke0(java.lang.reflect.Method, java.lang.Object, java.lang.Object[]) bci: (Interpreted frame)
0xe74f1b54 * sun.reflect.NativeMethodAccessorImpl.invoke(java.lang.Object, java.lang.Object[]) bci: line: (Interpreted frame)
0xe7639144 * sun.reflect.DelegatingMethodAccessorImpl.invoke(java.lang.Object, java.lang.Object[]) bci: line: (Compiled frame)
* java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) bci: line: (Compiled frame)
* org.apache.catalina.startup.Bootstrap.start() bci: line: (Interpreted frame)
0xe74f1d4a * org.apache.catalina.startup.Bootstrap.main(java.lang.String[]) bci: line: (Interpreted frame)
0xe74ed499 <StubRoutines>
0xf6becb3f _ZN9JavaCalls11call_helperEP9JavaValueP12methodHandleP17JavaCallArgumentsP6Thread + 0x101f
0xf6e40489 _ZN2os20os_exception_wrapperEPFvP9JavaValueP12methodHandleP17JavaCallArgumentsP6ThreadES1_S3_S5_S7_ + 0x19
0xf6beb10f _ZN9JavaCalls4callEP9JavaValue12methodHandleP17JavaCallArgumentsP6Thread + 0x2f
0xf6c35187 _ZL17jni_invoke_staticP7JNIEnv_P9JavaValueP8_jobject11JNICallTypeP10_jmethodIDP18JNI_ArgumentPusherP6Thread + 0x387
0xf6c47c0f jni_CallStaticVoidMethod + 0xdf
0xf76de8a9 JavaMain + 0x8e9
0xf76f3b2c start_thread + 0xcc
----------------- -----------------
0xf7735430 ????????
----------------- -----------------
0xf7735430 ????????
----------------- -----------------
0xf7735430 ????????
----------------- -----------------
0xf7735430 ????????
----------------- -----------------
0xf7735430 ????????
----------------- -----------------
0xf7735430 ????????
sun.jvm.hotspot.debugger.UnmappedAddressException: f0
at sun.jvm.hotspot.debugger.PageCache.checkPage(PageCache.java:)
at sun.jvm.hotspot.debugger.PageCache.getData(PageCache.java:)
at sun.jvm.hotspot.debugger.DebuggerBase.readBytes(DebuggerBase.java:)
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.readCInteger(LinuxDebuggerLocal.java:)
at sun.jvm.hotspot.debugger.DebuggerBase.readAddressValue(DebuggerBase.java:)
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.readAddress(LinuxDebuggerLocal.java:)
at sun.jvm.hotspot.debugger.linux.LinuxAddress.getAddressAt(LinuxAddress.java:)
at sun.jvm.hotspot.debugger.linux.x86.LinuxX86CFrame.sender(LinuxX86CFrame.java:)
at sun.jvm.hotspot.tools.PStack.run(PStack.java:)
at sun.jvm.hotspot.tools.PStack.run(PStack.java:)
at sun.jvm.hotspot.tools.PStack.run(PStack.java:)
at sun.jvm.hotspot.tools.JStack.run(JStack.java:)
at sun.jvm.hotspot.tools.Tool.startInternal(Tool.java:)
at sun.jvm.hotspot.tools.Tool.start(Tool.java:)
at sun.jvm.hotspot.tools.Tool.execute(Tool.java:)
at sun.jvm.hotspot.tools.JStack.main(JStack.java:)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:)
at java.lang.reflect.Method.invoke(Method.java:)
at sun.tools.jstack.JStack.runJStackTool(JStack.java:)
at sun.tools.jstack.JStack.main(JStack.java:)
----------------- 18622 -----------------
0xf7735430 ????????
----------------- 18623 -----------------
0xf7735430 ????????
----------------- 18624 -----------------
0xf7735430 ????????
----------------- 18625 -----------------
0xf7735430 ????????
----------------- 18631 -----------------
0xf7735430 ????????
----------------- 18649 -----------------
0xf7735430 ????????
----------------- 18650 -----------------
0xf7735430 ????????
0xe7d9e4cb <Unknown compiled code>
0xe806cbfc * sun.nio.ch.EPollArrayWrapper.poll(long) bci:18 line:269 (Compiled frame)
* sun.nio.ch.EPollSelectorImpl.doSelect(long) bci:28 line:93 (Compiled frame)
* sun.nio.ch.SelectorImpl.lockAndDoSelect(long) bci:37 line:86 (Compiled frame)
* sun.nio.ch.SelectorImpl.select(long) bci:30 line:97 (Compiled frame)
----------------- 18651 -----------------
0xf7735430 ????????
0xe7d9e4cb <Unknown compiled code>
0xe806cbfc * sun.nio.ch.EPollArrayWrapper.poll(long) bci:18 line:269 (Compiled frame)
* sun.nio.ch.EPollSelectorImpl.doSelect(long) bci:28 line:93 (Compiled frame)
* sun.nio.ch.SelectorImpl.lockAndDoSelect(long) bci:37 line:86 (Compiled frame)
* sun.nio.ch.SelectorImpl.select(long) bci:30 line:97 (Compiled frame)
2、jstat
该命令用于监视虚拟机各种运行状态信息,包括类装载、内存、垃圾回收、JIT编译等数据,是运行期定位JVM性能问题的首选工具。
jstat命令格式:
jstat [option vmid [interval[s|ms] [count]] ]
interval:查询间隔(ms单位),count:查询次数,若忽略这2个参数,则只查询一次。
option为用户需要查询的虚拟机信息,包括类装载、GC、运行期编译状态等。可选项如下:
-class 监视类装载、卸载数量、总空间及类装载所耗费的时间
-gc 监视java堆状况,包括Eden区、两个survivor区、老年代、永久代等的容量、已用空间、GC时间合计等信息
-gccapacity 监视内容与-gc基本相同,但输出的主要关注java堆各个区域使用到的最大、最小空间
-gcutil 监视内容与-gc基本相同,但输出的主要关注已使用空间占总空间的百分比
-gccause 与-gcutil功能一样,但是会额外输出上一次GC产生的原因
-gcnew 监视新生代GC状况
-gcnewcapacity 监视内容与-gcnew基本相同,输出主要关注使用到的最大、最小空间
-gcold 监视老年代GC状况
-gcoldcapacity 监视内容与-gcold基本相同,输出主要关注使用到的最大、最小空间
-gcpermcapacity 输出永久代使用到的最大、最小空间
-compiler 输出JIT编译器编译过的方法、耗时等信息
-printcompilation 输出已被JIT编译的方法
示例:
2.1 jstat -gc 18614 200 10 每200毫秒查询一次进程PID为18614的垃圾收集情况,一共查询10次。
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
33792.0 42240.0 806.7 0.0 171264.0 162460.8 61440.0 30525.4 25240.0 24905.6 0.0 0.0 0.338 0.239 0.576
查询结果的标题列含义:
S0C:当前Survivor0区空间大小(kb);S1C:当前Survivor1区空间大小(kb)
S0U:当前Survivor0区空间使用量(kb);S1U:当前Survivor1区空间使用量(kb)
EC:当前Eden区空间大小(kb);EU:当前Eden区空间使用量(kb)
OC:当前Old区空间大小(kb);OU:当前Old区空间使用量(kb)
MC:方法区空间大小(kb);MU:方法区空间使用量(kb)
CCSC:压缩类空间大小(kb);CCSU:压缩类空间空间使用量(kb)
YGC:Young GC次数;YGCT:Young GC耗时
FGC:Full GC次数;FGCT:Full GC耗时
GCT:GC总耗时
2.2 jstat -gcutil 18614
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
2.39 0.00 100.00 49.68 98.68 - 0.338 0.239 0.576
说明:Survivor0区使用了2.39%的空间,Survivor1区使用了0.00%的空间,Eden区使用了100%的空间,Old区使用了49.68%的空间,方法区使用了98.68的空间。程序运行以来,Young GC发生了24次,耗时0.338秒,Full GC发生了3次,耗时0.239秒,所有GC总耗时0.576秒。
2.2 jstat -class 18614
Loaded Bytes Unloaded Bytes Time
6350.0 0.0 7.93
说明:程序装载了5554个类,共6350字节,卸载了0个类,共0个字节,类装载耗时7.93秒。
2.3 jstat -gcnew 18614
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
41216.0 1024.0 0.0 798.2 41216.0 164096.0 5723.0 0.342
2.4 jstat -gcold 18614
MC MU CCSC CCSU OC OU YGC FGC FGCT GCT
25368.0 24912.1 0.0 0.0 61440.0 30525.4 0.239 0.580
2.5 jstat -compiler 18614
Compiled Failed Invalid Time FailedType FailedMethod
21.29 com/mysql/jdbc/AbandonedConnectionCleanupThread run
说明:JIT编译器编译成功方法7018个,失败1个,无效0个,耗时21.29秒,失败类型为1,失败的方法为AbandonedConnectionCleanupThread类的run方法。
参考:《深入理解Java虚拟机》---周志明 著