JVM工具介绍与使用
✔jps命令
介绍:查看所有具有权限的java进程的具体状态:包括进程ID,进程启动路径,以及进程启动参数。
使用格式:
1 | jps [options] [hostid] |
常用参数:
1 | -l #输出应用程序主类的完整包名,或者是应用程序JAR文件的完整路径。 -v #输出传给JVM的参数。 |
使用:
1 | root@ip-10-21-151-113:~# jps 59117 WatchdogManager 59171 Resin 15374 Jps |
1 | root@ip-10-21-151-113:~# jps -v 59117 WatchdogManager -Dresin.watchdog=a -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djava.awt.headless=true -Dresin.home=/usr/local/resin/ -Dresin.root=/usr/local/resin -Xrs -Xss256k -Xmx32m 15401 Jps -Dapplication.home=/opt/jdk1.7.0_45 -Xms8m 59171 Resin -Xss1m -XX:MaxGCPauseMillis=200 -Dnetworkaddress.cache.ttl=600 -Xms1001m -Xmx1001m -Dresin.server=1 -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djava.system.class.loader=com.caucho.loader.SystemClassLoader -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djava.awt.headless=true -Dresin.home=/usr/local/resin/ -Dresin.watchdog=a -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djava.awt.headless=true -Dresin.home=/usr/local/resin/ -Dresin.root=/usr/local/resin -Dresin.watchdog=a -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djava.awt.headless=true -Dresin.home=/usr/local/resin/ -Dresin.root=/usr/local/resin |
1 | root@ip-10-21-151-113:~# jps -l 59117 com.caucho.boot.WatchdogManager 59171 com.caucho.server.resin.Resin 15486 sun.tools.jps.Jps |
✔jstat命令
介绍:对堆的使用情况进行实时的命令行统计。
- 类加载及卸载情况
- 新生代,老年代及永久代的容量及使用情况
- 新生代,老年代及永久代垃圾回收情况,垃圾回收次数和垃圾回收时间
- 新生代中Eden区及Survior区中容量及分配情况等
使用格式
1 | jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]] |
常用的option
1 | root@ip-10-21-151-113:~# jstat -options -class -compiler -gc -gccapacity -gccause -gcnew -gcnewcapacity -gcold -gcoldcapacity -gcpermcapacity -gcutil -printcompilation |
使用:
1 | root@ip-10-21-151-113:~# jstat -gcutil 59171 1000 5 S0 S1 E O P YGC YGCT FGC FGCT GCT 57.29 0.00 63.16 79.74 81.34 1760 151.129 5 3.705 154.834 57.29 0.00 63.16 79.74 81.34 1760 151.129 5 3.705 154.834 57.29 0.00 63.16 79.74 81.34 1760 151.129 5 3.705 154.834 57.29 0.00 63.16 79.74 81.34 1760 151.129 5 3.705 154.834 57.29 0.00 64.39 79.74 81.34 1760 151.129 5 3.705 154.834 |
1 | root@ip-10-21-151-113:~# jstat -gc 59171 1000 5 S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT 3072.0 3072.0 1760.1 0.0 335360.0 251031.8 683520.0 545011.9 83968.0 68301.5 1760 151.129 5 3.705 154.834 3072.0 3072.0 1760.1 0.0 335360.0 251153.7 683520.0 545011.9 83968.0 68301.5 1760 151.129 5 3.705 154.834 3072.0 3072.0 1760.1 0.0 335360.0 251153.7 683520.0 545011.9 83968.0 68301.5 1760 151.129 5 3.705 154.834 3072.0 3072.0 1760.1 0.0 335360.0 251153.7 683520.0 545011.9 83968.0 68301.5 1760 151.129 5 3.705 154.834 3072.0 3072.0 1760.1 0.0 335360.0 251153.7 683520.0 545011.9 83968.0 68301.5 1760 151.129 5 3.705 154.834 |
✔jmap命令
介绍:打印进程内存中的所有对象(对象类名,数量,大小)。
用法:
1 | jmap [option] <pid> |
常用参数:
1 | -heap to print java heap summary -histo[:live] to print histogram of java object heap; if the "live" suboption is specified, only count live objects -permstat to print permanent generation statistics -finalizerinfo to print information on objects awaiting finalization -dump:<dump-options> to dump java heap in hprof binary format dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> -F force. Use with -dump:<dump-options> <pid> or -histo to force a heap dump or histogram when <pid> does not respond. The "live" suboption is not supported in this mode. |
示例:
打印所以对象的信息
1 | root@ip-10-21-151-113:~# jmap -F -histo 59117 Attaching to process ID 59117, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.45-b08 Iterating over heap. This may take a while... Object Histogram: num #instances #bytes Class description -------------------------------------------------------------------------- 1: 9272 7797920 byte[] 2: 33781 4515096 * ConstMethodKlass 3: 33781 4333168 * MethodKlass 4: 2751 3170088 * ConstantPoolKlass 5: 2751 1971192 * InstanceKlassKlass 6: 2046 1726176 * ConstantPoolCacheKlass 7: 21264 1535528 char[] 8: 41 1349264 com.caucho.util.LruCache$CacheItem[] 9: 46838 749408 java.lang.Object 10: 1371 572952 int[] 11: 5682 473872 java.lang.Object[] 12: 19461 467064 java.lang.String 13: 2948 352512 java.lang.Class 14: 3478 278240 java.lang.reflect.Method 15: 613 254928 * MethodDataKlass 16: 4383 247776 * System ObjArray 17: 3670 223024 short[] 18: 6732 215424 java.util.HashMap$Entry 19: 5151 164832 java.util.concurrent.ConcurrentHashMap$HashEntry 20: 1238 146688 java.util.HashMap$Entry[] 21: 3479 139160 java.util.LinkedHashMap$Entry 22: 5504 132096 java.util.ArrayList |
将内存dump出来(然后可以用MAT图形化工具来分析)
1 | root@ip-10-21-151-15:~# jmap -dump:format=b,file=test.bin 51419 Dumping heap to /root/test.bin ... Heap dump file created root@ip-10-21-151-15:~# ll test.bin -rw------- 1 root root 25598311 Aug 2 16:15 test.bin |
✔jstack打印java进程的堆栈信息
介绍:打印java进程的堆栈信息
使用:
1 | jstack -F [-m] [-l] <pid> |
相关参数
1 | -F to force a thread dump. Use when jstack <pid> does not respond (process is hung) -m to print both java and native frames (mixed mode) -l long listing. Prints additional information about locks |
示例:
1 | root@ip-10-21-151-15:~# jstack -F 51419 Attaching to process ID 51419, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.45-b08 Deadlock Detection: No deadlocks found. Thread 30624: (state = BLOCKED) - sun.misc.Unsafe.park(boolean, long) @bci=0 (Compiled frame; information may be imprecise) - java.util.concurrent.locks.LockSupport.park() @bci=5, line=315 (Compiled frame) - com.caucho.util.ThreadPool$PoolThread.nextTask() @bci=254, line=962 (Compiled frame) - com.caucho.util.ThreadPool$PoolThread.runTasks() @bci=26, line=887 (Compiled frame) - com.caucho.util.ThreadPool$PoolThread.run() @bci=69, line=866 (Interpreted frame) Thread 56062: (state = BLOCKED) - sun.misc.Unsafe.park(boolean, long) @bci=0 (Compiled frame; information may be imprecise) - java.util.concurrent.locks.LockSupport.park() @bci=5, line=315 (Compiled frame) - com.caucho.util.ThreadPool$PoolThread.nextTask() @bci=254, line=962 (Compiled frame) - com.caucho.util.ThreadPool$PoolThread.runTasks() @bci=26, line=887 (Compiled frame) - com.caucho.util.ThreadPool$PoolThread.run() @bci=69, line=866 (Interpreted frame) |
✔图形化工具MAT(memory analyze tool)
基本用法:
- 先用jmap命令将内存dump出到一个文件中
- 然后打开MAT,在MAT中打开dump文件
- 然后进行相关分析
✔总结
jvm故障排查基本就用到这些工具,因为我们的服务器都是linux,都是命令行终端的,所以一般不会使用到windows下面的jdk自带的图形化工具。
熟练使用这些工具,可以使我们在故障排查迅速地找到问题的所在。