zoukankan      html  css  js  c++  java
  • Jstack Jmap jstat

    jstack jmap jstat

    代码,这里以这个为例怎样使用jstack诊断Java应用程序故障

    public class DeadLock {
    
    	public static void main(String[] args) {
    		final Object obj_1 = new Object(), obj_2 = new Object();
    		
    		Thread t1 = new Thread("t1"){
    			@Override
    			public void run() {
    				synchronized (obj_1) {
    					try {
    						Thread.sleep(3000);
    					} catch (InterruptedException e) {}
    					
    					synchronized (obj_2) {
    						System.out.println("thread t1 done.");
    					}
    				}
    			}
    		};
    		
    		Thread t2 = new Thread("t2"){
    			@Override
    			public void run() {
    				synchronized (obj_2) {
    					try {
    						Thread.sleep(3000);
    					} catch (InterruptedException e) {}
    					
    					synchronized (obj_1) {
    						System.out.println("thread t2 done.");
    					}
    				}
    			}
    		};
    		
    		t1.start();
    		t2.start();
    	}
    	
    }
    

    jstack 死锁问题

    root@ubuntu:~# jstack 5606
    2015-01-24 13:45:07
    Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.7-b01 mixed mode):
    
    "Attach Listener" daemon prio=10 tid=0x0000000001441800 nid=0x165d waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
    "DestroyJavaVM" prio=10 tid=0x00007f435007b000 nid=0x15e7 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
    "t2" prio=10 tid=0x00007f4350079000 nid=0x15f5 waiting for monitor entry [0x00007f4356b5d000]
       java.lang.Thread.State: BLOCKED (on object monitor)
            at DeadLock$2.run(DeadLock.java:30)
            - waiting to lock <0x0000000085800808> (a java.lang.Object)
            - locked <0x0000000085800818> (a java.lang.Object)
    
    "t1" prio=10 tid=0x00007f4350077000 nid=0x15f4 waiting for monitor entry [0x00007f4356c5e000]
       java.lang.Thread.State: BLOCKED (on object monitor)
            at DeadLock$1.run(DeadLock.java:15)
            - waiting to lock <0x0000000085800818> (a java.lang.Object)
            - locked <0x0000000085800808> (a java.lang.Object)
    
    "Service Thread" daemon prio=10 tid=0x00007f4350056000 nid=0x15f2 runnable [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
    "C2 CompilerThread1" daemon prio=10 tid=0x00007f4350053000 nid=0x15f1 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
    "C2 CompilerThread0" daemon prio=10 tid=0x00007f4350050000 nid=0x15f0 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
    "Signal Dispatcher" daemon prio=10 tid=0x00007f435004e000 nid=0x15ef runnable [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
    "Finalizer" daemon prio=10 tid=0x00007f4350001000 nid=0x15ee in Object.wait() [0x00007f4357739000]
       java.lang.Thread.State: WAITING (on object monitor)
            at java.lang.Object.wait(Native Method)
            - waiting on <0x0000000085808120> (a java.lang.ref.ReferenceQueue$Lock)
            at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
            - locked <0x0000000085808120> (a java.lang.ref.ReferenceQueue$Lock)
            at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
            at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:177)
    
    "Reference Handler" daemon prio=10 tid=0x00000000014d2000 nid=0x15ed in Object.wait() [0x00007f435783a000]
       java.lang.Thread.State: WAITING (on object monitor)
            at java.lang.Object.wait(Native Method)
            - waiting on <0x00000000858006b8> (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 <0x00000000858006b8> (a java.lang.ref.Reference$Lock)
    
    "VM Thread" prio=10 tid=0x00000000014ca800 nid=0x15ec runnable 
    
    "GC task thread#0 (ParallelGC)" prio=10 tid=0x000000000144f000 nid=0x15e8 runnable 
    
    "GC task thread#1 (ParallelGC)" prio=10 tid=0x0000000001451000 nid=0x15e9 runnable 
    
    "GC task thread#2 (ParallelGC)" prio=10 tid=0x0000000001453000 nid=0x15ea runnable 
    
    "GC task thread#3 (ParallelGC)" prio=10 tid=0x0000000001455000 nid=0x15eb runnable 
    
    "VM Periodic Task Thread" prio=10 tid=0x00007f4350068800 nid=0x15f3 waiting on condition 
    
    JNI global references: 111
    
    
    Found one Java-level deadlock:
    =============================
    "t2":
      waiting to lock monitor 0x00000000014d9600 (object 0x0000000085800808, a java.lang.Object),
      which is held by "t1"
    "t1":
      waiting to lock monitor 0x00000000014d8250 (object 0x0000000085800818, a java.lang.Object),
      which is held by "t2"
    
    Java stack information for the threads listed above:
    ===================================================
    "t2":
            at DeadLock$2.run(DeadLock.java:30)
            - waiting to lock <0x0000000085800808> (a java.lang.Object)
            - locked <0x0000000085800818> (a java.lang.Object)
    "t1":
            at DeadLock$1.run(DeadLock.java:15)
            - waiting to lock <0x0000000085800818> (a java.lang.Object)
            - locked <0x0000000085800808> (a java.lang.Object)
    
    Found 1 deadlock.
    

    jmap

    jmap -dump:live,format=b,file=jmapfile 5606
    

    jhat -J-Xmx1024M jmapfile,可以通过http://ip:7000查看

    instance 数量 http://172.16.10.187:7000/histo/

    root@ubuntu:~# jhat -J-Xmx1024M  jmapfile 
    Reading from jmapfile...
    Dump file created Sat Jan 24 11:39:28 CST 2015
    Snapshot read, resolving...
    Resolving 4839 objects...
    Chasing references, expect 0 dots
    Eliminating duplicate references
    Snapshot resolved.
    Started HTTP server on port 7000
    Server is ready.
    

    也可以直接
    jmap -histo 5606

    root@ubuntu:~# jmap -histo 5606
    
     num     #instances         #bytes  class name
    ----------------------------------------------
       1:           344        1305352  [I
       2:          5315         727592  <methodKlass>
       3:          5315         640880  <constMethodKlass>
       4:           356         407288  <constantPoolKlass>
       5:           322         257216  <constantPoolCacheKlass>
       6:           356         255536  <instanceKlassKlass>
       7:           428          81568  [B
       8:           879          76496  [C
       9:           416          50712  java.lang.Class
      10:           573          37152  [[I
      11:           536          33704  [S
      12:            43          24768  <objArrayKlassKlass>
      13:           858          20592  java.lang.String
      14:           312          13144  [Ljava.lang.Object;
      15:           107           7704  java.lang.reflect.Field
      16:             8           4608  <typeArrayKlassKlass>
      17:           108           3456  java.util.Hashtable$Entry
      18:            11           2288  <klassKlass>
      19:            52           1904  [Ljava.lang.String;
      20:            38           1824  sun.util.locale.LocaleObjectCache$CacheEntry
      21:            55           1760  java.util.concurrent.ConcurrentHashMap$HashEntry
      22:            40           1600  java.util.concurrent.ConcurrentHashMap$Segment
      23:            16           1344  [Ljava.util.HashMap$Entry;
      24:            40           1280  java.util.concurrent.locks.ReentrantLock$NonfairSync
      25:            40           1088  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
      26:            16           1024  java.net.URL
      27:             6            992  [Ljava.util.Hashtable$Entry;
      28:            14            784  java.util.HashMap
      29:            19            760  java.io.ObjectStreamField
      30:            19            760  sun.util.locale.BaseLocale$Key
    

    jstat

    -class: 统计class loader 行为信息
    
    -compiler: 统计编译行为信息
    
    -gc:统计jdk gc时heap信息
    
    -gccapacity:统计不同的generations(新生代、老生代、永久代)相应的heap容量信息
    
    -gccause:统计gc的情况,以及引起gc的事情。同-gcutil
    
    -gcnew:统计新生代的gc情况
    
    -gcnewcapacity:统计新生代gc时heap的容量信息
    
    -gcold:统计老生代的gc情况
    
    -gcoldcapacity:统计老生代gc时heap容量信息
    
    -gcpermcapacity:统计永久代gc时的容量信息
    
    -gcutil:统计heap的gc情况
    
    -printcompilation:
    

    jstat -gcutil 5606 100 10

    root@ubuntu:~# jstat -gcutil 5606 100 10
      S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
      0.00   0.00   4.00   0.71  11.33      1    0.001     1    0.007    0.008
    

    结果信息:

    S0 — Heap上的 Survivor space 0 区已使用空间的百分比

    S1 — Heap上的 Survivor space 1 区已使用空间的百分比

    E — Heap上的 Eden space 区已使用空间的百分比

    O — Heap上的 Old space 区已使用空间的百分比

    P — Perm space 区已使用空间的百分比

    YGC — 从应用程序启动到采样时发生 Young GC 的次数

    YGCT– 从应用程序启动到采样时 Young GC 所用的时间(单位秒)

    FGC — 从应用程序启动到采样时发生 Full GC 的次数

    FGCT– 从应用程序启动到采样时 Full GC 所用的时间(单位秒)

    GCT — 从应用程序启动到采样时用于垃圾回收的总时间(单位秒)

    jstat -gccapacity 3851 1000 10

    root@ubuntu:~# jstat -gccapacity 5606 1000 10
     NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC      PGCMN    PGCMX     PGC       PC     YGC    FGC 
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
     41792.0 668992.0  41792.0 5184.0 5184.0  31424.0    83648.0  1338048.0    31680.0    31680.0  21248.0  83968.0  21248.0  21248.0      1     1
    
  • 相关阅读:
    jQuery.hover() 函数详解
    深入了解css的行高Line Height属性
    yii2 restfulapi QueryParamAuth验证
    yii2 restfulapi 的配置和访问
    yii2 urlmanager的配置
    xubuntu install nodejs
    使用Putty连接VirtualBox的Ubuntu
    mvc与mvvm
    对二叉树进行广度优先遍历
    JavaScript 中的 FileReader
  • 原文地址:https://www.cnblogs.com/donganwangshi/p/4245852.html
Copyright © 2011-2022 走看看