jdk 性能监测工具,jvm性能监测工具。

  • jps:查看java进程。
  • jstat :虚拟机运行时信息堆监控工具。
  • jstack:线程堆栈监控工具。
  • jinfo:虚拟机参数查看设置工具。
  • jmap:导出堆文件工具。
  • jhat:堆文件分析工具。
  • jcmd:多功能命令工具推荐替代jmap。
  • hprof:性能统计工具在vm中设置参数。
  • jconsole:图形化监控工具。
  • visual VM:图形化监控工具。
  • jmc:图形化诊断工具。

jstat

  • jstat -gc 42718 1000 3
    每隔1秒打印gc信息总共打印3次。
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
5120.0 5120.0  0.0    0.0   33280.0   4675.8   87552.0      0.0     4480.0 779.9  384.0   76.4       0    0.000   0      0.000    0.000
单位(KB)
年轻代大小 已使用年轻代 老年代大小 已使用老年代 永久区大小 已使用永久区大小 压缩类大小 压缩类使用大小 新生代gc次数 新生代gc耗时 Full GC次数 Full GC 耗时 GC总耗时
EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
33280 4675.8 87552 0 4480 779.9 384 76.4
          0
0 0 0 0
  • jstat -gccapacity 42718 1000 3
    每隔1秒打印堆信息总共打印3次。
 NGCMN    NGCMX   NGC      S0C     S1C     EC          OGCMN    OGCMX       OGC         OC           MCMN     MCMX      MC           CCSMN    CCSMX     CCSC       YGC    FGC 
 43520.0 698880.0 698368.0 41984.0 42496.0 609280.0    87552.0  1398272.0   126464.0   126464.0      0.0     1105920.0  62936.0      0.0      1048576.0   7168.0     17     3
 43520.0 698880.0 698368.0 41984.0 42496.0 609280.0    87552.0  1398272.0   126464.0   126464.0      0.0     1105920.0  62936.0      0.0      1048576.0   7168.0     17     3
 43520.0 698880.0 698368.0 41984.0 42496.0 609280.0    87552.0  1398272.0   126464.0   126464.0      0.0     1105920.0  62936.0      0.0      1048576.0   7168.0     17     3
单位(KB)
新生代最小值 新生代最大值 新生代当前值 s0大小 s2大小 伊甸园区大小 老年代最小值 老年代最大值 老年代当前值 老年代当前大小 元数据最小值 元数据最大值 元数据当前值 压缩类空间最小值 压缩类空间最大值 压缩类空间当前值 年轻代gc次数 老年代GC次数
NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
43520 698880 698368 41984 42496 609280 87552 1E+06 126464 126464 0 1105920 62936 0 1048576 7168 17 3
43520 698880 698368 41984 42496 609280 87552 1E+06 126464 126464 0 1105920 62936 0 1048576 7168 17 3
43520 698880 698368 41984 42496 609280 87552 1E+06 126464 126464 0 1105920 62936 0 1048576 7168 17 3
  • jstat -gccapacity 42718 1000 3
    每隔1秒打印堆内存占比gc原因 总共打印3次。
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC                 
 98.37   0.00  22.42  32.73  98.01  96.15     20    0.749     3    0.473    1.222 Allocation Failure   No GC               
 98.37   0.00  22.54  32.73  98.01  96.15     20    0.749     3    0.473    1.222 Allocation Failure   No GC               
 98.37   0.00  22.80  32.73  98.01  96.15     20    0.749     3    0.473    1.222 Allocation Failure   No GC 











上次gc原因 当前原因gc
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT LGCC GCC
98.37 0 22.42 33 98.01 96.15 20 0.749 3 0.473 1.22 Allocation Failure No GC
98.37 0 22.54 33 98.01 96.15 20 0.749 3 0.473 1.22 Allocation Failure No GC
98.37 0 22.8 33 98.01 96.15 20 0.749 3 0.473 1.22 Allocation Failure No GC
  • jstat -gcnew 42718 1000 3
    每隔1秒打印新生代gc信息总共打印3次。
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  
6656.0 36864.0 6418.5    0.0  9  15 36864.0 394752.0  15966.7     28    1.288
6656.0 36864.0 6418.5    0.0  9  15 36864.0 394752.0  15966.7     28    1.288
6656.0 36864.0 6418.5    0.0  9  15 36864.0 394752.0  15967.1     28    1.288
  • jstat -gcold 42718 1000 3
    每隔1秒打印老年代gc信息总共打印3次。

   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT   
 74328.0  72643.7   8064.0   7678.4    126464.0     54344.2     28     3    0.473    1.761
 74328.0  72643.7   8064.0   7678.4    126464.0     54344.2     28     3    0.473    1.761
 74328.0  72643.7   8064.0   7678.4    126464.0     54344.2     28     3    0.473    1.761
  • jstat -gcnewcapacity 42718 1000 3
    每隔1秒打印新生代容量信息总共打印3次。

  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC      YGC   FGC 
   43520.0   698880.0   484352.0 232960.0   6656.0 232960.0  36864.0   697856.0   394752.0    28     3
   43520.0   698880.0   484352.0 232960.0   6656.0 232960.0  36864.0   697856.0   394752.0    28     3
   43520.0   698880.0   484352.0 232960.0   6656.0 232960.0  36864.0   697856.0   394752.0    28     3
  • jstat -gcoldcapacity 42718 1000 3
    每隔1秒打印老年代容量信息总共打印3次。

   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT   
    87552.0   1398272.0    126464.0    126464.0    28     3    0.473    1.761
    87552.0   1398272.0    126464.0    126464.0    28     3    0.473    1.761
    87552.0   1398272.0    126464.0    126464.0    28     3    0.473    1.761
  • jstat -gcmetacapacity 42718
    每隔1秒打印元数据容量信息总共打印3次。
     MCMN       MCMX        MC       CCSMN      CCSMX       CCSC     YGC   FGC    FGCT     GCT   
       0.0  1116160.0    74328.0        0.0  1048576.0     8064.0    28     3    0.473    1.761
       0.0  1116160.0    74328.0        0.0  1048576.0     8064.0    28     3    0.473    1.761
       0.0  1116160.0    74328.0        0.0  1048576.0     8064.0    28     3    0.473    1.761
  • jstat -gcutil 42718 1000 3
    每隔1秒打gc回收信息各区使用占比总共打印3次。

  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  95.87   2.73  42.97  97.75  95.22     29    1.359     3    0.473    1.832
  0.00  95.87   2.78  42.97  97.75  95.22     29    1.359     3    0.473    1.832
  0.00  95.87   2.79  42.97  97.75  95.22     29    1.359     3    0.473    1.832

jinfo

jinfo -< option > < pid >

其中option可以为以下信息:

  • -flag< name >: 打印指定java虚拟机的参数值。
  • -flag [+|-]< name >:设置或取消指定java虚拟机参数的布尔值。
  • -flag < name >=< value >:设置指定java虚拟机的参数的值。

  • 查看配置项PrintGCDetails的信息: jinfo -flag PrintGCDetails 42718
-XX:-PrintGCDetails
  • 查看配置项PrintGCDetails的信息: jinfo -flag PrintGCDetails 42718
-XX:-PrintGCDetails
  • 设置配置项PrintGCDetails的信息: jinfo -flag +PrintGCDetails 42718
$ jinfo -flag  +PrintGCDetails 42718
$ jinfo -flag  PrintGCDetails 42718
-XX:+PrintGCDetails
$ jinfo -flag  -PrintGCDetails 42718
$ jinfo -flag  PrintGCDetails 42718
-XX:-PrintGCDetails

jmap

  • jmap -histo 51310 >1.txt
    可以查看当前类的总量

num     #instances         #bytes  class name
----------------------------------------------
  1:       1859469      248589744  [C
  2:        825306      146112544  [B
  3:         40333       92152680  [I
  4:       1002967       24071208  java.lang.String
.......
4559:             1             16  sun.util.CoreResourceBundleControl
4560:             1             16  sun.util.calendar.Gregorian
4561:             1             16  sun.util.locale.InternalLocaleBuilder$CaseInsensitiveChar
4562:             1             16  sun.util.locale.provider.AuxLocaleProviderAdapter$NullProvider
4563:             1             16  sun.util.locale.provider.CalendarDataUtility$CalendarWeekParameterGetter
4564:             1             16  sun.util.locale.provider.SPILocaleProviderAdapter
4565:             1             16  sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter
4566:             1             16  sun.util.resources.LocaleData
4567:             1             16  sun.util.resources.LocaleData$LocaleDataResourceBundleControl
Total       5619075      579873064
  • jmap -dump:format=b,file=heap.hprof 51310
    导出堆快照 导出后可以使用 jhat,VisualVM ,MAT等工具分析。
Dumping heap to /Users/Desktop/heap.hprof ...
Heap dump file created

jhat

  • jhat heap.hprof
    使用jhat 分析jmap导出的heap.hprof,
Reading from heap.hprof...
Dump file created Wed Dec 19 15:28:30 CST 2018
Snapshot read, resolving...
Resolving 3387970 objects...
Chasing references, expect 677 dots.....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references.....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.
  • 状态如上输出后访问:http://127.0.0.1:7000/

jstack

  • jstack -l 52354
    查看线程的状态直接输出
  • jstack -l 52354 >1.txt
    输出到文件
  • 使用jstack 查找死锁
    • 模拟死锁java代码
String sync1=new String("sync1");
       String sync2=new String("sync2");
       Thread thread1=new Thread(()->{
           while (true){
               synchronized (sync1){
                   System.out.println("");
                   synchronized (sync2){

                   }
               }
           }
       });
       thread1.setName("MyThread2");

       Thread thread2=new Thread(()->{
           while (true){
               synchronized (sync2){
                   System.out.println("");
                   synchronized (sync1){

                   }
               }
           }
       });
       thread2.setName("MyThread2");

       thread1.start();
       thread2.start();
* jstack -l 52354
 我们在结尾处可以很清晰看到发现一个死锁:Found 1 deadlock.
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.191-b12 mixed mode):

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

   Locked ownable synchronizers:
	- None

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

   Locked ownable synchronizers:
	- None

"MyThread2" #11 prio=5 os_prio=31 tid=0x00007fe44d19f800 nid=0x4603 waiting for monitor entry [0x0000700007527000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at com.Test.lambda$main$1(Test.java:34)
	- waiting to lock <0x00000007958d0f88> (a java.lang.String)
	- locked <0x00000007958d0fd8> (a java.lang.String)
	at com.Test$$Lambda$2/764308918.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"MyThread2" #10 prio=5 os_prio=31 tid=0x00007fe44e02e000 nid=0x4003 waiting for monitor entry [0x0000700007424000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at com.Test.lambda$main$0(Test.java:22)
	- waiting to lock <0x00000007958d0fd8> (a java.lang.String)
	- locked <0x00000007958d0f88> (a java.lang.String)
	at com.Test$$Lambda$1/670576685.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"Service Thread" #9 daemon prio=9 os_prio=31 tid=0x00007fe44d121800 nid=0x4903 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C1 CompilerThread2" #8 daemon prio=9 os_prio=31 tid=0x00007fe44e97d000 nid=0x3c03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C2 CompilerThread1" #7 daemon prio=9 os_prio=31 tid=0x00007fe44e01f000 nid=0x4b03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=31 tid=0x00007fe44e974000 nid=0x3a03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"Monitor Ctrl-Break" #5 daemon prio=5 os_prio=31 tid=0x00007fe44d11f000 nid=0x4d03 runnable [0x0000700006e12000]
   java.lang.Thread.State: RUNNABLE
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
	at java.net.SocketInputStream.read(SocketInputStream.java:171)
	at java.net.SocketInputStream.read(SocketInputStream.java:141)
	at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
	at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
	at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
	- locked <0x0000000795859e00> (a java.io.InputStreamReader)
	at java.io.InputStreamReader.read(InputStreamReader.java:184)
	at java.io.BufferedReader.fill(BufferedReader.java:161)
	at java.io.BufferedReader.readLine(BufferedReader.java:324)
	- locked <0x0000000795859e00> (a java.io.InputStreamReader)
	at java.io.BufferedReader.readLine(BufferedReader.java:389)
	at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)

   Locked ownable synchronizers:
	- None

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007fe44e866000 nid=0x4e07 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007fe44d001800 nid=0x3003 in Object.wait() [0x0000700006c0c000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x0000000795588ed0> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
	- locked <0x0000000795588ed0> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

   Locked ownable synchronizers:
	- None

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007fe44d000800 nid=0x5303 in Object.wait() [0x0000700006b09000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x0000000795586bf8> (a java.lang.ref.Reference$Lock)
	at java.lang.Object.wait(Object.java:502)
	at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
	- locked <0x0000000795586bf8> (a java.lang.ref.Reference$Lock)
	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

   Locked ownable synchronizers:
	- None

"VM Thread" os_prio=31 tid=0x00007fe44e031800 nid=0x5403 runnable 

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007fe44d002800 nid=0x2107 runnable 

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007fe44e806000 nid=0x2003 runnable 

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007fe44d003000 nid=0x2a03 runnable 

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007fe44d003800 nid=0x2c03 runnable 

"VM Periodic Task Thread" os_prio=31 tid=0x00007fe44e290800 nid=0x3e03 waiting on condition 

JNI global references: 319


Found one Java-level deadlock:
=============================
"MyThread2":
  waiting to lock monitor 0x00007fe44e82acb8 (object 0x00000007958d0f88, a java.lang.String),
  which is held by "MyThread2"
"MyThread2":
  waiting to lock monitor 0x00007fe44e82d758 (object 0x00000007958d0fd8, a java.lang.String),
  which is held by "MyThread2"

Java stack information for the threads listed above:
===================================================
"MyThread2":
	at com.Test.lambda$main$1(Test.java:34)
	- waiting to lock <0x00000007958d0f88> (a java.lang.String)
	- locked <0x00000007958d0fd8> (a java.lang.String)
	at com.Test$$Lambda$2/764308918.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:748)
"MyThread2":
	at com.Test.lambda$main$0(Test.java:22)
	- waiting to lock <0x00000007958d0fd8> (a java.lang.String)
	- locked <0x00000007958d0f88> (a java.lang.String)
	at com.Test$$Lambda$1/670576685.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

jcmd

  • jcmd -l:列出当前java 进程。
52354 com.Test
52355 org.jetbrains.jps.cmdline.Launcher /Applications/IntelliJ IDEA.app/Contents/lib/httpclient-4.5.5.jar:/Applications/IntelliJ IDEA.app/Contents/lib/httpcore-4.4.9.jar:/Applications/IntelliJ IDEA.app/Contents/lib/plexus-component-annotations-1.6.jar:/Applications/IntelliJ IDEA.app/Contents/lib/guava-23.6-jre.jar:/Applications/IntelliJ IDEA.app/Contents/lib/aether-api-1.1.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/aether-transport-http-1.1.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/asm-all.jar:/Applications/IntelliJ IDEA.app/Contents/lib/aether-util-1.1.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/lz4-1.3.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/util.jar:/Applications/IntelliJ IDEA.app/Contents/lib/platform-api.jar:/Applications/IntelliJ IDEA.app/Contents/lib/plexus-interpolation-1.21.jar:/Applications/IntelliJ IDEA.app/Contents/lib/aether-spi-1.1.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/protobuf-java-3.4.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/aether-connector
598 
52714 sun.tools.jcmd.JCmd -l

  • jcmd 52354 help:查看jcmd支持当前进程的命令。
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
VM.classloader_stats
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.uptime
VM.dynlibs
VM.flags
VM.system_properties
VM.command_line
VM.version
  • jcmd 31275 Thread.print -l # 打印线程栈
  • jcmd 31275 VM.command_line # 打印启动命令及参数
  • jcmd 31275 GC.heap_dump /data/31275.dump # dump heap
  • jcmd 31275 GC.class_histogram #查看类的统计信息
  • jcmd 31275 VM.system_properties #查看系统属性内容
  • jcmd 31275 VM.uptime #查看虚拟机启动时间
  • jcmd 31275 PerfCounter.print #查看性能统计

hprof

  • java -agentlib:hprof=help : 查看帮助文档

     HPROF: Heap and CPU Profiling Agent (JVMTI Demonstration Code)

hprof usage: java -agentlib:hprof=[help]|[<option>=<value>, ...]

Option Name and Value  Description                    Default
---------------------  -----------                    -------
heap=dump|sites|all    heap profiling                 all
cpu=samples|times|old  CPU usage                      off
monitor=y|n            monitor contention             n
format=a|b             text(txt) or binary output     a
file=<file>            write data to file             java.hprof[{.txt}]
net=<host>:<port>      send data over a socket        off
depth=<size>           stack trace depth              4
interval=<ms>          sample interval in ms          10
cutoff=<value>         output cutoff point            0.0001
lineno=y|n             line number in traces?         y
thread=y|n             thread in traces?              n
doe=y|n                dump on exit?                  y
msa=y|n                Solaris micro state accounting n
force=y|n              force output to <file>         y
verbose=y|n            print messages about dumps     y

Obsolete Options
----------------
gc_okay=y|n

Examples
--------
  - Get sample cpu information every 20 millisec, with a stack depth of 3:
      java -agentlib:hprof=cpu=samples,interval=20,depth=3 classname
  - Get heap usage information based on the allocation sites:
      java -agentlib:hprof=heap=sites classname

Notes
-----
  - The option format=b cannot be used with monitor=y.
  - The option format=b cannot be used with cpu=old|times.
  - Use of the -Xrunhprof interface can still be used, e.g.
       java -Xrunhprof:[help]|[<option>=<value>, ...]
    will behave exactly the same as:
       java -agentlib:hprof=[help]|[<option>=<value>, ...]

Warnings
--------
  - This is demonstration code for the JVMTI interface and use of BCI,
    it is not an official product or formal part of the JDK.
  - The -Xrunhprof interface will be removed in a future release.
  - The option format=b is considered experimental, this format may change
    in a future release.

  • -agentlib:hprof=cpu=times,interval=10,file=/Users/dxm/Desktop/java.hprof1.txt :在vm中加入参数程序运行会产生一个文件其中self 是方法执行时间占比。
rank   self  accum   count trace method
   1 47.62% 47.62%       1 304392 Test.c
   2 35.72% 83.34%       1 304391 Test.b
   3 11.93% 95.27%       1 304390 Test.a
  • -agentlib:hprof=heap=dump,format=b,file=/Users/dxm/Desktop/javaTest.hprof:在vm中加入参数可以将运行程序的堆快照保存到指定文件。

jconsole

Visual VM

JMC

你可能感兴趣的:(java)