JVM命令行调优工具介绍

x33g5p2x  于2021-12-18 转载在 其他  
字(13.6k)|赞(0)|评价(0)|浏览(904)

JVM命令行调优工具介绍

jps虚拟机进程状况工具

jps(JVM Process Status Tool):列出正在运行的虚拟机进程,并显示虚拟机执行主类(Main Class,main()函数所在的类)名称以及这些进程的本地虚拟机唯一ID(Local Virtual Machine Identifier,LVMID)。

虽然功能比较单一,但它是使用频率最高的JDK命令行工具,因为其他的JDK工具大多需要输入它查询到的LVMID来确定要监控的是哪一个虚拟机进程。

对于本地虚拟机进程来说,LVMID与操作系统的进程ID(Process Identifier,PID)是一致的,使用Windows的任务管理器或者UNIX的ps命令也可以查询到虚拟机进程的LVMID,但如果同时启动了多个虚拟机进程,无法根据进程名称定位时,那就只能依赖jps命令显示主类的功能才能区分了。

命令格式:

  1. usage: jps [-help]
  2. jps [-q] [-mlvV] [<hostid>]
  3. Definitions:
  4. <hostid>: <hostname>[:<port>]

option参数:

  • -q:只打印LVMID。
  • -m:打印JVM启动时传递给main()的参数。
  • -l:打印主类全名或jar路径。
  • -v:打印JVM启动时显示指定的JVM参数。
  • -V:打印通过flag文件传递到JVM中的参数(.hotspotrc文件或-XX:Flags=所指定的文件)。

示例:

  1. $ jps -l
  2. 13560 sun.tools.jps.Jps
  3. 8940 org.jetbrains.jps.cmdline.Launcher

jinfo虚拟机配置信息工具

jinfo(JVM Configuration Info):实时查看和调整虚拟机运行参数。

命令格式:

  1. Usage:
  2. jinfo [option] <pid>
  3. (to connect to running process)
  4. jinfo [option] <executable <core>
  5. (to connect to a core file)
  6. jinfo [option] [server_id@]<remote server IP or hostname>
  7. (to connect to remote debug server)
  8. where <option> is one of:
  9. -flag <name> to print the value of the named VM flag
  10. -flag [+|-]<name> to enable or disable the named VM flag
  11. -flag <name>=<value> to set the named VM flag to the given value
  12. -flags to print VM flags
  13. -sysprops to print Java system properties
  14. <no option> to print both of the above
  15. -h | -help to print this help message

option参数:

  • -flag name:打印对应名称的参数。
  • -flag [+|-]name:开启或者关闭对应名称的参数。
  • -flag name=value:设定对应名称的参数。
  • -flags:打印全部的参数。
  • -sysprops:打印系统属性。
  • no option:打印全部的参数和系统属性。

示例:

  1. $ jinfo -flags 8940
  2. Attaching to process ID 8940, please wait...
  3. Debugger attached successfully.
  4. Server compiler detected.
  5. JVM version is 25.172-b11
  6. Non-default VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=132120576 -XX:MaxHeapSize=734003200 -XX:MaxNewSize=244318208 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=44040192 -XX:OldSize=88080384 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndi
  7. vidualAllocation -XX:+UseParallelGC
  8. Command line: -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs="" -Djdt.compiler.useSingleThread=true -Dpreload.project.path=D:/gitPrj/morris-book/后端开发/Java/JVM -Dpreload.config.path=C:/Users/wj65651/.IntelliJIdea2018.1/config/options -Dexternal.project.config=C:\Users\wj65651\.IntelliJIdea201
  9. 8.1\system\external_build_system\jvm.664992bf -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djava.net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=-6591130954637100000 -Dfile.encoding=GBK -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2018.1 -Didea.home.pa
  10. th=D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4 -Didea.config.path=C:\Users\wj65651\.IntelliJIdea2018.1\config -Didea.plugins.path=C:\Users\wj65651\.IntelliJIdea2018.1\config\plugins -Djps.log.dir=C:/Users/wj65651/.IntelliJIdea2018.1/system/log/build-log -Djps.fallback.jdk.home=D:/Program Files/JetB
  11. rains/IntelliJ IDEA 2018.1.4/jre64 -Djps.fallback.jdk.version=1.8.0_152-release -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/wj65651/.IntelliJIdea2018.1/system/compile-server/jvm_30d198c8/_temp_ -Djps.backward.ref.index.builder=true

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

jstat(JVM Statistics Monitoring):用于监视虚拟机运行时状态信息的命令,它可以显示出虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据。

命令格式:

  1. Usage: jstat -help|-options
  2. jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
  3. Definitions:
  4. <option> An option reported by the -options option
  5. <vmid> Virtual Machine Identifier. A vmid takes the following form:
  6. <lvmid>[@<hostname>[:<port>]]
  7. Where <lvmid> is the local vm identifier for the target
  8. Java virtual machine, typically a process id; <hostname> is
  9. the name of the host running the target Java virtual machine;
  10. and <port> is the port number for the rmiregistry on the
  11. target host. See the jvmstat documentation for a more complete
  12. description of the Virtual Machine Identifier.
  13. <lines> Number of samples between header lines.
  14. <interval> Sampling interval. The following forms are allowed:
  15. <n>["ms"|"s"]
  16. Where <n> is an integer and the suffix specifies the units as
  17. milliseconds("ms") or seconds("s"). The default units are "ms".
  18. <count> Number of samples to take before terminating.
  19. -J<flag> Pass <flag> directly to the runtime system.

参数:

  • option:打印参数选项。
  • -t:可以在打印的列加上Timestamp列,用于显示系统运行的时间。
  • vmid:Virtual Machine ID(进程的pid)。
  • -h:可以在周期性数据数据的时候,可以在指定输出多少行以后输出一次表头。
  • interval:执行每次的间隔时间,单位为毫秒。
  • count:用于指定输出多少次记录,缺省则会一直打印。

options参数:

  1. $ jstat -options
  2. -class
  3. -compiler
  4. -gc
  5. -gccapacity
  6. -gccause
  7. -gcmetacapacity
  8. -gcnew
  9. -gcnewcapacity
  10. -gcold
  11. -gcoldcapacity
  12. -gcutil
  13. -printcompilation

-class

类加载的行为统计,监视类装载、卸载数量、总空间以及耗费的时间。

示例:

  1. $ jstat -class 8940
  2. Loaded Bytes Unloaded Bytes Time
  3. 3152 5933.9 0 0.0 0.87
  • Loaded:加载class的数量。
  • Bytes:class字节大小。
  • Unloaded:未加载class的数量。
  • Bytes:未加载class的字节大小。
  • Time:加载时间。

-compiler

JIT编译器行为统计,输出JIT编译过的方法数量耗时等。

示例:

  1. $ jstat -compiler 8940
  2. Compiled Failed Invalid Time FailedType FailedMethod
  3. 1075 0 0 0.89 0
  • Compiled:编译数量。
  • Failed:编译失败数量。
  • Invalid:无效数量。
  • Time:编译耗时。
  • FailedType:失败类型。
  • FailedMethod:失败方法的全限定名。

-gc

垃圾回收堆的行为统计。

示例:

  1. $ jstat -gc 8940 250 3
  2. S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
  3. 5120.0 5120.0 0.0 4439.7 32768.0 31665.7 86016.0 16464.0 13696.0 13279.9 1664.0 1584.6 1 0.004 0 0.000 0.004
  4. 5120.0 5120.0 0.0 4439.7 32768.0 31665.7 86016.0 16464.0 13696.0 13279.9 1664.0 1584.6 1 0.004 0 0.000 0.004
  5. 5120.0 5120.0 0.0 4439.7 32768.0 31665.7 86016.0 16464.0 13696.0 13279.9 1664.0 1584.6 1 0.004 0 0.000 0.004

C即Capacity 总容量,U即Used 已使用的容量,单位为KB。

  • S0C:survivor0区的总容量。
  • S1C:survivor1区的总容量。
  • S0U:survivor0区已使用的容量。
  • S1C:survivor1区已使用的容量。
  • EC:Eden区的总容量。
  • EU:Eden区已使用的容量。
  • OC:Old区的总容量。
  • OU:Old区已使用的容量。
  • MC:方法区的总容量。
  • MU:方法区已使用的容量。
  • CCSC:压缩类空间大小。
  • CCSU:压缩类空间使用大小。
  • YGC:新生代垃圾回收次数。
  • YGCT:新生代垃圾回收消耗时间。
  • FGC:老年代垃圾回收次数。
  • FGCT:老年代垃圾回收消耗时间。
  • GCT:垃圾回收消耗总时间。

-gccapacity

各个垃圾回收代容量和他们相应的空间统计。

示例:

  1. $ jstat -gccapacity 8940
  2. NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
  3. 43008.0 238592.0 43008.0 5120.0 5120.0 32768.0 86016.0 478208.0 86016.0 86016.0 0.0 1060864.0 13696.0 0.0 1048576.0 1664.0 1 0
  • NGCMN:新生代使用到的最小容量。
  • NGCMX:新生代使用到的最大容量。
  • NGC:当前新生代容量。
  • S0C:survivor0区的总容量。
  • S1C:survivor1区的总容量。
  • EC:Eden区的总容量。
  • OGCMN:老年代使用到的最小容量。
  • OGCMX:老年代使用到的最大容量。
  • OGC:当前老年代大小。
  • OC:当前老年代大小。
  • MCMN:元数据使用到的最小容量。
  • MCMX:元数据使用到的最大容量。
  • MC:当前元数据空间大小。
  • CCSMN:最小压缩类空间大小。
  • CCSMX:最大压缩类空间大小。
  • CCSC:当前压缩类空间大小。
  • YGC:年轻代GC次数。
  • FGC:老年代GC次数。

-gcnew

新生代垃圾回收统计。

示例:

  1. $ jstat -gcnew 8940
  2. S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
  3. 5120.0 5120.0 0.0 4439.7 7 15 5120.0 32768.0 31665.7 1 0.004
  • S0C:survivor0区的总容量
  • S1C:survivor1区的总容量
  • S0U:survivor0区已使用的容量
  • S1C:survivor1区已使用的容量
  • TT:对象在新生代存活的次数
  • MTT:对象在新生代存活的最大次数
  • DSS:期望的survivor大小
  • EC:Eden区的总容量
  • EU:Eden区已使用的容量
  • YGC:新生代垃圾回收次数
  • YGCT:新生代垃圾回收消耗时间

-gcnewcapacity

新生代与其相应的内存空间的统计。

示例:

  1. $ jstat -gcnewcapacity 8940
  2. NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC
  3. 43008.0 238592.0 43008.0 79360.0 5120.0 79360.0 5120.0 237568.0 32768.0 1 0
  • NGCMN:新生代使用到的最小容量。
  • NGCMX:新生代使用到的最大容量。
  • NGC:当前新生代容量。
  • S0CMX:survivor0区使用到的最大容量。
  • S0C:当前survivor0区大小。
  • S1CMX:survivor1区使用到的最大容量。
  • S1C:当前survivor1区大小。
  • ECMX:最大Eden区大小。
  • EC:Eden区的总容量。
  • YGC:新生代垃圾回收次数。
  • FGC:老年代回收次数。

-gcold

老年代垃圾回收统计。

示例

  1. $ jstat -gcold 8940
  2. MC MU CCSC CCSU OC OU YGC FGC FGCT GCT
  3. 13696.0 13279.9 1664.0 1584.6 86016.0 16464.0 1 0 0.000 0.004
  • MC:方法区大小。
  • MU:方法区使用大小。
  • CCSC:压缩类空间大小。
  • CCSU:压缩类空间使用大小。
  • OC:老年代大小。
  • OU:老年代使用大小。
  • YGC:年轻代垃圾回收次数。
  • FGC:老年代垃圾回收次数。
  • FGCT:老年代垃圾回收消耗时间。
  • GCT:垃圾回收消耗总时间。

-gcoldcapacity

老生代与其相应的内存空间的统计。

示例:

  1. $ jstat -gcoldcapacity 8940
  2. OGCMN OGCMX OGC OC YGC FGC FGCT GCT
  3. 86016.0 478208.0 86016.0 86016.0 1 0 0.000 0.004
  • OGCMN:老年代最小容量。
  • OGCMX:老年代最大容量。
  • OGC:当前老年代大小。
  • OC:老年代大小。
  • YGC:年轻代垃圾回收次数。
  • FGC:老年代垃圾回收次数。
  • FGCT:老年代垃圾回收消耗时间。
  • GCT:垃圾回收消耗总时间。

-gcmetacapacity

元数据空间统计。

示例:

  1. $ jstat -gcmetacapacity 8940
  2. MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC FGCT GCT
  3. 0.0 1060864.0 13696.0 0.0 1048576.0 1664.0 1 0 0.000 0.004
  • MCMN:最小元数据容量。
  • MCMX:最大元数据容量。
  • MC:当前元数据空间大小。
  • CCSMN:最小压缩类空间大小。
  • CCSMX:最大压缩类空间大小。
  • CCSC:当前压缩类空间大小。
  • YGC:年轻代垃圾回收次数。
  • FGC:老年代垃圾回收次数。
  • FGCT:老年代垃圾回收消耗时间。
  • GCT:垃圾回收消耗总时间。

-gcutil

圾收集统计概述(百分比)。

示例:

  1. $ jstat -gcutil 8940
  2. S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
  3. 0.00 86.71 96.64 19.14 96.96 95.23 1 0.004 0 0.000 0.004
  • S0:幸存1区当前使用比例。
  • S1:幸存2区当前使用比例。
  • E:伊甸园区使用比例。
  • O:老年代使用比例。
  • M:元数据区使用比例。
  • CCS:压缩使用比例。
  • YGC:年轻代垃圾回收次数。
  • FGC:老年代垃圾回收次数。
  • FGCT:老年代垃圾回收消耗时间。
  • GCT:垃圾回收消耗总时间。

-gccause

垃圾收集统计概述(同-gcutil),附加最近两次垃圾回收事件的原因。

  1. $ jstat -gccause 13116
  2. S0 S1 E O M CCS YGC YGCT FGC FGCT GCT LGCC GCC
  3. 58.73 0.00 82.00 47.46 81.01 85.00 18 2.539 8 1.685 4.224 Allocation Failure No GC

-printcompilation

输出JIT编译的方法信息。

  1. $ jstat -printcompilation 13116
  2. Compiled Size Type Method
  3. 83 43 1 java/nio/Buffer position

jmap:Java内存映像工具

jmap(Memory Map for Java):用于生成堆转储快照(一般称为heapdump或dump文件)。

jmap的作用并不仅仅是为了获取dump文件,它还可以查询finalize执行队列、Java堆和永久代的详细信息,如空间使用率、当前用的是哪种收集器等。和jinfo命令一样,jmap有不少功能在Windows平台下都是受限的,除了生成dump文件的-dump选项和用于查看每个类的实例、空间占用统计的-histo选项在所有操作系统都提供之外,其余选项都只能在Linux/Solaris下使用。

命令格式:

  1. Usage:
  2. jmap [option] <pid>
  3. (to connect to running process)
  4. jmap [option] <executable <core>
  5. (to connect to a core file)
  6. jmap [option] [server_id@]<remote server IP or hostname>
  7. (to connect to remote debug server)
  8. where <option> is one of:
  9. <none> to print same info as Solaris pmap
  10. -heap to print java heap summary
  11. -histo[:live] to print histogram of java object heap; if the "live"
  12. suboption is specified, only count live objects
  13. -clstats to print class loader statistics
  14. -finalizerinfo to print information on objects awaiting finalization
  15. -dump:<dump-options> to dump java heap in hprof binary format
  16. dump-options:
  17. live dump only live objects; if not specified,
  18. all objects in the heap are dumped.
  19. format=b binary format
  20. file=<file> dump heap to <file>
  21. Example: jmap -dump:live,format=b,file=heap.bin <pid>
  22. -F force. Use with -dump:<dump-options> <pid> or -histo
  23. to force a heap dump or histogram when <pid> does not
  24. respond. The "live" suboption is not supported
  25. in this mode.
  26. -h | -help to print this help message
  27. -J<flag> to pass <flag> directly to the runtime system

option参数:

  • dump:生成堆转储快照,格式为:-dump:[live, ]format=b,file=,其中live子参数说明是否只dump出存活的对象。
  • finalizerinfo:显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
  • heap:显示Java堆详细信息。
  • histo:显示堆中对象的统计信息,GC使用的算法,heap的配置及wise。heap的使用情况,可以用此来判断内存目前的使用情况以及垃圾回收情况。
  • F:当-dump没有响应时,强制生成dump快照。

示例:

  1. $ jmap -heap 8940
  2. Attaching to process ID 8940, please wait...
  3. Debugger attached successfully.
  4. Server compiler detected.
  5. JVM version is 25.172-b11
  6. using thread-local object allocation.
  7. Parallel GC with 4 thread(s)
  8. Heap Configuration:
  9. MinHeapFreeRatio = 0
  10. MaxHeapFreeRatio = 100
  11. MaxHeapSize = 734003200 (700.0MB)
  12. NewSize = 44040192 (42.0MB)
  13. MaxNewSize = 244318208 (233.0MB)
  14. OldSize = 88080384 (84.0MB)
  15. NewRatio = 2
  16. SurvivorRatio = 8
  17. MetaspaceSize = 21807104 (20.796875MB)
  18. CompressedClassSpaceSize = 1073741824 (1024.0MB)
  19. MaxMetaspaceSize = 17592186044415 MB
  20. G1HeapRegionSize = 0 (0.0MB)
  21. Heap Usage:
  22. PS Young Generation
  23. Eden Space:
  24. capacity = 33554432 (32.0MB)
  25. used = 32425712 (30.923568725585938MB)
  26. free = 1128720 (1.0764312744140625MB)
  27. 96.63615226745605% used
  28. From Space:
  29. capacity = 5242880 (5.0MB)
  30. used = 4546224 (4.3356170654296875MB)
  31. free = 696656 (0.6643829345703125MB)
  32. 86.71234130859375% used
  33. To Space:
  34. capacity = 5242880 (5.0MB)
  35. used = 0 (0.0MB)
  36. free = 5242880 (5.0MB)
  37. 0.0% used
  38. PS Old Generation
  39. capacity = 88080384 (84.0MB)
  40. used = 16859168 (16.078155517578125MB)
  41. free = 71221216 (67.92184448242188MB)
  42. 19.14066133045015% used
  43. 5329 interned Strings occupying 483136 bytes.

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

jhat(JVM Heap Analysis Tool)命令与jmap搭配使用,来分析jmap生成的堆转储快照。jhat内置了一个微型的HTTP/HTML服务器,生成dump文件的分析结果后,可以在浏览器中查看。

jhat的分析功能相对来说比较简陋,VisualVM以及专业用于分析dump文件的Eclipse Memory Analyzer、IBM HeapAnalyzer等工具,都能实现比jhat更强大更专业的分析功能。

命令格式:

  1. $ jhat -help
  2. Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>
  3. -J<flag> Pass <flag> directly to the runtime system. For
  4. example, -J-mx512m to use a maximum heap size of 512MB
  5. -stack false: Turn off tracking object allocation call stack.
  6. -refs false: Turn off tracking of references to objects
  7. -port <port>: Set the port for the HTTP server. Defaults to 7000
  8. -exclude <file>: Specify a file that lists data members that should
  9. be excluded from the reachableFrom query.
  10. -baseline <file>: Specify a baseline object dump. Objects in
  11. both heap dumps with the same ID and same class will
  12. be marked as not being "new".
  13. -debug <int>: Set debug level.
  14. 0: No debug output
  15. 1: Debug hprof file parsing
  16. 2: Debug hprof file parsing, no server
  17. -version Report version number
  18. -h|-help Print this help and exit
  19. <file> The file to read
  20. For a dump file that contains multiple heap dumps,
  21. you may specify which dump in the file
  22. by appending "#<number>" to the file name, i.e. "foo.hprof#3".
  23. All boolean options default to "true"

jstack:Java堆栈跟踪工具

jstack(Stack Trace for Java):用于生成虚拟机当前时刻的线程快照(一般称为threaddump或者javacore文件)。线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待等都是导致线程长时间停顿的常见原因。线程出现停顿的时候通过jstack来查看各个线程的调用堆栈,就可以知道没有响应的线程到底在后台做些什么事情,或者等待着什么资源。

命令格式:

  1. $ jstack -help
  2. Usage:
  3. jstack [-l] <pid>
  4. (to connect to running process)
  5. jstack -F [-m] [-l] <pid>
  6. (to connect to a hung process)
  7. jstack [-m] [-l] <executable> <core>
  8. (to connect to a core file)
  9. jstack [-m] [-l] [server_id@]<remote server IP or hostname>
  10. (to connect to a remote debug server)
  11. Options:
  12. -F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
  13. -m to print both java and native frames (mixed mode)
  14. -l long listing. Prints additional information about locks
  15. -h or -help to print this help message

option参数:

  • -F:当正常输出请求不被响应时,强制输出线程堆栈。
  • -l:除堆栈外,显示关于锁的附加信息。
  • -m:如果调用到本地方法的话,可以显示C/C++的堆栈。

示例:

  1. $ jstack -l 8940
  2. 2018-12-04 15:50:27
  3. Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.172-b11 mixed mode):
  4. "NettythreadDeathWatcher-2-1" #14 daemon prio=1 os_prio=-2 tid=0x00000000591e6000 nid=0x1e30 waiting on condition [0x000000005b16f000]
  5. java.lang.Thread.State: TIMED_WAITING (sleeping)
  6. at java.lang.Thread.sleep(Native Method)
  7. at io.netty.util.ThreadDeathWatcher$Watcher.run(ThreadDeathWatcher.java:152)
  8. at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
  9. at java.lang.Thread.run(Thread.java:748)
  10. Locked ownable synchronizers:
  11. - None
  12. ... ...
  13. "VM Thread" os_prio=2 tid=0x0000000054e84000 nid=0x37ac runnable
  14. "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002a03800 nid=0x4200 runnable
  15. "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002a05000 nid=0x3b48 runnable
  16. "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002a07000 nid=0x3a90 runnable
  17. "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002a08800 nid=0x2654 runnable
  18. "VM Periodic Task Thread" os_prio=2 tid=0x00000000563c2800 nid=0x4208 waiting on condition
  19. JNI global references: 255

相关文章