java性能优化常用工具jps、jstat、jinfo
jps:虚拟机进程状况工具
jps可以用来查看虚拟机进程,基本等同于ps -ef|grep java
#查看jps的使用文档 [root@localhost script]# jps -help usage: jps [-help] jps [-q] [-mlvV] [<hostid>] Definitions: <hostid>: <hostname>[:<port>] #查看虚拟机的进程号 [root@localhost script]# jps 4176 Jps 3659 swaggerdemo-0.0.1-SNAPSHOT.jar #查看虚拟机的进程号,只输出进程号 [root@localhost script]# jps -q 4192 3659 #输出虚拟机启动时传给main方法的参数 [root@localhost script]# jps -m 4208 Jps -m 3659 swaggerdemo-0.0.1-SNAPSHOT.jar #输出主类的全名,或者jar的全路径 [root@localhost script]# jps -l 4224 sun.tools.jps.Jps 3659 /data/services/demo/swaggerdemo-0.0.1-SNAPSHOT.jar #输出jvm的参数 [root@localhost script]# jps -v 4240 Jps -Denv.class.path=.:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar -Dapplication.home=/usr/java/jdk1.8.0_131 -Xms8m 3659 swaggerdemo-0.0.1-SNAPSHOT.jar -Dloader.path=/data/services/demo -Dlog.path=/data/logs -Xms256m -Xmx512m #输出-l和-v的合集 [root@localhost script]# jps -lv 4256 sun.tools.jps.Jps -Denv.class.path=.:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar -Dapplication.home=/usr/java/jdk1.8.0_131 -Xms8m 3659 /data/services/demo/swaggerdemo-0.0.1-SNAPSHOT.jar -Dloader.path=/data/services/demo -Dlog.path=/data/logs -Xms256m -Xmx512m
jstat:虚拟机统计信息监视工具
用来监视虚拟机各种运行状态信息的命令行工具,可以显示虚拟机进程中类装载、内存、垃圾回收、JIT编译等运行数据,是运行期定位问题的首选工具。
#查看帮助文档 [root@localhost script]# 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.
option:可以通过jstat -options 查看具体有哪些java虚拟机进程的数据可以被监控,见下面的执行代码。
vmid:java虚拟机进程的id,可以通过jps -l来查看。(距离我这儿的java虚拟机进程id是3659)
lines:用来添加表头,比如你要监视类装载情况,通过 -h5,可以指定每5行输出一次表头。(jstat -class -t -h5 3659 500 20和jstat -class -t 3659 500 20各执行一次,就可以明白)
interval:采样的间隔时间,比如每500ms采样一次输出一行。(jstat -class -t -h5 3659 500 20)
count:采样的次数,比如对虚拟机类装载采样20次,每隔500ms采一次,每5行输出一次表头。(jstat -class -t -h5 3659 500 20,-class是监视类装载,-t是输出时间戳,3659是虚拟机进程id,500是每隔500ms采样,20是总共采样的次数)
[root@localhost script]# jstat -class -t -h5 3659 500 20 Timestamp Loaded Bytes Unloaded Bytes Time 2505.5 7846 13754.1 0 0.0 11.06 2506.0 7846 13754.1 0 0.0 11.06 2506.5 7846 13754.1 0 0.0 11.06 2507.0 7846 13754.1 0 0.0 11.06 2507.5 7846 13754.1 0 0.0 11.06 Timestamp Loaded Bytes Unloaded Bytes Time 2508.0 7846 13754.1 0 0.0 11.06 2508.5 7846 13754.1 0 0.0 11.06 2509.0 7846 13754.1 0 0.0 11.06 2509.5 7846 13754.1 0 0.0 11.06 2510.0 7846 13754.1 0 0.0 11.06 Timestamp Loaded Bytes Unloaded Bytes Time 2510.5 7846 13754.1 0 0.0 11.06 2511.0 7846 13754.1 0 0.0 11.06 2511.5 7846 13754.1 0 0.0 11.06 2512.0 7846 13754.1 0 0.0 11.06 2512.5 7846 13754.1 0 0.0 11.06 Timestamp Loaded Bytes Unloaded Bytes Time 2513.1 7846 13754.1 0 0.0 11.06 2513.5 7846 13754.1 0 0.0 11.06 2514.0 7846 13754.1 0 0.0 11.06 2514.6 7846 13754.1 0 0.0 11.06 2515.1 7846 13754.1 0 0.0 11.06
通过jstat -options查看,能够监视的类别有哪些
[root@localhost script]# jstat -options -class -compiler -gc -gccapacity -gccause -gcmetacapacity -gcnew -gcnewcapacity -gcold -gcoldcapacity -gcutil -printcompilation
-class |
监视类装载、卸载数量、总空间及类装载所耗费时间 |
-compiler |
输出编译器编译的方法、耗时等信息 |
-gc |
监视堆得使用情况,包括eden、survivor、老年代、永久代 |
-gccapacity |
与-gc基本相同,主要关注堆各个区域最大、最小空间 |
-gccause |
与-gcutil基本相同,输出最近一次GC产生原因 |
-gcmetacapacity |
输出元空间用到的最大、最小空间 |
-gcnew |
新生代,参考-gc解释 |
-gcnewcapacity |
新生代,参考-gccapacity解释 |
-gcold |
老年代 |
-gcoldcapacity |
老年代 |
-gcutil |
与-gc基本相同,主要关注已使用空间占总空间的比值 |
-printcompilation |
监视已经被编译的方法 |
jinfo:java配置信息工具
jinfo可以实时查看和调整虚拟机各项参数
[root@localhost script]# jinfo -help 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
jinfo可以查看两种类型数据,一种是system properties(通过执行jinfo -sysprops 3659),一种是vm flags(通过执行jinfo -flags 3659),如果不指定参数,则把两种都打印出来(jinfo 3659)。查询某个vm flag,可以通过制定flag name来进行(jinfo -flag NewSize 3659,会把虚拟机参数NewSize打印出来),也支持实时修改flag参数。(3659是java虚拟机进程id)