内容大纲:
涵盖java以下常用工具的概念和用法,及其常用案例
jps,jstat,jinfo,jmap,jhat,jstack
使用频率最高的JDK命令工具:可以列出正在运行的JVM进程,并可以显示出JVM执行主类和进程号(LVMID:本地虚拟机唯一ID)
命令 | 作用 |
---|---|
jps | 显示进程号和主类名 |
jps -l | 显示进程号和主类全名 |
jps -m | 输出执行主类main()函数的参数 |
jps -v | 输出启动时JVM参数 |
此处较为简单,不做详解,可自行实验
官方链接
功能:实时查看和调整JVM各项参数
jps -v可以查看显式指定的参数,但要知道未被指定的参数默认值,需要用jinfo
命令 | 作用 |
---|---|
-flag name | 输出指定的参数 |
-flag [+|-] name | 开-关指定参数 |
-flag name=value | 输出执行主类main()函数的参数 |
-flags | 输出给JVM默认和命令行传递的参数 |
-sysprops | 输出Java系统配置 |
首先指定java程序,并设置参数
java -Xms10M -Xmx100M jvm.DeadLock
> jinfo -flag MaxHeapSize 13301
-XX:MaxHeapSize=4294967296
> jinfo -flag InitialHeapSize 13301
-XX:InitialHeapSize=268435456
> jinfo -flags 13757
Attaching to process ID 13757, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Non-default VM flags: -XX:CICompilerCount=4 -XX:InitialHeapSize=10485760 -XX:MaxHeapSize=104857600 -XX:MaxNewSize=34603008 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=3145728 -XX:OldSize=7340032 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
Command line: -Xms10M -Xmx100M
某些机器不支持动态修改
> jinfo -flag -MaxHeapSize 13757
> jinfo -flag +MaxHeapSize 13757
某些机器不支持动态修改
> jinfo -flag MaxHeapSize=20971520 13757
官方链接
监视进程运行中的jvm物理内存的占用情况
命令 | 作用 |
---|---|
-dump:[live,]format=b,file= | 输出jvm的heap内容到文件,live可选,代表只输出活的对象到文件 |
-finalizerinfo | 打印正等候回收的对象的信息 |
-heap | 打印heap的概要信息,GC算法,heap的配置及内存 |
-histo | 打印每个class的实例数目,内存占用,类全名信息 |
-sysprops | 输出Java系统配置 |
dump文件可以用MAT等分析,关于如何分析,此处挖个坑,后续填补
jmap -dump:format=b,file=Downloads/dump.bin 13757
Dumping heap to /Users/xiaoran/Downloads/dump.bin ...
Heap dump file created
> jmap -finalizerinfo 13757
Attaching to process ID 13757, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Number of objects pending for finalization: 0 (等待回收对象0个)
> jmap -heap 13757
Attaching to process ID 13757, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
using thread-local object allocation.
Parallel GC with 8 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 104857600 (100.0MB)
NewSize = 3145728 (3.0MB)
MaxNewSize = 34603008 (33.0MB)
OldSize = 7340032 (7.0MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 2097152 (2.0MB)
used = 0 (0.0MB)
free = 2097152 (2.0MB)
0.0% used
From Space:
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
To Space:
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
PS Old Generation
capacity = 7340032 (7.0MB)
used = 450216 (0.42935943603515625MB)
free = 6889816 (6.570640563964844MB)
6.133706229073661% used
929 interned Strings occupying 65848 bytes.
> jmap -histo 13757
num #instances #bytes class name
----------------------------------------------
1: 1421 124632 [C
2: 701 80000 java.lang.Class
3: 810 36832 [Ljava.lang.Object;
4: 1408 33792 java.lang.String
5: 15 25912 [B
6: 185 10360 java.lang.invoke.MemberName
7: 273 8736 java.util.concurrent.ConcurrentHashMap$Node
dump文件分析工具,可用刚刚dump的文件
jhat /Users/xiaoran/Downloads/dump.bin
开启后,可通过http://127.0.0.1:7000/访问
官方链接
观察jvm中当前所有线程的运行情况和线程当前状态。程序崩溃或卡住是贼好用
命令 | 作用 |
---|---|
-F | 正常请求不被响应,强制输出线程堆栈 |
-l | 除堆栈外,锁的信息 |
-m | 如调用本地方法,可显示c/c++堆栈 |
写了一个死锁的程序作为调试程序
> jstack -F 13757
Attaching to process ID 13757, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Deadlock Detection:
Found one Java-level deadlock:
=============================
"Thread-0":
waiting to lock Monitor@0x00007f863f02aea8 (Object@0x00000007b9c08c00, a java/lang/Boolean),
which is held by "Thread-1"
"Thread-1":
waiting to lock Monitor@0x00007f863f0286c8 (Object@0x00000007b9c08580, a java/lang/Boolean),
which is held by "Thread-0"
Found a total of 1 deadlock.
Thread 18955: (state = BLOCKED)
Thread 3843: (state = BLOCKED)
Thread 43011: (state = BLOCKED)
- jvm.DeadLock$2.run() @bci=63, line=57 (Interpreted frame)
- java.lang.Thread.run() @bci=11, line=748 (Interpreted frame)
Thread 21763: (state = BLOCKED)
- jvm.DeadLock$1.run() @bci=63, line=36 (Interpreted frame)
- java.lang.Thread.run() @bci=11, line=748 (Interpreted frame)
Thread 18435: (state = BLOCKED)
Thread 19715: (state = BLOCKED)
- java.lang.Object.wait(long) @bci=0 (Interpreted frame)
- java.lang.ref.ReferenceQueue.remove(long) @bci=59, line=143 (Interpreted frame)
- java.lang.ref.ReferenceQueue.remove() @bci=2, line=164 (Interpreted frame)
- java.lang.ref.Finalizer$FinalizerThread.run() @bci=36, line=209 (Interpreted frame)
Thread 19971: (state = BLOCKED)
- java.lang.Object.wait(long) @bci=0 (Interpreted frame)
- java.lang.Object.wait() @bci=2, line=502 (Interpreted frame)
- java.lang.ref.Reference.tryHandlePending(boolean) @bci=54, line=191 (Interpreted frame)
- java.lang.ref.Reference$ReferenceHandler.run() @bci=1, line=153 (Interpreted frame)
> jstack -l 13757
//此处省略若干,关注锁的信息
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x00007f863f0286c8 (object 0x00000007b9c08580, a java.lang.Boolean),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x00007f863f02aea8 (object 0x00000007b9c08c00, a java.lang.Boolean),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at jvm.DeadLock$2.run(DeadLock.java:57)
- waiting to lock <0x00000007b9c08580> (a java.lang.Boolean)
- locked <0x00000007b9c08c00> (a java.lang.Boolean)
at java.lang.Thread.run(Thread.java:748)
"Thread-0":
at jvm.DeadLock$1.run(DeadLock.java:36)
- waiting to lock <0x00000007b9c08c00> (a java.lang.Boolean)
- locked <0x00000007b9c08580> (a java.lang.Boolean)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
官方链接
用于监控JVM各种运行状态信息,运行期间定位JVM性能问题的首选
命令格式:
jstat -<option> <vmid> [<interval> <count>]
jstat [-命令选项] [vmid] [间隔时间/毫秒] [查询次数]
当监控时间不传入时,只记录当前一次的情况
命令 | 作用 |
---|---|
-gc | 监视堆状况:Eden,S0,S1,老年代,永久代(元空间),压缩类空间,YoungGC,FullGC,GC消耗总时间 |
-gcutil | 与gc基本一致,关注点在百分比 |
-gccause | 与gcutil一致,额外输出上一次GC产生的原因 |
>jstat -class 2060
Loaded Bytes Unloaded Bytes Time
15756 17355.6 0 0.0 11.29
>jstat -compiler 9848
Compiled Failed Invalid Time FailedType FailedMethod
1135 0 0 0.96 0
>jstat -gc 2060
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
20480.0 20480.0 0.0 13115.3 163840.0 113334.2 614400.0 436045.7 63872.0 61266.5 0.0 0.0 149 3.440 8 0.295 3.735
>jstat -gccapacity 2060
NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
204800.0 204800.0 204800.0 20480.0 20480.0 163840.0 614400.0 614400.0 614400.0 614400.0 0.0 63872.0 63872.0 0.0 0.0 0.0 149 8
>jstat -gcnew 9849
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
10752.0 10752.0 0.0 0.0 15 15 0.0 65536.0 10521.4 0 0.000
>jstat -gcnewcapacity 9849
NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC
87040.0 1397760.0 87040.0 465920.0 10752.0 465920.0 10752.0 1396736.0 65536.0 0 0
>jstat -gcold 9849
MC MU CCSC CCSU OC OU YGC FGC FGCT GCT
4480.0 774.6 384.0 75.9 175104.0 0.0 0 0 0.000 0.000
>jstat -gcoldcapacity 9849
OGCMN OGCMX OGC OC YGC FGC FGCT GCT
175104.0 2796544.0 175104.0 175104.0 0 0 0.000 0.000
>jstat -gcmetacapacity 9849
MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC FGCT GCT
0.0 1056768.0 4480.0 0.0 1048576.0 384.0 0 0 0.000 0.000
>jstat -gcutil 9849
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
0.00 0.00 16.05 0.00 17.29 19.76 0 0.000 0 0.000 0.000
>jstat -gccause 9849
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT LGCC GCC
0.00 0.00 16.05 0.00 17.29 19.76 0 0.000 0 0.000 0.000 No GC No GC