JVM 常用的命令行工具

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

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

格式:jps [-q] [-mlvV] [<hostid>]
解释

   -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] <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// 打印给定name对应的命令行参数值。
    -flag [+|-]<name>    to enable or disable the named VM flag //更改boolean类型的命令行参数值
    -flag <name>=<value> to set the named VM flag to the given value //更改name对应的命令行参数值为value。
    -flags               to print VM flags          //打印传给JVM的命令参数值
    -sysprops            to print Java system properties  //打印系统属性值
    <no option>          to print both of the above
    -h | -help           to print this help message

示例

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 -<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.
bogon:~ hzjdemac$ jstat -options
-class          //监视类装载、卸载数量、总空间以及类装载所耗费的时间.
-compiler       //显示编译器编译过的方法、耗时等信息.
-gc             //显示Java堆状况、包括Eden区、两个survivor区、老年代、永久代等的容量、已用空间、GC次数以及时间等信息.
-gccapacity     //显示VM内存中三代(young,old,perm)对象的使用和占用大小
-gcutil         //gc统计信息, 输出主要关注已使用空间占总弓箭的百分比
-gccause        //与-gcutil基本相同, 会额外输出导致上一次GC产生的原因
-gcnew          //显示新生代GC状况
-gcnewcapacity  //显示内容与-gcnew相同,输出主要关注使用到的最大最小空间
-gcold          // 显示老年代GC状况
-gcoldcapacity  //显示内容与-gcold相同,输出主要关注使用到的最大最小空间
-printcompilation //当前vm执行的信息

示例

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] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary   //打印jvm heap的情况,例如GC收集器,参数配置,JVM内存分区情况
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects  //打印jvm heap的直方图。其输出信息包括类名,对象数量,对象占用大小
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

示例

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=<value>:设置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] <pid>
        (to connect to running process)
    jstack -F [-m] [-l] <pid>
        (to connect to a hung process)
    jstack [-m] [-l] <executable> <core>
        (to connect to a core file)
    jstack [-m] [-l] [server_id@]<remote server IP or hostname>
        (to connect to a remote debug server)

Options:
    -F  to force a thread dump. Use when jstack <pid> 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虚拟机

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269