zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java自带的性能监测工具之jstat

JAVA性能工具 监测 自带 jstat
2023-06-13 09:17:11 时间

本文使用jstat命令工具~

jstat简介

jstat (Java Virtual Machine Statistics Monitoring Tool) 是一个可以用于观察Java应用程序运行时信息的工具,主要利用JVM内建的指令对Java应用程序的资源和性能进行实时的命令行的监控,包括了对Heap size和垃圾回收状况的监控。

jstat基本语法

使用jstat -help查看jstat基本语法以及基本的参数说明等~

[root@dev18 ~]# jstat -help
Usage: jstat -help|-options
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

Definitions:
  <option>      An option reported by the -options option
  <vmid>        Virtual Machine Identifier. A vmid takes the following form:
                     <lvmid>[@<hostname>[:<port>]]
                Where <lvmid> is the local vm identifier for the target
                Java virtual machine, typically a process id; <hostname> is
                the name of the host running the target Java virtual machine;
                and <port> is the port number for the rmiregistry on the
                target host. See the jvmstat documentation for a more complete
                description of the Virtual Machine Identifier.
  <lines>       Number of samples between header lines.
  <interval>    Sampling interval. The following forms are allowed:
                    <n>["ms"|"s"]
                Where <n> is an integer and the suffix specifies the units as 
                milliseconds("ms") or seconds("s"). The default units are "ms".
  <count>       Number of samples to take before terminating.
  -J<flag>      Pass <flag> directly to the runtime system.
[root@dev18 ~]# 

jstat可选项及其说明

使用jstat -options可以查看可使用的选择项,如-class,-compiler等

[root@dev18 ~]# jstat -options
-class
-compiler
-gc
-gccapacity
-gccause
-gcnew
-gcnewcapacity
-gcold
-gcoldcapacity
-gcpermcapacity
-gcutil
-printcompilation
[root@dev18 ~]# 

options说明~

-class                       显示class loader的信息 ,例如当前总共加载了多少个类

-compile                     显示HotSpot Just-in-Time compiler的信息

-gc                          显示jdk gc时heap信息 

-gccapacity                  显示不同的generations相应的heap容量情况 

-gccause                     显示gc的情况,(同-gcutil)和引起gc的事件 

-gcnew                       显示gc时,新生代的情况 

-gcnewcapacity               显示gc时,新生代heap容量 

-gcold                       显示gc时,老年区的情况 

-gcoldcapacity               显示gc时,老年区heap容量 

-gcpermcapacity              显示gc时,permanent区heap容量 

-gcutil                      显示垃圾收集信息

-printcompilation            输出JIT编译的方法信息

-gc综合了-gcnew、-gcold的输出; -gccapacity综合了-gcnewcapacity、-gcoldcapacity、-gcpermcapacity的输出

jstat示例

显示Java进程的ClassLoader信息

输出Java进程(PID为12905)的Class Loader信息,每隔2秒执行一下,一共输出5次~

[root@dev18 ~]# jstat -class 12905 2000 5
Loaded  Bytes  Unloaded  Bytes     Time   
  1515  2875.1        0     0.0       0.57
  1515  2875.1        0     0.0       0.57
  1515  2875.1        0     0.0       0.57
  1515  2875.1        0     0.0       0.57
  1515  2875.1        0     0.0       0.57
[root@dev18 ~]# 

其中,Loaded表示载入了类的数量,Bytes表示载入类的合计大小。

显示JVM gc信息

如下示例说明

[root@dev18 ~]# jstat -gc 12905 2000 5
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC     PU    YGC     YGCT    FGC    FGCT     GCT   
512.0  512.0   0.0    64.0  14848.0   6236.9   40448.0     3532.2   21504.0 8729.3     91    0.172   0      0.000    0.172
512.0  512.0   0.0    64.0  14848.0   6236.9   40448.0     3532.2   21504.0 8729.3     91    0.172   0      0.000    0.172
512.0  512.0   0.0    64.0  14848.0   6236.9   40448.0     3532.2   21504.0 8729.3     91    0.172   0      0.000    0.172
512.0  512.0   0.0    64.0  14848.0   6236.9   40448.0     3532.2   21504.0 8729.3     91    0.172   0      0.000    0.172
512.0  512.0   0.0    64.0  14848.0   6236.9   40448.0     3532.2   21504.0 8729.3     91    0.172   0      0.000    0.172
[root@dev18 ~]# 

参数说明:

S0C     当前survivor space 0 的总容量 (KB).

S1C     当前survivor space 1 的总容量 (KB).

S0U     当前survivor space 0 已使用的容量 (KB).

S1U     当前survivor space 1 已使用的容量 (KB).

EC      当前 eden space 总容量 (KB).

EU      当前eden space已经使用的容量 (KB).

OC      当前 old space 总容量 (KB).

OU      当前old space 已使用容量(KB).

PC      当前 permanent space 总容量(KB).

PU      当前 permanent space 已使用容量 (KB).

YGC     从应用启动时到现在,年轻代young generation 发生GC Events的总次数.

YGCT    从应用启动时到现在, 年轻代Young generation 垃圾回收的总耗时.

FGC     从应用启动时到现在, full GC事件总次数.

FGCT    从应用启动时到现在, Full sc总耗时.

GCT     从应用启动时到现在, 垃圾回收总时间. GCT=YGCT+FGCT

查看JIT编译的信息

[root@dev18 ~]# jstat -compiler 12905
Compiled Failed Invalid   Time   FailedType FailedMethod
     263      0       0     2.92          0    

显示垃圾收集信息

如:

[root@dev18 ~]# jstat -gcutil  12905
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   
  0.00  12.50  48.01   8.73  40.59     91    0.172     0    0.000    0.172
[root@dev18 ~]# 
[root@dev18 ~]# jstat -gcnew  12905
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  
 512.0  512.0    0.0   64.0  1  15  512.0  14848.0   7128.3     91    0.172
[root@dev18 ~]# 
root@dev18 ~]# jstat -gcold  12905
   PC       PU        OC          OU       YGC    FGC    FGCT     GCT   
 21504.0   8729.3     40448.0      3532.2     91     0    0.000    0.172
[root@dev18 ~]# 

还有很多示例,这里就不一一列举了~