jdk命令行工具

 

一 jstat

命令操作

-class  监视类装载,卸载数量,总空间及类装载所耗费时间。

-gc                    监视java堆状况,包括eden区,2个survivor区、老年代、永久代等的容量、已用空间、GC时间合计等信息

-gccapacity       监视内容与-gc基本相同,但输出主要关注java 堆各个区域使用到的最大和最小空间

-gcutil                监视内容与-gc基本相同,但输出主要关注已使用空间占总空间百分比

-gccause           与-gcutil功能一样,但是会额外输出导致上一次gc产生的原因

-gcnew               监视新生代gc的状况

-gcold                 监视年老代gc的状况

-gcoldcapacity    监视内容基本与-gcold基本相同,输出主要关注最大和最小空间

-gcpermcapacity 输出永久代使用到的最大和最小空间

-compiler             输出JIT编译器编译过的方法、耗时等信息

-printcompilation 输出已经被JIT编译的方法

1. jstat -gc 

下面以进程35713示例

 执行jstat -gc 35713 250 2 

 注:

35713 表示进程Id
250 表示采集间隔
2 采集次数
显示内容
S0C:年轻代中第一个survivor(幸存区)的容量 (字节)         
S1C:年轻代中第二个survivor(幸存区)的容量 (字节)         
S0U:年轻代中第一个survivor(幸存区)目前已使用空间 (字节)         
S1U:年轻代中第二个survivor(幸存区)目前已使用空间 (字节)         
EC:年轻代中Eden的容量 (字节)         
EU:年轻代中Eden 目前已使用空间 (字节)         
OC:Old代的容量 (字节)         
OU:Old代目前已使用空间 (字节)         
PC:Perm(持久代)的容量 (字节)         
PU:Perm(持久代)目前已使用空间 (字节)         
YGC:从应用程序启动到采样时年轻代中gc次数         
YGCT:从应用程序启动到采样时年轻代中gc所用时间(s)         
FGC:从应用程序启动到采样时old代(全gc)gc次数         
FGCT:从应用程序启动到采样时old代(全gc)gc所用时间(s)         
GCT:从应用程序启动到采样时gc用的总时间(s) 

2. jstat -gcutil

 表明该进程的两个survivor区都是空的、新生代使用97.5%的空间 、年老代使用12.37%空间,元空间使用98.52%

ccs 压缩比例97.21%、年轻代gc发生次数138次,耗时3.591秒,年老代发生gc次数744次 耗时215.295秒 GCT垃圾回收消耗总时间218.886秒

3 jstat -gccause

 

 4 jstat - class  

 loaded 加载的类的数量,bytes所占空间大小。unloaded 未加载的类数量 time加载耗时

5. jstat -compile

 compiled 编译的数量 failed 失败数量 invalid不可用的数量 failedMethod 失败方法

 

二  jmap

 jmap 用于生成对内存转储快照,如果不使用jmap使进程发生OOM时自动生成dum文件通过参数 -XX:+HeapDumpOnOutOfMemoryError 参数设置,或者在linux 执行kill -3 恐吓一下虚拟机 也能拿到dump文件。

jmap可以用来查询finalize执行队列,java堆和永久代的详细信息,如空间使用率、当前使用的是哪种收集器等。

jmap命令格式

jmap [option] vmid

相关参数选项

-dump          生成java堆转储快照,格式: -dump:[live,]format=b.file=<filename>,其中live子参数说明是否只dump活的对象
-finalizerinfo 显示在F-queue中等待Finalizer线程执行finalize方法的对象、只在linux平台下有效
-heap 显示java堆详细信息,如使用哪种回收器,参数配置,分代状况等,只在linux平台有效
-histo 显示堆中对象统计信息,包括类、实例数量和合计容量
-permstat 以ClassLoader 为统计口径显示永久代内存状态 只在linux平台有效
-F 当虚拟机进程对-dump 选项没有响应时,可使用这个选项强制生成dump快照 只在linux平台有效

1.  jmap -dump

     jmap -dump:format=b,file=dump.log 35713 

    dump出来的文件可以用MAT、VisualVM等工具查看,这里用jhat查看

如果Dump文件太大,可能需要加上-J-Xmx512m这种参数指定最大堆内存

 

 

 

root@jssz-4:~ # jhat -J-Xmx20480m -port 9998 ./dump.log
Reading from ./dump.log...
Dump file created Sun May 17 08:55:05 CST 2020
Snapshot read, resolving...
Resolving 18571982 objects...
Chasing references, expect 3714 dots..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 9998
Server is ready

下面可以在浏览器打开了

 

 

 

 2. jamp -heap

jmap -heap 35713

Attaching to process ID 35713, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.60-b23

using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC

Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 26843545600 (25600.0MB)
NewSize = 715784192 (682.625MB)
MaxNewSize = 2878930944 (2745.5625MB)
OldSize = 1431699456 (1365.375MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 644349952 (614.5MB)
used = 16207832 (15.456993103027344MB)
free = 628142120 (599.0430068969727MB)
2.5153772340158413% used
Eden Space:
capacity = 572784640 (546.25MB)
used = 2064720 (1.9690704345703125MB)
free = 570719920 (544.2809295654297MB)
0.36047056010440504% used
From Space:
capacity = 71565312 (68.25MB)
used = 14143112 (13.487922668457031MB)
free = 57422200 (54.76207733154297MB)
19.7625240563473% used
To Space:
capacity = 71565312 (68.25MB)
used = 0 (0.0MB)
free = 71565312 (68.25MB)
0.0% used
concurrent mark-sweep generation:
capacity = 1431699456 (1365.375MB)
used = 177149408 (168.94284057617188MB)
free = 1254550048 (1196.4321594238281MB)
12.373365601111187% used

24709 interned Strings occupying 2684168 bytes.

3 jmap -histo

jmap -histo[:live] pid查看堆内存中的对象数目、大小统计直方图,如果带上live则只统计活对象,如下

root@jssz-:~ # jmap -histo:live 35713 |more

num #instances #bytes class name
----------------------------------------------
1: 5391 140070128 [B
2: 69890 9698968 [C
3: 18119 2014704 java.lang.Class
4: 68576 1645824 java.lang.String
5: 49156 1572992 java.util.concurrent.ConcurrentHashMap$Node
6: 15653 1149904 [Ljava.lang.Object;
7: 23267 744544 java.util.Hashtable$Entry
8: 18750 600000 java.util.HashMap$Node
9: 9324 596736 io.netty.buffer.PoolSubpage
10: 31818 509088 java.lang.Object
11: 229 506736 [Ljava.util.concurrent.ConcurrentHashMap$Node;
12: 4309 426072 [Ljava.util.HashMap$Node;
13: 12343 394976 com.codahale.metrics.WeightedSnapshot$WeightedSample
14: 8544 391512 [I
15: 11481 370088 [Ljava.lang.String;
16: 3005 360600 scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9
17: 148 357520 [[B
18: 12360 296640 java.util.concurrent.ConcurrentSkipListMap$Node
19: 12357 296568 java.lang.Double
20: 2843 272928 scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$14
21: 8447 270304 scala.reflect.internal.Symbols$TypeHistory
22: 2869 252472 java.lang.reflect.Method
23: 536 242048 [Lio.netty.buffer.PoolSubpage;
24: 6825 218400 scala.reflect.internal.pickling.UnPickler$Scan$LazyTypeRef
25: 4598 183920 java.util.LinkedHashMap$Entry
26: 3686 176928 java.util.HashMap
27: 207 170848 [Ljava.util.Hashtable$Entry;
28: 6963 167112 java.util.ArrayList
29: 6092 146208 java.util.concurrent.ConcurrentSkipListMap$Index
30: 4552 145664 scala.reflect.internal.Scopes$ScopeEntry
31: 1 131088 [Lscala.reflect.internal.Names$TermName;

 

 

class name是对象类型 B  byte, C  char,D  double,F  float,I  int,J  long,Z  boolean

 

 

 

 

参考资料:

《深入理解Java虚拟机:JVM高级特性与最佳实践》

https://www.cnblogs.com/sessionbest/p/9396678.html

posted @ 2020-05-17 01:10  songchaolin  阅读(234)  评论(0编辑  收藏  举报