---
title: "JVM6个常见指令介绍"
author: "sharefling"
date: "2014-05-21T15:55:16Z"
tags: []
categories: ["编程语言"]
slug: "jvm6"
draft: false
lastmod: "2014-05-21T15:55:16Z"
id: 2069205
url: "https://sharefling.iteye.com/blog/2069205"
---

jps 虚拟机进程状况工具 罗列所有进程

jstat 虚拟机统计信息工具进程jstat -gc uid times time（次数）

jinfo jvm配置信息相关参数

jmap jvm内存映像工具 堆快照和对象统计信息-dump pid \_文件路径，histo查看数量堆 jmap -dump:format=b,file=jmapdump 8148

jhat 堆内存快照分析工具 分析堆内存

jstack jvm栈查看工具

 

常见指令示例

jps

jstat -gc/-class/ uid

jinfo =jps -v==java -XX:+PrintFlagsFinal

jmap -dump:file=path uid

jhat file

jstack uid

 

 

 

jstatd远程附加jvm信息

 

jdk自带

jdk可视化工具jconsle信息查询

visual vm故障检测

jmeter压力测试

mat内存堆分析器

 

 

使用输出常量表

常量池中21项目

javap -verbose   java

hsdis工具

java -PrintAssembly (Xcomp)

输出所有参数java -XX:+PrintFlagsFinal

 

perfmon性能监测工具

 

 

一：JDK命令行工具

1，jps：虚拟机进程状况工具JVM Process Status Tool

    功能与 ps 命令类似，可以列出正在运行的虚拟机进程，显示虚拟机执行主类名称和本地虚拟机唯一ID（Local Virtual Machine Identifier, LVMID）。

    SYNOPSIS

       jps \[ options ] \[ hostid ]

    选项

    1）-q    只输出LVMID：

        litchifox@litchifox:~$ jps -q

        8737

        7349

        8148

        2938

    2）-m     输出虚拟机进程启动时传递给主类main()函数的参数

        litchifox@litchifox:~$ jps -m

        7349 Bootstrap start

        8148 SocketServer

        8751 Jps -m

        2938 

     3）-l    输出主类的全名，如果进程执行的是Jar包，输出Jar路径

        litchifox@litchifox:~$ jps -l

        7349 org.apache.catalina.startup.Bootstrap

        8773 sun.tools.jps.Jps

        8148 fox.jvm.monitor.SocketServer

        2938 

    4）-v    输出虚拟及进程启动时JVM参数

        litchifox@litchifox:~$ jps -v

        7349 Bootstrap -Djava.util.logging.config.file=/home/litchifox/Learning/apache-tomcat-8.0.0-RC5/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/home/litchifox/Learning/apache-tomcat-8.0.0-RC5/endorsed -Dcatalina.base=/home/litchifox/Learning/apache-tomcat-8.0.0-RC5 -Dcatalina.home=/home/litchifox/Learning/apache-tomcat-8.0.0-RC5 -Djava.io.tmpdir=/home/litchifox/Learning/apache-tomcat-8.0.0-RC5/temp

        8788 Jps -Dapplication.home=/usr/lib/jvm/myopenjdk7 -Xms8m

        8148 SocketServer -Dfile.encoding=UTF-8

        2938  -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m

    

    jps可以通过RMI协议查询开启了RMI服务的远程虚拟机进程的状态，hostid为RMI注册表中注册的主机名。

 

2，jstat：虚拟机统计信息监视工具     JVM Statistics Monitoring Tool

    SYNOPSIS

       jstat \[ generalOption | outputOptions vmid \[interval\[s|ms] \[count]] ]

    如果是本地虚拟机进程，VMID与LVMID是一致的，如果是远程虚拟机进程，那VMID的格式为：

        \[protocol:]\[//]lvmid\[@hostname\[:port]/servername]

    interval 表示查询间隔（默认为ms）， count 表示查询次数，省略这两个参数，说明只查询一次。

    

   -class Option    监视类装载、卸载的数量、总空间以及类装载所耗费的时间

   -compiler Option      JIT编译器过的方法、耗时等信息

   -gc Option    监视Java堆状况，包括Eden区、两个survivor区、老年代、永久代等的容量、已用空间、GC时间合计等信息

   -gccapacity Option    与gc基本相同，会输出主要关注的Java堆各个区域使用到的最大、最小空间

   -gccause Option   与-gcutil功能一样，会额外输出导致上次GC产生的原因

       This  option displays the same summary of garbage collection statistics

       as the -gcutil option, but includes the causes of the last garbage col‐

       lection event and (if applicable) the current garbage collection event.

       In addition to the columns listed for -gcutil,  this  option  adds  the

       following columns:

   -gcnew Option    新生代GC状况

   -gcnewcapacity Option    主要关注使用到的最大、最小空间

   -gcold Option    老年代GC状况

   -gcoldcapacity Option    

   -gcpermcapacity Option    永久代使用到的最大、最小空间

   -gcutil Option    与-gc基本相同，主要关注已使用空间占总空间的百分比

   -printcompilation Option    已被JIT编译的方法

 

         -h n    

             Display a column header every n samples (output rows), where n is

             a positive integer. Default value is 0, which displays the column

             header above the first row of data.

          -t n

             Display  a  timestamp  column  as the first column of output. The

             timestamp is the time since the start time of the target JVM.

          -JjavaOption

             Pass javaOption to the java application  launcher.  For  example,

             -J-Xms48m sets the startup memory to 48 megabytes. For a complete

             list of options, see java(1)

    示例：

    litchifox@litchifox:~$ jstat -gcutil -h3 -t 7349 1s 10

    Timestamp         S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   

             4517.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

             4518.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

             4519.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

    Timestamp         S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   

             4520.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

             4521.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

             4522.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

    Timestamp         S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   

             4523.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

             4524.5   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

             4525.6   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

    Timestamp         S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   

             4526.6   0.00  99.92  34.72  58.75  85.41      7    0.094     0    0.000    0.094

    S0,S1表示Survivor，O表老年代，P表永久代，E表Eden区，YGC表Minor GC(Young GC)，FGC表Full GC

    

    litchifox@litchifox:~$ jstat -gc -h3 -t 7349 1s 4

    Timestamp        S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC    PU    YGC     YGCT    FGC    FGCT     GCT   

             4749.7 13056.0 8704.0  0.0   8697.0 63232.0  23064.9   41728.0    24516.4   16384.0 13994.1      7    0.094   0      0.000    0.094

             4750.7 13056.0 8704.0  0.0   8697.0 63232.0  23064.9   41728.0    24516.4   16384.0 13994.1      7    0.094   0      0.000    0.094

             4751.7 13056.0 8704.0  0.0   8697.0 63232.0  23064.9   41728.0    24516.4   16384.0 13994.1      7    0.094   0      0.000    0.094

    Timestamp        S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC    PU    YGC     YGCT    FGC    FGCT     GCT   

             4752.7 13056.0 8704.0  0.0   8697.0 63232.0  23064.9   41728.0    24516.4   16384.0 13994.1      7    0.094   0      0.000    0.094

 

    litchifox@litchifox:~$ jstat -gccapacity -h3 7349 250 5

     NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC     PGCMN    PGCMX     PGC       PC     YGC    FGC 

     20992.0 335360.0 152576.0 13056.0 8704.0  63232.0    41728.0   670208.0    41728.0    41728.0  16384.0  65536.0  16384.0  16384.0      7     0

     20992.0 335360.0 152576.0 13056.0 8704.0  63232.0    41728.0   670208.0    41728.0    41728.0  16384.0  65536.0  16384.0  16384.0      7     0

     20992.0 335360.0 152576.0 13056.0 8704.0  63232.0    41728.0   670208.0    41728.0    41728.0  16384.0  65536.0  16384.0  16384.0      7     0

     NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC     PGCMN    PGCMX     PGC       PC     YGC    FGC 

     20992.0 335360.0 152576.0 13056.0 8704.0  63232.0    41728.0   670208.0    41728.0    41728.0  16384.0  65536.0  16384.0  16384.0      7     0

     20992.0 335360.0 152576.0 13056.0 8704.0  63232.0    41728.0   670208.0    41728.0    41728.0  16384.0  65536.0  16384.0  16384.0      7     0

    

    litchifox@litchifox:~$ jstat -gccause -t 7349 250 5

    Timestamp         S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC                 

             4996.9   0.00  99.92  37.80  58.75  85.42      7    0.094     0    0.000    0.094 Allocation Failure   No GC              

             4997.2   0.00  99.92  37.80  58.75  85.42      7    0.094     0    0.000    0.094 Allocation Failure   No GC              

             4997.4   0.00  99.92  37.80  58.75  85.42      7    0.094     0    0.000    0.094 Allocation Failure   No GC              

             4997.7   0.00  99.92  37.80  58.75  85.42      7    0.094     0    0.000    0.094 Allocation Failure   No GC              

             4997.9   0.00  99.92  37.80  58.75  85.42      7    0.094     0    0.000    0.094 Allocation Failure   No GC

 

3，jinfo：Java配置信息工具，Configuration Info for Java

    实时地查看和调整虚拟机各项参数。

    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)

    -flag         to print the value of the named VM flag

    -flag \[+|-]    to enable or disable the named VM flag   

    -flag = to set the named VM flag to the given value

    -flags               to print VM flags

    -sysprops            to print Java system propertie    可打印System.getProperties()的内容

 

    jps -v 可查看虚拟机启动时的显式指定的参数列表

    JDK1.6及以上，使用java -XX:+PrintFlagsFinal 查看参数默认值

 

4，jmap：Java内存映像工具 Memory Map for Java

    用于生成堆转储快照（一般称为heapdump或dump文件）。

    -XX:+HeapDumpOnOutOfMemoryError 可以让虚拟机在OOM异常出现后自动生成dump文件

    -XX:+HeapDumpOnCtrlBreak 可以使用Ctrl+Break键让虚拟机生成dump文件

    linux系统下通过kill -3 命令发送进程退出信号，也能拿到dump文件。

    

    -dump 生成Java堆转储快照。

    -finalizerinfo   显示在F-Queue中等待Finalizer线程执行的finalize方法的对象。只在Linux/Soaris平台下有效

    -heap    显示Java堆详细信息，如使用哪种回收器、参数配置、分代状况等。只在Linux/Solaris平台下有效

    -histo    显示堆中对象统计信息，包括类、实例数量、合计容量

    -permstat    以ClassLoader为统计口径显示永久代内存状态。只在Linux/Solaris平台下有效

    -F    当虚拟机进程对-dump选项没有响应时，可使用这个选项强制生成dump快照。只在Linux/Solaris平台下有效

 

    litchifox@litchifox:~$ jmap -dump:format=b,file=jmapdump 8148

    Dumping heap to /home/litchifox/jmapdump ...

    Heap dump file created

    

5，jhat：虚拟机堆转储快照分析工具  JVM Heap Analysis Tool

    litchifox@litchifox:~$ jhat jmapdump

    Reading from jmapdump...

    Dump file created Sat Dec 07 01:38:06 CST 2013

    Snapshot read, resolving...

    Resolving 6458 objects...

    Chasing references, expect 1 dots.

    Eliminating duplicate references.

    Snapshot resolved.

    Started HTTP server on port 7000

    Server is ready.

    

    在浏览器中键入[http://localhost:7000/查看分析结果](http://localhost:7000/%E6%9F%A5%E7%9C%8B%E5%88%86%E6%9E%90%E7%BB%93%E6%9E%9C)

 

6，jstack：Java堆栈跟踪工具     Stack Trace for Java

    生成虚拟机当前时刻的线程快照（一般称为threaddump或者javacore文件）。

    线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈的集合，生成线程快照的主要目的是定位线程出现长时间停顿的原因，如线程间死锁、死循环、请求外部资源导致的长时间等待等都是导致线程长时间停顿的常见原因。

    OPTIONS

          -F Force a stack dump when 'jstack \[-l] pid' does not respond.

          -l Long listing. Prints additional  information  about  locks  such  as  list  of  owned

             java.util.concurrent ownable synchronizers @

             http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/AbstractOwn‐

             ableSynchronizer.html.

          -m prints mixed mode (both Java and native C/C++ frames) stack trace.

    

    litchifox@litchifox:~$ jstack -l 8148

    2013-12-07 01:51:18

    Full thread dump Java HotSpot(TM) Server VM (23.25-b01 mixed mode):

     

    "Attach Listener" daemon prio=10 tid=0x6e403c00 nid=0x2941 waiting on condition \[0x00000000]

       java.lang.Thread.State: RUNNABLE

    

       Locked ownable synchronizers:

    - None

    

    "Service Thread" daemon prio=10 tid=0x6e8e9c00 nid=0x1fe7 runnable \[0x00000000]

       java.lang.Thread.State: RUNNABLE

    

       Locked ownable synchronizers:

    - None

    

    "C2 CompilerThread1" daemon prio=10 tid=0x6e8e8000 nid=0x1fe6 waiting on condition \[0x00000000]

       java.lang.Thread.State: RUNNABLE

    

       Locked ownable synchronizers:

    - None

    

    "C2 CompilerThread0" daemon prio=10 tid=0x6e8e6000 nid=0x1fe5 waiting on condition \[0x00000000]

       java.lang.Thread.State: RUNNABLE

    

       Locked ownable synchronizers:

    - None

    

    "Signal Dispatcher" daemon prio=10 tid=0x6e8e4400 nid=0x1fe4 runnable \[0x00000000]

       java.lang.Thread.State: RUNNABLE

    

       Locked ownable synchronizers:

    - None

    

    "Finalizer" daemon prio=10 tid=0x6e8a8c00 nid=0x1fe3 in Object.wait() \[0x71c58000]

       java.lang.Thread.State: WAITING (on object monitor)

    at java.lang.Object.wait(Native Method)

    - waiting on &lt;0x9ef85698&gt; (a java.lang.ref.ReferenceQueue$Lock)

    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)

    - locked &lt;0x9ef85698&gt; (a java.lang.ref.ReferenceQueue$Lock)

    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)

    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189)

    

       Locked ownable synchronizers:

    - None

    

    "Reference Handler" daemon prio=10 tid=0x6e8a6c00 nid=0x1fe2 in Object.wait() \[0x6e77d000]

       java.lang.Thread.State: WAITING (on object monitor)

    at java.lang.Object.wait(Native Method)

    - waiting on &lt;0x9ef85270&gt; (a java.lang.ref.Reference$Lock)

    at java.lang.Object.wait(Object.java:503)

    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)

    - locked &lt;0x9ef85270&gt; (a java.lang.ref.Reference$Lock)

    

       Locked ownable synchronizers:

    - None

    

    "main" prio=10 tid=0xb6806400 nid=0x1fda runnable \[0xb697a000]

       java.lang.Thread.State: RUNNABLE

    at java.net.PlainSocketImpl.socketAccept(Native Method)

    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)

    at java.net.ServerSocket.implAccept(ServerSocket.java:530)

    at java.net.ServerSocket.accept(ServerSocket.java:498)

    at fox.jvm.monitor.SocketServer.main(SocketServer.java:11)

    

       Locked ownable synchronizers:

    - None

    

    "VM Thread" prio=10 tid=0x6e8a1400 nid=0x1fe1 runnable 

    

    "GC task thread#0 (ParallelGC)" prio=10 tid=0xb6810400 nid=0x1fdd runnable 

    

    "GC task thread#1 (ParallelGC)" prio=10 tid=0xb6811c00 nid=0x1fde runnable 

    

    "GC task thread#2 (ParallelGC)" prio=10 tid=0xb6813000 nid=0x1fdf runnable 

    

    "GC task thread#3 (ParallelGC)" prio=10 tid=0xb6814800 nid=0x1fe0 runnable 

    

    "VM Periodic Task Thread" prio=10 tid=0x6e8ebc00 nid=0x1fe8 waiting on condition 

    

    JNI global references: 122

    

   在JDK1.5中，java.lang.Thread新增一个getAllStackTraces()的方法用于获取虚拟机中所有线程的StackTraceElement对象。

    在实际项目中，可以做个jsp页面，查看线程堆栈。

 

7，HSDIS：JIT生成代码反汇编  

    下载hsdis-i386.so:[https://kenai.com/projects/base-hsdis/downloads](https://kenai.com/projects/base-hsdis/downloads) 

    或者利用jdk源码编译hsdis（需要binutils）

   复制到 JAVA\_HOME/jre/bin/i386/client    JAVA\_HOME/jre/bin/i386/server 目录下（与libjvm.so同目录）

 

 

    package fox.jvm.monitor;

    

    public class Bar {

    int a = 1;

    static int b = 2;

    public int sum(int c) {

    return a + b + c;

    }

    public static void main(String\[] args) {

    new Bar().sum(3);

    }

    }

 

 

 

    Java HotSpot(TM) Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output

    CompilerOracle: dontinline \*Bar.sum

    CompilerOracle: compileonly \*Bar.sum

    Loaded disassembler from hsdis-i386.so

    Decoding compiled method 0xb3733b88:

    Code:

    \[Disassembling for mach='i386']

    \[Entry Point]

    \[Constants]

      # {method} 'sum' '(I)I' in 'fox/jvm/monitor/Bar'

      # this:     ecx       = 'fox/jvm/monitor/Bar'

      # parm0:    edx       = int

      #           \[sp+0x10]  (sp of caller)

      0xb3733c80: cmp    0x4(�x),�x

      0xb3733c83: jne    0xb3715120         ;   {runtime\_call}

      0xb3733c89: xchg   %ax,%ax

    \[Verified Entry Point]

      0xb3733c8c: sub    $0xc,%esp

      0xb3733c92: mov    �p,0x8(%esp)     ;\*synchronization entry

                                            ; - fox.jvm.monitor.Bar::sum@-1 (line 13)

      0xb3733c96: mov    $0x9ef197a8,�p   ;   {oop(a 'java/lang/Class' = 'fox/jvm/monitor/Bar')}

      0xb3733c9b: mov    0x70(�p),�x

      0xb3733c9e: add    0x8(�x),�x

      0xb3733ca1: mov    �x,�x

      0xb3733ca3: add    �x,�x          ;\*iadd

                                            ; - fox.jvm.monitor.Bar::sum@9 (line 13)

      0xb3733ca5: add    $0x8,%esp

      0xb3733ca8: pop    �p

      0xb3733ca9: test   �x,0xb770a000    ;   {poll\_return}

      0xb3733caf: ret    

      0xb3733cb0: hlt    

      0xb3733cb1: hlt    

      0xb3733cb2: hlt    

      0xb3733cb3: hlt    

      0xb3733cb4: hlt    

      0xb3733cb5: hlt    

      0xb3733cb6: hlt    

      0xb3733cb7: hlt    

      0xb3733cb8: hlt    

      0xb3733cb9: hlt    

      0xb3733cba: hlt    

      0xb3733cbb: hlt    

      0xb3733cbc: hlt    

      0xb3733cbd: hlt    

      0xb3733cbe: hlt    

      0xb3733cbf: hlt    

    \[Exception Handler]

    \[Stub Code]

      0xb3733cc0: jmp    0xb37303a0         ;   {no\_reloc}

    \[Deopt Handler Code]

      0xb3733cc5: push   $0xb3733cc5        ;   {section\_word}

      0xb3733cca: jmp    0xb37163a0         ;   {runtime\_call}

      0xb3733ccf: hlt    

    

二：JDK可视化工具

1，JConsole：Java监视与管理控制台  Java Monitoring and Management Console

    JDK/bin 目录 下jconsole 

    内存（可视化的jstat），线程（可视化的jstack），类，VM Summary

 

2，VisualVM：All-in-Ome Java Troublehooting Tool

      下载地址：[http://visualvm.java.net](http://visualvm.java.net/) 

 

    可通过插件扩展：

    显示虚拟机进程以及进程的配置、环境信息（jps、jinfo）

    监视应用程序的CPU、GC、堆、方法区以及线程的信息（jstat、jstack）

    dump以及分析堆转储快照（jmap、jhat）

    方法级的程序运行性能分析，找出被调用最多、运行时间最长的方法。

    离线程序快照：收集程序的运行时配置、线程dump、内存dump等信息建立一个快照，可以将快照发送开发者处进行Bug反馈。

 

    BTrace 动态日志跟踪：

    在不停止目标程序运行的前提下，通过HotSpot虚拟机的HotSwap技术动态加入原本并不存在的调试代码。

    HotSwap技术：代码热替换技术，HotSpot虚拟机运行在不停在运行的情况下，更新已经加载的代码。

来源： &lt;[http://blog.sina.com.cn/s/blog\_5969fc560101gso1.html](http://blog.sina.com.cn/s/blog_5969fc560101gso1.html)&gt;