JDK8

一、javap

javap:Java 反编译工具,主要根据 Java 字节码文件反汇编为 java 源代码文件

1.1、操作指南

直接 cmd 命令 javap

  1. C:\Users\zhixing>javap
  2. 用法: javap <options> <classes>
  3. 其中, 可能的选项包括:
  4. -help --help -? 输出此用法消息
  5. -version 版本信息
  6. -v -verbose 输出附加信息
  7. -l 输出行号和本地变量表
  8. -public 仅显示公共类和成员
  9. -protected 显示受保护的/公共类和成员
  10. -package 显示程序包/受保护的/公共类
  11. 和成员 (默认)
  12. -p -private 显示所有类和成员
  13. -c 对代码进行反汇编
  14. -s 输出内部类型签名
  15. -sysinfo 显示正在处理的类的
  16. 系统信息 (路径, 大小, 日期, MD5 散列)
  17. -constants 显示最终常量
  18. -classpath <path> 指定查找用户类文件的位置
  19. -cp <path> 指定查找用户类文件的位置
  20. -bootclasspath <path> 覆盖引导类文件的位置

1.2、使用案例

javap -v ${class}

D:\1-programme\background\cw\target\classes\com\cw>javap -v CwApplication.class
Classfile /D:/1-programme/background/cw/target/classes/com/cw/CwApplication.class
  Last modified 2021-10-5; size 1030 bytes
  MD5 checksum a6dff1cbfa76b4565c81da12a170bc09
  Compiled from "CwApplication.java"
public class com.cw.CwApplication
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#28         // java/lang/Object."<init>":()V
   #2 = Class              #29            // com/cw/CwApplication
   #3 = Methodref          #30.#31        // org/springframework/boot/SpringApplication.run:(Ljava/lang/Class;[Ljava/lang/String;)Lorg/springframework/context/ConfigurableApplicationContext;
   #4 = Class              #32            // java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Utf8               LineNumberTable
// 省略。。。。。。

二、jps(Java Virtual Machine Porcess Status Tool)

jps:显示当前所有Java 进程 pid

$ jps -help
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

Definitions:
    <hostid>:      <hostname>[:<port>]

## 常使用命令
$ jps    ## 默认使用;输出 进程 PID + 对应的class
26992 Jps
3472 JvmUtilsDemoApplication
20308 RemoteMavenServer

$ jps -q  ## 仅输出进程 pid ,不包括启动类名
3472
20308

$ jps -v  ## 输出 JVM 对应的参数
3472 JvmUtilsDemoApplication -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:62586,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:D:\2-software\9-idea-2019-1\lib\rt\debugger-agent.jar -Dfile.encoding=UTF-8
20308 RemoteMavenServer -Djava.awt.headless=true -Didea.version==2019.1.1 -Xmx768m -Didea.maven.embedder.version=3.3.9 -Dfile.encoding=UTF-8
15080 Launcher -Xmx700m -Djava.awt.headless=true -Djdt.compiler.useSingleThread=true -Dpreload.project.path=D:/1-programme/2-mine/2-demo-code/jvm/jvm-utils-demo -Dpreload.config.path=C:/Users/zhixing/.IntelliJIdea2019.1/config/options -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djava.net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=-7532185629396075064 -Dfile.encoding=UTF-8 -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2019.1 -Didea.home.path=D:\2-software\9-idea-2019-1 -Didea.config.path=C:\Users\zhixing\.IntelliJIdea2019.1\config -Didea.plugins.path=C:\Users\zhixing\.IntelliJIdea2019.1\config\plugins -Djps.log.dir=C:/Users/zhixing/.IntelliJIdea2019.1/system/log/build-log -Djps.fallback.jdk.home=D:/2-software/9-idea-2019-1/jre64 -Djps.fallback.jdk.version=11.0.2 -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/zhixing/.IntelliJIdea2019.1/system/compile-server/jvm-utils-demo_b1ea92fb/_temp_ -Djps.backward.ref.index.builder=true -Dkotlin.incremental.compilation=true -Dkot
26904  -Xms128m -Xmx750m -XX:ReservedCodeCacheSize=240m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Dsun.io.useCanonCaches=false -Djava.net.preferIPv4Stack=true -Djdk.http.auth.tunneling.disabledSchemes="" -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -javaagent:D:\2-software\9-idea-2019-1\bin\jetbrains-agent.jar -Dfile.encoding=UTF-8 -Djb.vmOptionsFile=C:\Users\zhixing\.IntelliJIdea2019.1\config\idea64.exe.vmoptions -Didea.jre.check=true -Dide.native.launcher=true -Didea.paths.selector=IntelliJIdea2019.1 -XX:ErrorFile=C:\Users\zhixing\java_error_in_idea_%p.log -XX:HeapDumpPath=C:\Users\zhixing\java_error_in_idea.hprof
18188 Jps -Denv.class.path=.;D:\2-software\15-jdk8\jdk\lib\dt.jar;D:\2-software\15-jdk8\jdk\lib\tools.jar -Dapplication.home=D:\2-software\15-jdk8\jdk -Xms8m
3900 KotlinCompileDaemon -Djava.awt.headless=true -Djava.rmi.server.hostname=127.0.0.1 -Xmx700m -Dkotlin.incremental.compilation=true -Dkotlin.incremental.compilation.js=true

$ jps -l   ## 数据class 全限定名
3472 com.zhixing.JvmUtilsDemoApplication
17588 sun.tools.jps.Jps
20308 org.jetbrains.idea.maven.server.RemoteMavenServer
15080 org.jetbrains.jps.cmdline.Launcher

三、jstat

jstat:监视查看JVM统计信息

$ jstat -help
Usage: jstat -help|-options
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
             【
            -t:在输出信息前面加一个 Timestamp 时间列,该时间为JVM启动时间
         -h:周期性输出数据,在输出一定行数据后,再次打印表头,方便查看
       】
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>    数据采样间隔. 使用方式如:["ms"|"s"]。默认 :"ms"
  <count>       终止前的采样数据
  -J<flag>      将 <flag> 传递给这个你在运行的应用程序,如:-J-Xms48m。

## 选项
$ jstat -options
-class                # 显示有关类加载器行为的统计信息
-compiler             # 显示有关Java HotSpot VM实时编译器行为的统计信息
-gc                   # 显示有关垃圾回收堆行为的统计信息
-gccapacity           # 显示有关代的容量及其相应空间的统计信息
-gccause              # 显示有关GC统计信息(同gcutil)摘要,+ 当前及最后一次GC事件的原因
-gcmetacapacity       # 显示有关Metaspace大小的统计信息
-gcnew                # 显示新生代行为的统计信息
-gcnewcapacity        # 显示有关新生代及其相应空间大小的统计信息
-gcold                # 显示有关老年代和Metaspace统计信息行为的统计信息
-gcoldcapacity        # 显示有关老年代大小的统计信息
-gcutil               # 显示有关垃圾回收统计信息的摘要
-printcompilation     # 显示Java HotSpot VM编译方法统计信息

## 常用命令
C:\Users\zhixing>jstat -gc -h10 25872 1000
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 0.0    0.0    0.0    0.0   26624.0  19456.0   225280.0    1024.0   4480.0 3848.9 384.0  377.6       0    0.000   0      0.000    0.000
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT

3.1、-class (类加载统计信息)

$ jstat -class 3472
Loaded  Bytes  Unloaded  Bytes     Time
6677 12139.1        0     0.0       1.94

Loaded:            加载的类总数
Bytes:            加载的kB数
Unloaded:        卸载的类数
Bytes:            卸载的Kbytes数
Time:                执行类加载和卸载操作所花费的时间

3.2、-compiler(编译器统计信息)

$ jstat -compiler 3472
Compiled Failed Invalid   Time   FailedType FailedMethod
    3505      0       0     0.87          0

Compiled:            执行的编译任务数
Failed:                编译任务失败的次数
Invalid:            已失效的编译任务数
Time:                    执行编译任务所花费的时间
FailedType:        编译上次失败的编译的类型
FailedMethod:    上次失败编译的类名和方法

3.3、-gc( GC堆统计信息 )

 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
 0.0   10240.0  0.0   10240.0 109568.0 12288.0   132096.0    3107.3   36048.0 34662.4 4992.0 4416.1      5    0.027   0      0.000    0.027

S0C:        当前S0容量(kB)
S1C:        当前S1容量(kB)
S0U:        S0利用率(kB)
S1U:        S1利用率(kB)
EC:         Eden容量(kB)
EU:            Eden利用率(kB)
OC:            老年代容量(kB)
OU:            老年代利用率(kB)
MC:          Metaspace容量(kB)
MU:            Metaspace利用率(kB)
CCSC:      类指针压缩空间容量(kB)
CCSU:      使用的类指针压缩空间(kB)
YGC:        新生代GC活动的数量
YGCT:        新生代GC时间
FGC:        Full GC的数量
FGCT:        Full GC时间
GCT:        GC总时间

3.3、 ­-gccapacity (分代容量和空间容量)

$ jstat -gccapacity 3472
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC
0.0 4030464.0 119808.0    0.0 10240.0 109568.0        0.0  4030464.0   132096.0   132096.0      0.0 1081344.0  36048.0      0.0 1048576.0   4992.0      5     0

NGCMN:        最小新生容量(kB)
NGCMX:        最大新生容量(kB)
NGC:            当前的新生代容量(kB)
S0C:            当前S0容量(kB)
S1C:            当前S1容量(kB)
EC:                当前的Eden空间容量(kB)
OGCMN:        最小老年代容量(kB)
OGCMX:        最大老年代容量(kB)
OGC:            当前的老年代容量(kB)
OC:                当前老年代容量(kB)
MCMN:            最小Metaspace容量(kB)
MCMX:            最大Metaspace容量(kB)
MC:                Metaspace容量(kB)
CCSMN:        类指针压缩空间最小容量(kB)
CCSMX:        类指针压缩空间最大容量(kB)
CCSC:            类指针压缩空间容量(kB)
YGC:            新生代GC事件的数量
FGC:            FullGC事件的数量

3.4、-gccause(显示有关GC统计信息(同gcutil)摘要,+ 当前及最后一次GC事件的原因)

$ jstat -gccause 3472
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC
  0.00 100.00  11.21   2.35  96.16  88.46      5    0.027     0    0.000    0.027 Metadata GC Threshold No GC

S0:        S0利用率占空间当前容量的百分比
S1:        S1占空间当前容量的百分比
E:        Eden空间利用率占空间当前容量的百分比
O:        老年代利用率占空间当前容量的百分比
M:        Metaspace利用率占空间当前容量的百分比
CCS:    压缩的类空间利用率百分比
YGC:    新生代GC事件的数量
YGCT:    新生代GC时间
FGC:    FullGC事件的数量
FGCT:    FullGC时间
GCT:    GC总时间
LGCC:    上次垃圾回收的原因
GCC:    当前垃圾回收的原因

3.5、-gcmetacapacity(显示有关Metaspace大小的统计信息)

$ jstat -gcmetacapacity 3472
MCMN       MCMX        MC       CCSMN      CCSMX       CCSC     YGC   FGC    FGCT     GCT
0.0  1081344.0    36048.0        0.0  1048576.0     4992.0     5     0    0.000    0.027

MCMN:    最小Metaspace容量(kB)
MCMX:    最大Metaspace容量(kB)
MC:        Metaspace容量(kB)
CCSMN:类指针压缩空间最小容量(kB)
CCSMX:类指针压缩空间最大容量(kB)
YGC:    新生代GC事件的数量
FGC:    FullGC事件的数量
FGCT:    FullGC时间
GCT:    GC总时间

3.6、-gcnew(显示新生代行为的统计信息)
$ jstat -gcnew 3472
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT
 0.0 10240.0    0.0 10240.0 15  15 9728.0 109568.0  13312.0      5    0.027

S0C:    当前S0容量(kB)
S1C:    当前S1容量(kB)
S0U:    S0利用率(kB)
S1U:    S1利用率(kB)
TT:        JVM动态调整的移动到老年代的阈值
MTT:    移动到老年代的最大阈值,超过这个YGC次数,一定会进入老年代
DSS:    期望的survivor大小(kB)
EC:        当前的Eden空间容量(kB)
EU:        Eden空间利用率(kB)
YGC:    新生代GC事件的数量
YGCT:    新生代GC时间

3.7、-gcnewcapacity(显示有关新生代及其相应空间大小的统计信息)
$ jstat -gcnewcapacity 3472
  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC      YGC   FGC
  0.0  4030464.0   119808.0      0.0      0.0 4030464.0  10240.0  4030464.0   109568.0     5     0

NGCMN:    最小新生容量(kB)
NGCMX:    最大新生容量(kB)
NGC:        当前的新生代容量(kB)
S0CMX:    最大S0容量(kB)
S0C:        当前S0容量(kB)
S1CMX:    最大S1容量(kB)
S1C:        当前S1容量(kB)
ECMX:        最大Eden空间容量(kB)
EC:            当前的Eden空间容量(kB)
YGC:        新生代GC事件的数量
FGC:        FullGC事件的数量

3.8、-gcold(显示有关老年代和Metaspace统计信息行为的统计信息)

$ jstat -gcold 3472
   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT
 36048.0  34662.4   4992.0   4416.1    132096.0      3107.3      5     0    0.000    0.027

MC:        Metaspace容量(kB)
MU:        Metaspace利用率(kB)
CCSC:    类指针压缩空间容量(kB)
CCSU:    使用的类指针压缩空间(kB)
OC:        当前老年代容量(kB)
OU:        老年代利用率(kB)
YGC:    新生代GC事件的数量
FGC:    FullGC事件的数量
FGCT:    FullGC时间
GCT:    GC总时间

3.9、-gcoldcapacity(显示有关老年代大小的统计信息)

$ jstat -gcoldcapacity 3472
   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT
   0.0   4030464.0    132096.0    132096.0     5     0    0.000    0.027

OGCMN:    最小老年代容量(kB)
OGCMX:    最大老年代容量(kB)
OGC:        当前的老年代容量(kB)
OC:            当前老年代容量(kB)
YGC:        新生代GC事件的数量
FGC:        FullGC事件的数量
FGCT:        FullGC时间
GCT:        GC总时间

3.10、-gcutil(显示有关垃圾回收统计信息的摘要)

$ jstat -gcutil 3472
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
  0.00 100.00  12.15   2.35  96.16  88.46      5    0.027     0    0.000    0.027

S0:        S0利用率占空间当前容量的百分比
S1:        S1占空间当前容量的百分比
E:        Eden空间利用率占空间当前容量的百分比
O:        老年代利用率占空间当前容量的百分比
M:        Metaspace利用率占空间当前容量的百分比
CCS:    压缩的类空间利用率百分比
YGC:    新生代GC事件的数量
YGCT:    新生代GC时间
FGC:    FullGC事件的数量
FGCT:    FullGC时间
GCT:    GC总时间

3.11、-printcompilation( 显示Java HotSpot VM编译方法统计信息)

$ jstat -printcompilation 3472
Compiled  Size  Type Method
3533    172    1 java/util/concurrent/locks/AbstractQueuedSynchronizer doAcquireInterruptibly

Compiled:    最近编译的方法执行的编译任务数
Size:            最近编译的方法的字节代码的字节数
Type:            最近编译的方法的编译类型
Method:        标识最近编译的方法的类名和方法名。类名使用斜杠(/)而不是点(。)作为名称空间分隔符。方法名称是指定类中的方法。这两个字段的格式与HotSpot XX:+PrintCompilation选项一致

四、jmap

jmap:打印 Java 进程内存对象信息。

C:\Users\zhixing>jmap
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                打印堆栈统计信息
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader statistics
    -finalizerinfo       打印正等待回收的对象信息
    -dump:<dump-options> 使用 hprof 二进制格式打印堆信息
                         dump-options:
                           live         仅打印存活对象;否则打印所有 对象信息。
                           format=b     二进制格式
                           file=<file>  heap 信息打印到文件
                         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.
                         在pid没有响应时强制打印heap信息,该模式下”live“无效。
    -h | -help           打印帮助信息
    -J<flag>             to pass <flag> directly to the runtime system

五、jstack

jstack:打印给定java进程堆栈信息。

C:\Users\zhixing>jstack
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

六、JvisualVM(图形化工具)