说一说 jdk 自带的 JVM 性能调优常用工具?

说一说 jdk 自带的 JVM 性能调优常用工具?

1 个解决方案

AllenJiang
中间件研发,关注微信公众号 : 小哈学Java, 回复"666", 即可免费领取10G学习&面试资料

企业级开发中,难免会遇到一些靠分析线上打印的日志,无法锁定具体问题代码的情况发生,诸如:

  • OOM 内存溢出,内存泄漏

  • 线程死锁

  • 消耗 cpu 过高等

下面简单介绍一下常用的几个工具.

jps

jps 主要用来输出 JVM 中运行的进程状态信息.使用说明如下:

[root@iZbp1j8y1bab0djl9gdp33Z ~]# jps --help
illegal argument: --help
usage: jps [-help]
	   jps [-q] [-mlvV] [<hostid>]

Definitions:
	<hostid>:      <hostname>[:<port>]

参数说明:

-q 指定 jps 只输出进程 ID ,不输出类的短名称

-m 输出传递给 Java 进程(主函数)的参数

-l 输出主函数的完整路径

-v 显示传递给 JVM 的参数

jinfo

jvm 的配置信息工具。使用说明如下:

[root@iZbp1j8y1bab0djl9gdp33Z ~]# jinfo
Usage:
	jinfo [option] <pid>
		(to connect to running process)
	jinfo [option] <executable <core>
		(to connect to a core file)
	jinfo [option] [server_id@]<remote server IP or hostname>
		(to connect to remote debug server)

where <option> is one of:
	-flag <name>         to print the value of the named VM flag
	-flag [+|-]<name>    to enable or disable the named VM flag
	-flag <name>=<value> to set the named VM flag to the given value
	-flags               to print VM flags
	-sysprops            to print Java system properties
	<no option>          to print both of the above
	-h | -help           to print this help message

参数说明:

-flag 打印指定 JVM 的参数值

-flag [+|-] 设置指定 JVM 参数的布尔值

-flag = 设置指定 JVM 参数的值

jmap

查看 jvm 内存信息工具。使用说明如下:

Usage:
	jmap [option] <pid>
		(to connect to running process)
	jmap [option] <executable <core>
		(to connect to a core file)
	jmap [option] [server_id@]<remote server IP or hostname>
		(to connect to remote debug server)

where <option> is one of:
	<none>               to print same info as Solaris pmap
	-heap                to print java heap summary
	-histo[:live]        to print histogram of java object heap; if the "live"
						 suboption is specified, only count live objects
	-clstats             to print class loader statistics
	-finalizerinfo       to print information on objects awaiting finalization
	-dump:<dump-options> to dump java heap in hprof binary format
						 dump-options:
						   live         dump only live objects; if not specified,
										all objects in the heap are dumped.
						   format=b     binary format
						   file=<file>  dump heap to <file>
						 Example: jmap -dump:live,format=b,file=heap.bin <pid>
	-F                   force. Use with -dump:<dump-options> <pid> or -histo
						 to force a heap dump or histogram when <pid> does not
						 respond. The "live" suboption is not supported
						 in this mode.
	-h | -help           to print this help message
	-J<flag>             to pass <flag> directly to the runtime system

参数说明:

-histo 生成 Java 应用程序的堆快照和对象的统计信息

-dump Dump 堆详细信息,可以用于分析 OOM 导致的原因

-heap 输出堆信息

jstack

jvm 栈信息工具.使用说明如下:

Usage:
	jstack [-l] <pid>
		(to connect to running process)
	jstack -F [-m] [-l] <pid>
		(to connect to a hung process)
	jstack [-m] [-l] <executable> <core>
		(to connect to a core file)
	jstack [-m] [-l] [server_id@]<remote server IP or hostname>
		(to connect to a remote debug server)

Options:
	-F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)
	-m  to print both java and native frames (mixed mode)
	-l  long listing. Prints additional information about locks
	-h or -help to print this help message

参数说明:

Stack Trace 打印线程 dump, 发现线程目前停留在那行代码

-l 打印线程锁信息

-F 强制 dump,当 jstack 没有响应时使用

jstat

jvm 统计监测工具.使用说明如下:

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.

参数说明:

class (类加载器) 

compiler (JIT) 

gc (GC 堆状态) 

gccapacity (各区大小) 

gccause (最近一次 GC 统计和原因) 

gcnew (新生代统计)

gcnewcapacity (新生代大小)

gcold (老年代统计)

gcoldcapacity (老年代大小)

gcpermcapacity (永久区大小)

gcutil (GC 统计汇总)

printcompilation (HotSpot 编译统计)


jstat -gc <pid>: 可以显示 gc 的信息,查看 gc 的次数,及时间, 各个列名描述:

显示列名具体描述
S0C年轻代中第一个 survivor(幸存区)的容量 (KB)
S1C年轻代中第二个 survivor(幸存区)的容量 (KB)
S0U年轻代中第一个 survivor(幸存区)目前已使用空间 (KB)
S1U年轻代中第二个 survivor(幸存区)目前已使用空间 (KB)
EC年轻代中 Eden(伊甸园)的容量 (KB)
EU年轻代中 Eden(伊甸园)目前已使用空间 (KB)
OCOld 代的容量 (KB)
OUOld 代目前已使用空间 (KB)
PCPerm(持久代) 的容量 (KB)
PUPerm(持久代) 目前已使用空间 (KB)
YGC从应用程序启动到采样时年轻代中 gc 次数
YGCT从应用程序启动到采样时年轻代中 gc 所用时间 (s)
FGC从应用程序启动到采样时 old 代 (全 gc)gc 次数
FGCT从应用程序启动到采样时 old 代 (全 gc)gc 所用时间 (s)
GCT从应用程序启动到采样时 gc 用的总时间 (s)

jstat -gcutil <pid>: 统计 gc 信息,各个列名描述:

显示列名具体描述
S0年轻代中第一个 survivor(幸存区)已使用的占当前容量百分比
S1年轻代中第二个 survivor(幸存区)已使用的占当前容量百分比
年轻代中 Eden(伊甸园)已使用的占当前容量百分比
Oold 代已使用的占当前容量百分比
perm 代已使用的占当前容量百分比
YGC从应用程序启动到采样时年轻代中 gc 次数
YGCT从应用程序启动到采样时年轻代中 gc 所用时间 (s)
FGC从应用程序启动到采样时 old 代 (全 gc)gc 次数
FGCT从应用程序启动到采样时 old 代 (全 gc)gc 所用时间 (s)
GCT从应用程序启动到采样时 gc 用的总时间 (s)

jconsole

jdk 自带的图形化性能分析工具。

jvisualvm

jdk 自带的图形化性能分析工具。