一 点睛
jstat(JVM Statistics Monitoring Tool):用于监视虚拟机各种运行状态信息的命令行工具。它可以显示本地或者远程虚拟机进程中的类装载、内存、垃圾收集、JIT 编译等运行数据。
在没有 GUI 图形界面,只提供了纯文本控制台环境的服务器上,它将是运行期定位虚拟机性能问题的首选工具。常用于检测垃圾回收问题以及内存泄漏问题。
官方文档:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html
基本使用语法为:jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
查看命令相关参数:jstat -h 或 jstat -help 或 jstat
F:\svn\19_byzb_back\branches\JVMDemo3>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.
其中 vmid 是进程 id 号,也就是 jps 之后看到的前面的号码,如下:
F:\svn\19_byzb_back\branches\JVMDemo3>jps
10128 Launcher
7744 Jps
7872 ScannerTest
2756
option 参数
选项 option 可以由以下值构成。
类装载相关的:
-
-class:显示 ClassLoader 的相关信息:类的装载、卸载数量、总空间、类装载所消耗的时间等
垃圾回收相关的:
-
-gc:显示与 GC 相关的堆信息。包括 Eden 区、两个 Survivor 区、老年代、永久代等的容量、已用空间、GC 时间合计等信息。
-
-gccapacity:显示内容与 -gc 基本相同,但输出主要关注 Java 堆各个区域使用到的最大、最小空间。
-
-gcutil:显示内容与 -gc 基本相同,但输出主要关注已使用空间占总空间的百分比。
-
-gccause:与 -gcutil 功能一样,但是会额外输出导致最后一次或当前正在发生的 GC 产生的原因。
-
-gcnew:显示新生代 GC 状况
-
-gcnewcapacity:显示内容与 -gcnew 基本相同,输出主要关注使用到的最大、最小空间
-
-geold:显示老年代 GC 状况
-
-gcoldcapacity:显示内容与 -gcold 基本相同,输出主要关注使用到的最大、最小空间
-
-gcpermcapacity:显示永久代使用到的最大、最小空间。
JIT 相关的:
-
-compiler:显示 JIT 编译器编译过的方法、耗时等信息
-
-printcompilation:输出已经被 JIT 编译的方法
interval 参数
用于指定输出统计数据的周期,单位为毫秒。即:查询间隔
count 参数
用于指定查询的总次数
-t 参数
可以在输出信息前加上一个 Timestamp 列,显示程序的运行时间。单位:秒
-h 参数
可以在周期性数据输出时,输出多少行数据后输出一个表头信息
二 实战—— jstat -class 搭配其它参数的使用
1 代码
package chaper02;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String info = scanner.next();
}
}
2 测试
F:\svn\19_byzb_back\branches\JVMDemo3>jps
10128 Launcher
7872 ScannerTest
2756
10748 Jps
# Loaded:加载的类的个数
# Bytes:加载的类所占的总空间
# Unloaded:卸载的类的个数
# Bytes:卸载的类所占的总空间
# Time:类装载所消耗的时间
F:\svn\19_byzb_back\branches\JVMDemo3>jstat -class 7872
Loaded Bytes Unloaded Bytes Time
615 1229.8 0 0.0 0.24
# interval 参数:用于指定输出统计数据的周期,单位为毫秒,即查询间隔。当为1000,每秒打印一次,不停的打印。
F:\svn\19_byzb_back\branches\JVMDemo3>jstat -class 7872 1000
Loaded Bytes Unloaded Bytes Time
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
# count 参数: 用于指定查询的总次数,这里只打印6次
F:\svn\19_byzb_back\branches\JVMDemo3>jstat -class 7872 1000 6
Loaded Bytes Unloaded Bytes Time
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
615 1229.8 0 0.0 0.24
# -t 参数: 可以在输出信息前加上一个 Timestamp 列,显示程序的运行时间。单位:秒。这里说明程序已运行了 1759.5 秒。
F:\svn\19_byzb_back\branches\JVMDemo3>jstat -class -t 7872
Timestamp Loaded Bytes Unloaded Bytes Time
1759.5 615 1229.8 0 0.0 0.24
# -h 参数: 可以在周期性数据输出时,输出多少行数据后输出一个表头信息。这里表示每隔3行数据就输出一个表头
F:\svn\19_byzb_back\branches\JVMDemo3>jstat -class -t -h3 7872 1000 10
Timestamp Loaded Bytes Unloaded Bytes Time
1918.1 615 1229.8 0 0.0 0.24
1919.1 615 1229.8 0 0.0 0.24
1920.1 615 1229.8 0 0.0 0.24
Timestamp Loaded Bytes Unloaded Bytes Time
1921.1 615 1229.8 0 0.0 0.24
1922.1 615 1229.8 0 0.0 0.24
1923.1 615 1229.8 0 0.0 0.24
Timestamp Loaded Bytes Unloaded Bytes Time
1924.1 615 1229.8 0 0.0 0.24
1925.1 615 1229.8 0 0.0 0.24
1926.2 615 1229.8 0 0.0 0.24
Timestamp Loaded Bytes Unloaded Bytes Time
1927.1 615 1229.8 0 0.0 0.24