java性能分析--JDK自带监控工具值jcmd

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第9天,点击查看活动详情

经过前面的操作系统监控工具的学习,到了这一篇,总算进入到java相关的主题了。下面将会介绍java当中常用的性能分析工具,都是JDK自带的,可以帮助我们深入的了解JVM。

查看系统是否正确安装jdk

有些同学安装的是openjdk,在使用过程中,发现jps,jcmd等等命令都是无法使用的,这时我们需要通过下面的方式将他们安装上就可以了:

# 查看版本
[root@hecs-402944 nicstat-1.95]# java -version
openjdk version "1.8.0_322"
OpenJDK Runtime Environment (build 1.8.0_322-b06)
OpenJDK 64-Bit Server VM (build 25.322-b06, mixed mode)
# 安装
[root@hecs-402944 nicstat-1.95]# yum install java-1.8.0-openjdk-devel -y
复制代码

JDK自带工具

下面我们来学习下JDK为我们提供了哪些监控工具。

jcmd

通过名字就知道,类似于windows的cmd,这是jvm的cmd。

首先我们看下帮助文档:

[root@hecs-402944 nicstat-1.95]# jcmd -h
Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>
   or: jcmd -l                                                    
   or: jcmd -h                                                    
                                                                  
  command must be a valid jcmd command for the selected jvm.      
  Use the command "help" to see which commands are available.   
  If the pid is 0, commands will be sent to all Java processes.   
  The main class argument will be used to match (either partially 
  or fully) the class used to start Java.                         
  If no options are given, lists Java processes (same as -p).     
                                                                  
  PerfCounter.print display the counters exposed by this process  
  -f  read and execute commands from the file                     
  -l  list JVM processes on the local machine                     
  -h  this help       
复制代码

我们得知有三个参数可以使用:

  • -f 从文件读取和执行命令

  • -l 获取当前机器的所有jvm进程

  • -h 帮助

jcmd -l结果如下:

[root@hecs-402944 nicstat-1.95]# jcmd -l
24336 sun.tools.jcmd.JCmd -l
10435 weather-forecast-0.0.1-SNAPSHOT.jar
731 org.tanukisoftware.wrapper.WrapperSimpleApp CloudResetPwdUpdateAgent
复制代码

带命令的实现方式格式如下:

jcmd <pid | main class> <command ...|PerfCounter.print|-f file>
复制代码
  • pid 是java进程id

  • main class 进程名称(如上 -l 查询出来的 weather-forecast-0.0.1-SNAPSHOT.jar)

  • command 直接使用命令

    • 可以通过 java <pid | main class> help 查看可使用的命令
    [root@hecs-402944 ~]# jcmd 10435 help
    10435:
    The following commands are available:
    VM.unlock_commercial_features
    JFR.configure
    JFR.stop
    JFR.start
    JFR.dump
    JFR.check
    VM.native_memory
    ManagementAgent.stop
    ManagementAgent.start_local
    ManagementAgent.start
    VM.classloader_stats
    GC.rotate_log
    Thread.print
    GC.class_stats
    GC.class_histogram
    GC.heap_dump
    GC.finalizer_info
    GC.heap_info
    GC.run_finalization
    GC.run
    VM.uptime
    VM.dynlibs
    VM.flags
    VM.system_properties
    VM.command_line
    VM.version
    help
    复制代码

    如果还不明白每个命令的含义,可以使用java <pid | main class> help <command>的方式查看命令的明细,如下查看GC.run的含义:

      [root@hecs-402944 ~]# jcmd 10435 help GC.run
      10435:
      GC.run
      Call java.lang.System.gc().
    
      Impact: Medium: Depends on Java heap size and content.
    
      Syntax: GC.run
    复制代码
  • PerfCounter.print 打印性能计数器,通常用于查看进程的性能。这个内容很多,贴个图看看那大家能否看懂:

image.png

  • -f file 执行文件内的命令

    我们编辑一个测试脚本文件:

    vi test_script
    复制代码

    内容如下所示:

    VM.version
    VM.uptime
    复制代码

    执行:

    [root@hecs-402944 opt]# jcmd 10435 -f test_script 
    10435:
    OpenJDK 64-Bit Server VM version 25.322-b06
    JDK 8.0_322
    509103.971 s
    复制代码

    如上所示输出了JDK版本和更新时间。

小结

本票主要介绍了jcmd的使用方式,关于具体的操作命令我就不详细介绍了,需要各位需要的同学查看help去自行学习了。

猜你喜欢

转载自juejin.im/post/7104441940921286686