写在前面
本文已起看下给我们的Java程序看病常用到的工具都有哪些,通过工具得到指标,通过指标分析病情
,从而给出对应的治疗方案
。
1:jps/jinfo
用来获取运行的jvm进程信息,一般我们使用该命令来获取进程号,进而使用其来获取其他信息,jps如下:
C:\Users\Administrator>jps
21540 BB
21272 Jps
C:\Users\Administrator>jps -mlv
21540 com.example.BB
22012 sun.tools.jps.Jps -mlv -Denv.class.path=.;D:\program_files\jdk1.8.0_202\lib\dt.jar;D:\program_files\jdk1.8.0_202\lib\tools.jar; -Dapplication.home=D:\program_files\jdk1.8.0_202 -Xms8m
jinfo可用于获取指定进程的参数信息,系统环境变量信息等,如下:
C:\WINDOWS\system32>jinfo -flags 21540
Attaching to process ID 21540, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.202-b08
Non-default VM flags: -XX:CICompilerCount=4 -XX:InitialHeapSize=266338304 -XX:MaxHeapSize=4242538496 -XX:MaxNewSize=1414004736 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=88604672 -XX:OldSize=177733632 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
Command line:
2:jstat
该工具用于获取GC相关的信息,如GC的次数,时间,各个内存区域的大小等,主要有如下两种方式。
2.1:查看GC的内存百分比信息
如下:
C:\WINDOWS\system32>jstat -gcutil -t 22900 1000 1000
Timestamp S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
36.0 0.00 99.87 76.34 30.32 74.48 79.26 1 0.014 0 0.000 0.014
37.0 0.00 99.87 81.15 30.32 74.48 79.26 1 0.014 0 0.000 0.014
38.0 0.00 99.87 85.96 30.32 74.48 79.26 1 0.014 0 0.000 0.014
39.0 0.00 99.87 90.76 30.32 74.48 79.26 1 0.014 0 0.000 0.014
40.0 0.00 99.87 97.30 30.32 74.48 79.26 1 0.014 0 0.000 0.014
注意-t的位置必须再例子中的位置,其他位置都不行
每列的含义如下:
Timestamp:当前程序运行的秒数
S0:survivor0的占用百分比
S1:survivor1的占用百分比
E:Eden的占用百分比
O:Old的占用百分比
M:Meta的占用百分比
CCS:
YGC:young gc的总次数
YGCT:young gc的总时间
FGC:full gc的总次数
FGCT:full gc的总时间
GCT:所有GC的总时间
2.2:查看GC的内存大小信息
如下:
C:\WINDOWS\system32>jstat -gc -t 22900 1000 1000
Timestamp S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
808.0 15360.0 15872.0 15336.9 0.0 10752.0 5682.9 2525184.0 2486393.6 4864.0 3623.0 512.0 405.8 170 1.353 39 1.032 2.385
809.0 15360.0 15872.0 15336.9 0.0 10752.0 8857.8 2525184.0 2486393.6 4864.0 3623.0 512.0 405.8 170 1.353 39 1.032 2.385
810.0 16896.0 15872.0 0.0 15825.4 10752.0 1190.1 2525184.0 2496602.0 4864.0 3623.0 512.0 405.8 171 1.362 39 1.032 2.394
811.1 16896.0 15872.0 0.0 15825.4 10752.0 4511.5 2525184.0 2496602.0 4864.0 3623.0 512.0 405.8 171 1.362 39 1.032 2.394
注意-t的位置必须再例子中的位置,其他位置都不行
每列的含义如下:
Timestamp:当前程序运行的秒数
S0C:survivor0的总字节数
S1C:survivor1的总字节数
S0U:survivor0的已使用的字节数,S0U/S0C即为S0使用的百分比
S1U: survivor0的已使用的字节数,S0U/S0C即为S0使用的百分比
EC: Eden总大小
EU: Eden使用的大小,EU/EC就是Eden的使用百分比
OC:Old总大小
OU:Old使用大小
MC:Meta总大小
MU:Meta使用大小
CCSC:
CCSU:
YGC:young gc的总次数
YGCT:young gc的总时间
FGC:full gc的总次数
FGCT:full gc的总时间
GCT:所有gc 的总时间
3:jmap
该命令用来获取jvm运行的快照信息,主要包括堆内存快照,堆内存对象实例信息快照,堆转储快照。
- 获取堆内存快照
C:\WINDOWS\system32>jmap -heap 21448
Attaching to process ID 21448, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.202-b08
using thread-local object allocation.
Parallel GC with 10 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
...
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 66584576 (63.5MB)
used = 53232656 (50.76661682128906MB)
free = 13351920 (12.733383178710938MB)
79.94742806502215% used
From Space:
capacity = 11010048 (10.5MB)
used = 11009984 (10.49993896484375MB)
free = 64 (6.103515625E-5MB)
99.99941871279762% used
To Space:
capacity = 11010048 (10.5MB)
used = 0 (0.0MB)
free = 11010048 (10.5MB)
0.0% used
PS Old Generation
capacity = 177733632 (169.5MB)
used = 53833600 (51.3397216796875MB)
free = 123900032 (118.1602783203125MB)
30.288921344948378% used
2777 interned Strings occupying 229112 bytes.
展示了配置信息,以及当前运行状态下的各个主要内存区域的占用情况。
- 获取堆内存对象实例快照
用的多
C:\WINDOWS\system32>jmap -histo 21448
num #instances #bytes class name
----------------------------------------------
1: 15568 777974120 [B
2: 3823 369768 [C
3: 476 140704 [I
4: 613 125176 [Ljava.lang.Object;
...
可以看到本例中byte[]
的对象实例数最多,有15568个,占用堆内存大小将近800m。
- 获取堆转储快照
C:\WINDOWS\system32>jmap -dump:format=b,file=d:\\test\\xxxx1.hprof 22180
Dumping heap to D:\test\xxxx1.hprof ...
Heap dump file created
获取文件后我们就可以使用分析工具,如在线分析工具heapdump 来分析和定位问题,如下也可以看到byte[]
的对象实例数最多:
4:jstack
查看线程信息,一般在定位CPU高 之类的问题时会使用到,如:
C:\WINDOWS\system32>jstack -l 6908
2023-05-04 18:07:46
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.202-b08 mixed mode):
"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001dc18000 nid=0x2014 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
...
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001dc05800 nid=0x1888 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
...
"main" #1 prio=5 os_prio=0 tid=0x000000000286d800 nid=0x5108 waiting on condition [0x000000000276f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.example.BB.main(BB.java:15)
Locked ownable synchronizers:
- None
"VM Thread" os_prio=2 tid=0x000000001db72800 nid=0x4464 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002887800 nid=0x5ac0 runnable
...
"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000000002897000 nid=0x53b8 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000001dcab800 nid=0x5ba0 waiting on condition
另外,kill -3 PID 会强制进程打印自己的线程信息到控制台,即此时,如果时执行的话,会在应用程序本身的控制台看到线程的打印信息
5:jcmd
这是一个综合的命令,如果有该命令的话,可优先使用。
- 查看都支持哪些内容
C:\WINDOWS\system32>jcmd 6908 help
6908:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
VM.classloader_stats
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.uptime
VM.dynlibs
VM.flags
VM.system_properties
VM.command_line
VM.version
help
主要有虚拟机相关的,以VM
打头,垃圾回收相关的,以GC
打头。
- VM相关
查看启动时长:
C:\WINDOWS\system32>jcmd 6908 VM.uptime
6908:
504.189 s
查看JVM的版本号:
C:\WINDOWS\system32>jcmd 6908 VM.version
6908:
Java HotSpot(TM) 64-Bit Server VM version 25.202-b08
JDK 8.0_202
查看JVM的系统属性:
C:\WINDOWS\system32>jcmd 6908 VM.system_properties
6908:
#Thu May 04 18:18:19 CST 2023
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=D\:\\program_files\\jdk1.8.0_202\\jre\\bin
java.vm.version=25.202-b08
java.vm.vendor=Oracle Corporation
java.vendor.url=http\://java.oracle.com/
path.separator=;
...
- GC相关
查看类直方图:
C:\WINDOWS\system32>jcmd 16032 GC.class_histogram
16032:
num #instances #bytes class name
----------------------------------------------
1: 10617 510987592 [B
2: 5734 457744 [C
3: 5601 134424 java.lang.String
4: 630 103072 [Ljava.lang.Object;
...
和jmap -histo PID一样的结果
查看堆内存使用情况:
C:\WINDOWS\system32>jcmd 16032 GC.heap_info
16032:
PSYoungGen total 125440K, used 35554K [0x000000076bb80000, 0x000000077a380000, 0x00000007c0000000)
eden space 19456K, 83% used [0x000000076bb80000,0x000000076cb48f88,0x000000076ce80000)
from space 105984K, 18% used [0x0000000773c00000,0x0000000774eefbe0,0x000000077a380000)
to space 109056K, 0% used [0x000000076ce80000,0x000000076ce80000,0x0000000773900000)
ParOldGen total 761344K, used 693833K [0x00000006c3200000, 0x00000006f1980000, 0x000000076bb80000)
object space 761344K, 91% used [0x00000006c3200000,0x00000006ed7924a8,0x00000006f1980000)
Metaspace used 3859K, capacity 4632K, committed 4864K, reserved 1056768K
class space used 427K, capacity 460K, committed 512K, reserved 1048576K
- 其他
查看线程信息:
C:\WINDOWS\system32>jcmd 16032 Thread.print
16032:
2023-05-04 18:23:58
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.202-b08 mixed mode):
"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001df6c000 nid=0x5e8c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #9 daemon prio=9 os_prio=2 tid=0x000000001dee8800 nid=0x6090 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
和jstack -l PID结果是一样的
6:junscript/jjs
jrunscript直接执行js代码:
C:\WINDOWS\system32>jrunscript -e "cat('http://www.baidu.com')"
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度?下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度?? class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必?</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP?030173? <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
同curl。
jjs以交互式的方式执行js代码,如:
C:\WINDOWS\system32>jjs
jjs> var a=9;
jjs> var b=8
jjs> b
8
jjs> var c=a+b
jjs> c