JDK的命令行工具

image.png

jps: 虚拟机进程状况工具

JVM Process Status Tool, 可以列出正在运行的虚拟机进程, 并显示虚拟机执行主类(main()函数所在的类)的名称, 以及这些进程的本地虚拟机的唯一ID(LVMID, Local Virtual Machine Identifier).
对于本地虚拟机进程来说, LVMID与操作系统的进程ID(PID, Process Identifier)是一致的.

命令格式: jps [option] [hostid]
option如下表, hostid为RMI注册表中注册的主机名
image.png
测试: 本地跑个springboot项目

  1. C:\Users\Administrator>jps -q
  2. 13856
  3. 11604
  4. 12468
  5. 11384
  6. C:\Users\Administrator>jps -m
  7. 13856
  8. 12068 Jps -m
  9. 12468 Launcher C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/asm-all-7.0.1.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/plugins/java/lib/maven-model-builder-3.3.9.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/netty-buffer-4.1.41.Final.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/commons-logging-1.2.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/plugins/java/lib/jps-builders-6.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/lz4-java-1.6.0.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/trove4j.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/resources_en.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/qdox-2.0-M10.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/jna-platform.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/netty-codec-4.1.41.Final.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/nanoxml-2.2.3.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/lib/gson-2.8.5.
  10. 11384 ProducerApplication
  11. C:\Users\Administrator>jps -l
  12. 13856
  13. 10900 sun.tools.jps.Jps
  14. 12468 org.jetbrains.jps.cmdline.Launcher
  15. 11384 top.xinzhang0618.producer.ProducerApplication
  16. C:\Users\Administrator>jps -v
  17. 13856 exit -Xms2035m -Xmx2035m -XX:ReservedCodeCacheSize=240m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -XX:CICompilerCount=2 -Dsun.io.useCanonPrefixCache=false -Djava.net.preferIPv4Stack=true -Djdk.http.auth.tunneling.disabledSchemes="" -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Djdk.attach.allowAttachSelf -Dkotlinx.coroutines.debug=off -Djdk.module.illegalAccess.silent=true -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\bin\jetbrains-agent.jar -Djb.vmOptionsFile=C:\Users\Administrator\.IntelliJIdea2019.3\config\idea64.exe.vmoptions -Djava.library.path=C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\jbr\\bin;C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\jbr\\bin\server -Didea.jre.check=true -Dide.native.launcher=true -Didea.paths.selector=IntelliJIdea2019.3 -XX:ErrorFile=C:\Users\Administrator\java_error_in_idea_%p.log -XX:HeapDumpPath=C:\Users\Administrator\java_error_in_idea.hprof
  18. 12468 Launcher -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs="" -Djdt.compiler.useSingleThread=true -Dpreload.project.path=D:/巨益/参考项目/demo/xinzhang-demo -Dpreload.config.path=C:/Users/Administrator/.IntelliJIdea2019.3/config/options -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djava.net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=-6685267648664660870 -Dfile.encoding=GBK -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2019.3 -Didea.home.path=C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2 -Didea.config.path=C:\Users\Administrator/.IntelliJIdea2019.3/config -Didea.plugins.path=C:\Users\Administrator/.IntelliJIdea2019.3/config/plugins -Djps.log.dir=C:/Users/Administrator/.IntelliJIdea2019.3/system/log/build-log -Djps.fallback.jdk.home=C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.2/jbr -Djps.fallback.jdk.version=11.0.5 -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/Administrator/.IntelliJIdea2019.3/system/compile-server/xinzhang-demo_fb122103/_temp_ -Djps
  19. 11384 ProducerApplication -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:58857,suspend=y,server=n -XX:TieredStopAtLevel=1 -Xverify:none -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:C:\Users\Administrator\.IntelliJIdea2019.3\system\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8
  20. 15308 Jps -Denv.class.path=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar -Dapplication.home=C:\Program Files\Java\jdk1.8.0_221 -Xms8m

jstat: 虚拟机统计信息监视工具

JVM Statistics Monitoring Tool, 用于监视虚拟机各种运行状态信息; 可以显示本地或远程虚拟机进程中的类装载, 内存, 垃圾收集, JIT编译等运行数据, 是运行期定位虚拟机性能问题的首选工具.

命令格式: jstat [option vmid [interval[s|ms]] count[]]
option如下表
vmid若为本地虚拟机, vmid=lvmid;
vmid若为远程虚拟机, 格式为: [protocol:] [//]lvmid[@hostname[:port]/servername]
interval表示查询间隔
count表示查询次数, interval和count不写, 说明只查一次
image.png
测试:
参数表示查询11384的虚拟机进程, 间隔1s查一次, 共查3次

  1. C:\Users\Administrator>jstat -gc 11384 1000 3
  2. S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
  3. 12288.0 15360.0 0.0 0.0 186368.0 142285.2 136704.0 19163.2 35496.0 33769.5 4776.0 4454.9 6 0.063 2 0.079 0.142
  4. 12288.0 15360.0 0.0 0.0 186368.0 142285.2 136704.0 19163.2 35496.0 33769.5 4776.0 4454.9 6 0.063 2 0.079 0.142
  5. 12288.0 15360.0 0.0 0.0 186368.0 142285.2 136704.0 19163.2 35496.0 33769.5 4776.0 4454.9 6 0.063 2 0.079 0.142

jinfo: Java配置信息工具

Configuration Info For Java, 实时的查看和调整虚拟机参数

命令格式: jinfo [option] pid
jinfo -flag [+/-name]或 -flag name=value, 可以修改一部分运行期可写的虚拟机参数值
jinfo -flag name pid 可以查看参数
测试:

  1. C:\Users\Administrator>jinfo -flag CMSInitiatingOccupancyFraction 11384
  2. -XX:CMSInitiatingOccupancyFraction=-1

jmap: Java内存映像工具

Memory Map For Java, 用于生成堆转储快照文件(headdump/dump文件). 还可以查询finalize执行队列, Java堆和永久代的详细信息, 如空间使用率, 当前使用的哪种收集器等

命令格式: jmap [option] vmid
image.png
测试:

  1. C:\Users\Administrator>jmap -dump:format=b,file=test20200209.bin 11384
  2. Dumping heap to C:\Users\Administrator\test20200209.bin ...
  3. Heap dump file created

jhat: 虚拟机堆转储快照分析工具

JVM Heap Analysis Tool, 用来分析jmap生成的堆转储快照.

命令格式: jhat [堆转储快照]
测试:

  1. C:\Users\Administrator>jhat test20200209.bin
  2. Reading from test20200209.bin...
  3. Dump file created Sun Feb 09 16:52:39 CST 2020
  4. Snapshot read, resolving...
  5. Resolving 1922879 objects...
  6. Chasing references, expect 384 dots................................................................................................................................................................................................................................................................................................................................................................................................
  7. Eliminating duplicate references................................................................................................................................................................................................................................................................................................................................................................................................
  8. Snapshot resolved.
  9. Started HTTP server on port 7000
  10. Server is ready.

然后访问http://localhost:7000/
image.png

jstack: Java堆栈跟踪工具

Stack Trace for Java, 用于生成虚拟机当前时刻的线程快照(threaddump/javacore文件).
线程快照就是当前虚拟机中每一条线程正在执行的方法的堆栈的集合, 生成线程快照的目的是定位线程出现长时间停顿的原因, 如线程间死锁, 死循环, 请求外部资源导致的长时间等待.

命令格式: jstack [option] vimd
image.png

JDK的可视化工具

JConsole: Java监视与管理控制台

在C:\Program Files\Java\jdk1.8.0_221\bin找到jconsole.exe, 双击打开image.png

VisualVM: 多合一故障处理工具

用来运行监视, 故障处理, 性能分析
应该是这个jvisualvm.exe
image.png