JVM 常用的命令行工具

命令基于Sun JDK,用于监控和诊断HotSpot的java 虚拟机

1. jps 虚拟机进程状况工具

格式:jps [-q] [-mlvV] []
解释

   -l        输出主类全名。如果进程执行的是jar包,则输出包名。
   -q        只输出LVMID,同进程pid
   -m        输出JVM启动时传给主类main()的参数。
   -v        输出JVM启动时JVM参数。
   -V        输出通过标记的文件传递给JVM的参数(.hotspotrc文件,或者是通过参数-XX:Flags=指定的文件)

示例

bogon:~ hzjdemac$ jps
4081 org.eclipse.equinox.launcher_1.5.100.v20180827-1352.jar
14356 Launcher
14357 OutOfMemoryTest
14391 Jps
1290
12619 SSHMain
3453 RemoteMavenServer
bogon:~ hzjdemac$ jps -l
4081 /Users/hzjdemac/.vscode/extensions/redhat.java-0.30.0/server/plugins/org.eclipse.equinox.launcher_1.5.100.v20180827-1352.jar
14356 org.jetbrains.jps.cmdline.Launcher
14357 chapter2.OutOfMemoryTest
1290
14394 sun.tools.jps.Jps
12619 org.jetbrains.git4idea.ssh.SSHMain
3453 org.jetbrains.idea.maven.server.RemoteMavenServer
bogon:~ hzjdemac$ jps -v
4081 org.eclipse.equinox.launcher_1.5.100.v20180827-1352.jar -Declipse.application=org.eclipse.jdt.ls.core.id1 -Dosgi.bundles.defaultStartLevel=4 -Declipse.product=org.eclipse.jdt.ls.core.product -Xverify:none -Xmx1G -XX:+UseG1GC -XX:+UseStringDeduplication
14356 Launcher -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs="" -Djdt.compiler.useSingleThread=true -Dpreload.project.path=/Users/hzjdemac/IdeaProjects/java-learning -Dpreload.config.path=/Users/hzjdemac/Library/Preferences/IntelliJIdea2017.3/options -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djava.net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=-8492196048993142940 -Dfile.encoding=UTF-8 -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2017.3 -Didea.home.path=/Applications/IntelliJ IDEA.app/Contents -Didea.config.path=/Users/hzjdemac/Library/Preferences/IntelliJIdea2017.3 -Didea.plugins.path=/Users/hzjdemac/Library/Application Support/IntelliJIdea2017.3 -Djps.log.dir=/Users/hzjdemac/Library/Logs/IntelliJIdea2017.3/build-log -Djps.fallback.jdk.home=/Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre -Djps.fallback.jdk.version=1.8.0_152-release -Dio.netty.noUnsafe=true -Djava.io.tmpdir=/Users/hzjdemac/Library/Caches/IntelliJIdea2017.3/compile-server/java-l
1290  -Xms1024m -Xmx2048m -XX:ReservedCodeCacheSize=240m -XX:+UseCompressedOops -Dfile.encoding=UTF-8 -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Dsun.io.useCanonCaches=false -Djava.net.preferIPv4Stack=true -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Xverify:none -XX:ErrorFile=/Users/hzjdemac/java_error_in_idea_%p.log -XX:HeapDumpPath=/Users/hzjdemac/java_error_in_idea.hprof -Xbootclasspath/a:../lib/boot.jar -Djb.vmOptionsFile=/Applications/IntelliJ IDEA.app/Contents/bin/idea.vmoptions -Xbootclasspath/a:/Applications/IntelliJ IDEA.app/Contents/lib/boot.jar -Didea.java.redist=jdk-bundled -Didea.home.path=/Applications/IntelliJ IDEA.app/Contents -Didea.executable=idea -Didea.paths.selector=IntelliJIdea2017.3
12619 SSHMain
14397 Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home -Xms8m
3453 RemoteMavenServer -Djava.awt.headless=true -Didea.version==2017.3.1 -Xmx768m -Didea.maven.embedder.version=3.3.9 -Dfile.encoding=UTF-8

2. jinfo 实时地查看和调整虚拟机各项参数

格式:

bogon:~ hzjdemac$ jinfo -help
Usage:
    jinfo [option] 
        (to connect to running process)
    jinfo [option] 
        (to connect to a core file)
    jinfo [option] [server_id@]
        (to connect to remote debug server)

where 

示例

bogon:~ hzjdemac$ jinfo -flags 15612
Attaching to process ID 15612, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.101-b13
Non-default VM flags: -XX:CICompilerCount=4 -XX:+HeapDumpOnOutOfMemoryError -XX:InitialHeapSize=83886080 -XX:MaxHeapSize=83886080 -XX:MaxNewSize=27787264 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=27787264 -XX:OldSize=56098816 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
Command line:  -Xms80m -Xmx80m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

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

bogon:~ hzjdemac$ jstat -help
Usage: jstat -help|-options
       jstat -

示例

bogon:~ hzjdemac$ jstat -gc 16229 2000   5   //每2s执行一次查询统计,执行5次gc统计输出
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
10752.0 10752.0  0.0    0.0   65536.0   3932.6   175104.0     0.0     4480.0 774.0  384.0   75.8       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   3932.6   175104.0     0.0     4480.0 774.0  384.0   75.8       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   3932.6   175104.0     0.0     4480.0 774.0  384.0   75.8       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   3932.6   175104.0     0.0     4480.0 774.0  384.0   75.8       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   3932.6   175104.0     0.0     4480.0 774.0  384.0   75.8       0    0.000   0      0.000    0.000
bogon:~ hzjdemac$ jstat -class 16229
Loaded  Bytes  Unloaded  Bytes     Time
   421   868.0        0     0.0       0.05

S0C: 新生代中Survivor space 0区总空间
S1C: 新生代中Survivor space 1区总空间
S0U:新生代中Survivor space 0区已使用空间
S1U:新生代中Survivor space 1区已使用空间
EC: 新生代中总空间
EU:新生代已使用空间
OC: 老年代总空间
OU:老年代中已使用空间
MC:方法区大小
MU:方法区使用大小
CCSC:压缩类空间大小
CCSU:压缩类空间使用大小
YGC: 从应用程序启动到当前,发生Yang GC 的次数
YGCT: 从应用程序启动到当前,Yang GC所用的时间【单位秒】
FGC: 从应用程序启动到当前,发生Full GC的次数
FGCT: 从应用程序启动到当前,Full GC所用的时间
GCT: 从应用程序启动到当前,用于垃圾回收的总时间【单位秒】

4. jmap 打印指定Java进程(或核心文件、远程调试服务器)的共享对象内存映射或堆内存细节

bogon:~ hzjdemac$ jmap -help
Usage:
    jmap [option] 
        (to connect to running process)
    jmap [option] 
        (to connect to a core file)
    jmap [option] [server_id@]
        (to connect to remote debug server)

where 

示例

bogon:~ hzjdemac$ jmap -heap 16229
Attaching to process ID 16229, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.101-b13

using thread-local object allocation.
Parallel GC with 8 thread(s)

Heap Configuration:   //#堆内存初始化配置
   MinHeapFreeRatio         = 0  //-XX:MinHeapFreeRatio设置JVM堆最小空闲比率  
   MaxHeapFreeRatio         = 100   //-XX:MaxHeapFreeRatio设置JVM堆最大空闲比率 
   MaxHeapSize              = 4294967296 (4096.0MB)  //-XX:MaxHeapSize=设置JVM堆的最大大小
   NewSize                  = 89128960 (85.0MB)   //-XX:NewSize=设置JVM堆新生代的默认大小
   MaxNewSize               = 1431306240 (1365.0MB)  //-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
   OldSize                  = 179306496 (171.0MB)  //-XX:OldSize=设置JVM堆的‘老生代’的大小
   NewRatio                 = 2     //#-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
   SurvivorRatio            = 8   //-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值
   MetaspaceSize            = 21807104 (20.796875MB)  //-XX:PermSize=:设置JVM堆的‘持久代’的初始大小  
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 67108864 (64.0MB)
   used     = 4026944 (3.84039306640625MB)
   free     = 63081920 (60.15960693359375MB)
   6.000614166259766% used
From Space:
   capacity = 11010048 (10.5MB)
   used     = 0 (0.0MB)
   free     = 11010048 (10.5MB)
   0.0% used
To Space:
   capacity = 11010048 (10.5MB)
   used     = 0 (0.0MB)
   free     = 11010048 (10.5MB)
   0.0% used
PS Old Generation
   capacity = 179306496 (171.0MB)
   used     = 0 (0.0MB)
   free     = 179306496 (171.0MB)
   0.0% used

758 interned Strings occupying 50832 bytes.
bogon:~ hzjdemac$ jmap -histo 16229

 num     #instances         #bytes  class name
----------------------------------------------
   1:           439        4642832  [I
   2:          2553         347560  [C
   3:           731         145760  [B
   4:           487          55504  java.lang.Class
   5:          1699          40776  java.lang.String
   6:           584          30504  [Ljava.lang.Object;
   7:           115           8280  java.lang.reflect.Field
   8:           199           6368  java.io.File
   9:           238           5712  java.lang.StringBuilder
  10:            89           5696  java.net.URL
  11:            11           4136  java.lang.Thread
  12:           258           4128  java.lang.Integer
  13:            98           3920  java.lang.ref.SoftReference
  14:           122           3904  java.util.Hashtable$Entry
...
bogon:~ hzjdemac$ jmap  -dump:format=b,file=a.log 16229   //保存快照
Dumping heap to /Users/hzjdemac/a.log ...
Heap dump file created

配置JVM启动参数,让JVM在遇到OutOfMemoryError时自动生成Dump文件

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path

5. jstack 堆栈跟踪工具

bogon:~ hzjdemac$ jstack -help
Usage:
    jstack [-l] 
        (to connect to running process)
    jstack -F [-m] [-l] 
        (to connect to a hung process)
    jstack [-m] [-l]  
        (to connect to a core file)
    jstack [-m] [-l] [server_id@]
        (to connect to a remote debug server)

Options:
    -F  to force a thread dump. Use when jstack  does not respond (process is hung)
    -m  to print both java and native frames (mixed mode)
    -l  long listing. Prints additional information about locks
    -h or -help to print this help message

示例

bogon:~ hzjdemac$ jstack 16229
2019-03-18 18:27:04
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode):

"Attach Listener" #13 daemon prio=9 os_prio=31 tid=0x00007fa6c5001000 nid=0xc07 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"DestroyJavaVM" #12 prio=5 os_prio=31 tid=0x00007fa6c589f000 nid=0x1803 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Thread-1" #11 prio=5 os_prio=31 tid=0x00007fa6c589e000 nid=0x5603 waiting for monitor entry [0x000070000c031000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at chapter1.DeadLockDemo$2.run(DeadLockDemo.java:38)
    - waiting to lock <0x000000076abaccd8> (a java.lang.String)
    - locked <0x000000076abacd08> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:745)

"Thread-0" #10 prio=5 os_prio=31 tid=0x00007fa6c589d800 nid=0xa903 waiting for monitor entry [0x000070000bf2e000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at chapter1.DeadLockDemo$1.run(DeadLockDemo.java:28)
    - waiting to lock <0x000000076abacd08> (a java.lang.String)
    - locked <0x000000076abaccd8> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:745)
...

参考:
[1].https://www.cnblogs.com/dennisit/p/9119535.html
[2]. 深入理解Java虚拟机

你可能感兴趣的:(JVM 常用的命令行工具)