JVM监控命令
1.概述
Jcmd是一个诊断Jvm的命令集工具, 集成了包括Jps, Jstack以及采集JFR信息等功能. 它必须运行在被诊断Jvm进程的同一台机器上.
1)查询JVM进程及PID
/dapeng-container/bin # jcmd -l
252 sun.tools.jcmd.JCmd -l
13 com.github.dapeng.bootstrap.Bootstrap
2)查询指定进程及
/dapeng-container/bin # jcmd 13 VM.flags
13:
-XX:CICompilerCount=3 -XX:+HeapDumpOnOutOfMemoryError -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=268435456 -XX:MaxNewSize=134217728 -XX:MinHeapDeltaBytes=524288 -XX:NewRatio=1 -XX:NewSize=134217728 -XX:OldSize=134217728 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=30 -XX:ThreadStackSize=256 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:+UseParallelOldGC
2.功能描述
2.1 查看Jvm进程
等同于Jps,但信息更加详细
jcmd -l
166 sun.tools.jcmd.JCmd
11 com.github.dapeng.bootstrap.Bootstrap
2.2 查看某Jvm进程可用操作
jcmd pid help
jcmd 3202 help
3202:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help
For more information about a specific command use 'help <command>'.
2.3 查看操作的帮助信息(low)
可查看操作命令的说明, 语法以及该操作对jvm进程的影响
# jcmd 3202 help GC.run
3202:
GC.run
Call java.lang.System.gc().
Impact: Medium: Depends on Java heap size and content.
Syntax: GC.run
2.4 启用JVM高级特性(low)
启用某些高级的特性,例如JFR
# jcmd 12 VM.check_commercial_features
12:
Commercial Features are locked.
# jcmd 12 VM.unlock_commercial_features
12:
Commercial Features now unlocked.
# jcmd 12 VM.check_commercial_features
12:
Commercial Features are unlocked.
Status of individual features:
Java Flight Recorder has not been used.
Resource Management is disabled.
Current Memory Restriction: None (0)
2.5 开始记录JFR飞行记录仪(medium)
在 JDK 8u40 之前,JVM 必须带启动标记: -XX:+UnlockCommercialFeatures -XX:FlightRecorder,从 JDK 8u40 开始,JFR 可以在运行时启用。
使用前打开JVM的高级特性:
jcmd $PID VM.unlock_commercial_features
# jcmd 12 help JFR.start
12:
JFR.start
Starts a new JFR recording
Impact: Medium: Depending on the settings for a recording, the impact can range from low to high.
Permission: java.lang.management.ManagementPermission(monitor)
Syntax : JFR.start [options]
Options: (options must be specified using the <key> or <key>=<value> syntax)
name : [optional] Name that can be used to identify recording, e.g. \"My Recording\" (STRING, no default value)
defaultrecording : [optional] Starts the default recording, can only be combined with settings. (BOOLEAN, false)
dumponexit : [optional] Dump running recording when JVM shuts down (BOOLEAN, no default value)
settings : [optional] Settings file(s), e.g. profile or default. See JRE_HOME/lib/jfr (STRING SET, no default value)
delay : [optional] Delay recording start with (s)econds, (m)inutes), (h)ours), or (d)ays, e.g. 5h. (NANOTIME, 0)
duration : [optional] Duration of recording in (s)econds, (m)inutes, (h)ours, or (d)ays, e.g. 300s. (NANOTIME, 0)
filename : [optional] Resulting recording filename, e.g. \"/home/user/My Recording.jfr\" (STRING, no default value)
compress : [optional] GZip-compress the resulting recording file (BOOLEAN, false)
maxage : [optional] Maximum time to keep recorded data (on disk) in (s)econds, (m)inutes, (h)ours, or (d)ays, e.g. 60m, or 0 for no limit (NANOTIME, 0)
maxsize : [optional] Maximum amount of bytes to keep (on disk) in (k)B, (M)B or (G)B, e.g. 500M, or 0 for no limit (MEMORY SIZE, 0)
jcmd $PID JFR.start name=dapeng,duration=120s
等待至少duration(本文设定120s)后,执行命令:
jcmd $PID JFR.dump name=dapeng,duration=120s filename=dapeng.jfr(注意,文件名必须为.jfr后缀)
检查JFR状态
jcmd $PID JFR.check name=dapeng,duration=120s
停止JFR
jcmd $PID JFR.stop name=dapeng,duration=120s
2.2 查看进程的Jvm参数(low)
例如GC算法, 内存分配等
# jcmd 12 VM.flags
12:
-XX:CICompilerCount=3 -XX:+HeapDumpOnOutOfMemoryError -XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:MaxNewSize=715653120 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=715653120 -XX:OldSize=1431830528 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:ThreadStackSize=256 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
2.2 查看进程的命令行启动参数(low)
例如GC算法, 内存分配等
# jcmd 12 VM.command_line
12:
VM Arguments:
jvm_args: -Dfile.encoding=UTF-8 -Dsun.jun.encoding=UTF-8 -Dname=commonData_service -Dfile.encoding=UTF-8 -Dsun.jun.encoding=UTF-8 -Xms256m -Xmx256m -Xloggc:/dapeng-container/bin/../logs/gc.log -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGC -XX:+HeapDumpOnOutOfMemoryError -Dsoa.base=/dapeng-container/bin/../ -Dsoa.run.mode=native -Dsoa.transactional.enable=false -Dsoa.monitor.enable=true -Dsoa.core.pool.size=100 -Xms2g -Xmx2g -Xss256K
java_command: com.github.dapeng.bootstrap.Bootstrap
java_class_path (initial): ./dapeng-bootstrap.jar
Launcher Type: SUN_STANDARD
2.2 查看JDK版本(low)
# jcmd 12 VM.version
12:
Java HotSpot(TM) 64-Bit Server VM version 25.131-b11
JDK 8.0_131
2.2 查看Jvm进程运行时间(low)
# jcmd 12 VM.uptime
12:
9530.265 s
2.2 查看Jvm系统变量(low)
# jcmd 12 VM.system_properties
12:
#Fri Apr 13 00:31:37 GMT+08:00 2018
sun.jun.encoding=UTF-8
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=/opt/oracle-server-jre/jre/lib/amd64
java.vm.version=25.131-b11
java.vm.vendor=Oracle Corporation
java.vendor.url=http\://java.oracle.com/
path.separator=\:
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
soa.base=/dapeng-container/bin/../
file.encoding.pkg=sun.io
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/dapeng-container/bin
java.runtime.version=1.8.0_131-b11
soa.monitor.enable=true
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
java.endorsed.dirs=/opt/oracle-server-jre/jre/lib/endorsed
os.arch=amd64
name=commonData_service
java.io.tmpdir=/tmp
line.separator=\n
java.vm.specification.vendor=Oracle Corporation
soa.core.pool.size=100
os.name=Linux
sun.jnu.encoding=ANSI_X3.4-1968
java.library.path=/usr/java/packages/lib/amd64\:/usr/lib64\:/lib64\:/lib\:/usr/lib
sun.nio.ch.bugLevel=
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=3.10.0-514.el7.x86_64
user.home=/root
soa.transactional.enable=false
user.timezone=GMT+08\:00
java.awt.printerjob=sun.print.PSPrinterJob
file.encoding=UTF-8
java.specification.version=1.8
java.class.path=./dapeng-bootstrap.jar
user.name=root
java.vm.specification.version=1.8
sun.java.command=com.github.dapeng.bootstrap.Bootstrap
java.home=/opt/oracle-server-jre/jre
sun.arch.data.model=64
user.language=en
java.specification.vendor=Oracle Corporation
soa.run.mode=native
awt.toolkit=sun.awt.X11.XToolkit
java.vm.info=mixed mode
java.version=1.8.0_131
java.ext.dirs=/opt/oracle-server-jre/jre/lib/ext\:/usr/java/packages/lib/ext
sun.boot.class.path=/opt/oracle-server-jre/jre/lib/resources.jar\:/opt/oracle-server-jre/jre/lib/rt.jar\:/opt/oracle-server-jre/jre/lib/sunrsasign.jar\:/opt/oracle-server-jre/jre/lib/jsse.jar\:/opt/oracle-server-jre/jre/lib/jce.jar\:/opt/oracle-server-jre/jre/lib/charsets.jar\:/opt/oracle-server-jre/jre/lib/jfr.jar\:/opt/oracle-server-jre/jre/classes
java.vendor=Oracle Corporation
file.separator=/
java.vendor.url.bug=http\://bugreport.sun.com/bugreport/
sun.io.unicode.encoding=UnicodeLittle
sun.cpu.endian=little
sun.cpu.isalist=