Depth understanding of java virtual machine Second Edition (four) virtual machine performance monitoring and troubleshooting tool

JDK command line tools (jps: VM process status tool, jstat: Virtual Machine statistics monitoring tool, jinfo: Java configuration tool, jmap: Java memory imaging tools, jhat: a snapshot of the virtual machine heap dump analysis tools, jstack: Java stack trace tool ); visualization tools (JConsole, VisualVM)

 

A. JDK command-line tool

  • jps: JVM Process Status Tool, to display all the HotSpot virtual machine process within the specified system
  • jstat: JVM Statistics Monitoring Tool, users collect HotSpot virtual machine to run all aspects of data
  • jinfo: Configuration Info for Java, virtual machine configuration information display
  • Memory Dump Snapshot Memory Map for java, generated virtual machine: jmap
  • jhat, JVM Heap Dump Browser, user analysis heapdump file, it will create a HTTP / HTML server, so that users can view the results in a browser
  • jstack, Stack Trace for Java, a snapshot of the virtual machine display thread

1 jps: VM process status tool

jsp command format:

jps [options] [hostid]

jps执行样例:
D:\Develop\Java\jdk1.6.0_21\bin>jps-l
2388 D:\Develop\glassfish\bin\..\modules\admin-cli.jar
2764 com.sun.enterprise.glassfish.bootstrap.ASMain
3788 sun.tools.jps.Jps

 

2 jstat: Virtual Machine Statistics Monitoring Tool

jstat command format is:

jstat [option vmid[interval[s|ms][count]]]

Parameters interval and represents the query interval and count the number of times, if you omit both parameters, indicating that the query only once. Suppose you want to query every 250 milliseconds garbage collection process conditions 2764, a total of 20 inquiries, that the command should be:

jstat -gc 2764 250 20

 

jstat Apply example: jstat -gcutil 2764

S0 and S1 P FGC FGCT YGCT YGC GCT
0.00 0.00 6.20 41.42 47.20 16 0. 105 3 0,472 0,577

Query results showed that: the new generation Eden District this server (E, expressed Eden) using 6.2% of the space, two
Survivor areas (S0, S1, represents Survivor0, Survivor1) which are empty, the old year (O, He represents Old), and
the permanent generation of (P, represents permanent) respectively using 47.20% and 41.42% of the space. Since the program runs were
Minor GC (YGC, represents Young GC) 16 times, the total time 0.105 seconds, occurred Full GC (FGC, expressed Full
GC) 3 times, Full GC total elapsed time (FGCT, expressed Full GC Time) to 0.472 seconds, all GC total elapsed time (GCT,
expressed GC Time) was 0.577 seconds

 

3 jinfo: Java configuration tool

jinfo command format:

jinfo [option] pid

Apply example: CMSInitiatingOccupancyFraction query parameter values.
C: \> jinfo CMSInitiatingOccupancyFraction In Flag-1444
-XX: CMSInitiatingOccupancyFraction = 85

 

4 jmap: Java memory imaging tools

jmap command format:

jmap [option] vmid

使用jmap生成dump文件
C:\Users\IcyFenix>jmap-dump:format=b,file=eclipse.bin 3500
Dumping heap to C:\Users\IcyFenix\eclipse.bin……
Heap dump file created

 

5 jhat: a snapshot of the virtual machine heap dump analysis tool

Use jhat analysis dump file

C:\Users\IcyFenix>jhat eclipse.bin
Reading from eclipse.bin……
Dump file created Fri Nov 19 220721 CST 2010
Snapshot read,resolving……
Resolving 1225951 objects……
Chasing references,expect 245 dots……
Eliminating duplicate references……
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

Users in the browser, type http: // localhost: 7000 / can see results

 

6 jstack: Java stack trace tool

jstack command format:

jstack[option]vmid

 

 

Two. JDK visualization tools

JDK in addition to providing a large number of command-line tools, there are two powerful visualization tools: JConsole and VisualVM

1 JConsole: Java Monitoring and Management Console

1.1 Start JConsole

By "jconsole.exe" start in JDK / bin directory

 

 

 

 1.2 Memory Monitor

JConsole to monitor code

/ ** 
 * memory placeholder object, a target OOMObject approximately 64KB 
 * -Xms -Xmx 100m 100m -XX: + UseSerialGC 
 * / 
public  class OOMObject {
     public  byte [] = placeholder new new  byte [64 * 1024 ]; 
}
public class OOMObjectTest1 {
    public static void fillHeap(int num) throws InterruptedException {
        List<OOMObject> list = new ArrayList<OOMObject>();
        for (int i = 0; i < num; i++) {
            Thread.sleep(100);
            list.add(new OOMObject());
        }
        System.gc();
    }

    public static void main(String[] args) throws InterruptedException {
        fillHeap(1000);
    }
}

 

 

Running the program in the "Memory" tab can see the trend run Eden memory pool area presents a polygonal line, and can be seen from the chart, at the end of 1000 cycles to perform, run System.gc () after, though whole new generation of Eden and Survivor areas have basically been cleared, but the histogram represents the old era remain peak condition, which is filled into the heap data still alive after the System.gc () method performs

 

1.3 Monitoring thread 

Thread waits demo code

public  class OOMObjectTest2 {
     / ** 
     * demonstrate infinite loop thread 
     * / 
    public  static  void createBusyThread () { 
        the Thread Thread = new new the Thread ( new new the Runnable () {
             public  void RUN () {
                 the while ( to true ) // line 41 
                ; 
            } 
        } , "testBusyThread" ); 
        Thread.start (); 
    } 
    / ** 
     * thread lock waiting for the demo 
     * / 
    public  static  void createLockThread (final Object lock){
        Thread thread=new Thread(new Runnable(){
            public void run(){
                synchronized(lock){
                    try{
                        lock.wait();
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        },"testLockThread");
        thread.start();
    }
    public static void main(String[]args)throws Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        br.readLine();
        createBusyThread();
        br.readLine();
        Object obj=new Object();
        createLockThread(obj);
    }
}

After the program runs, first select the "Threads" tab in the main thread, the stack trace display BufferedReader waiting in readBytes System.in keyboard input method, then thread Runnable state, the thread Runnable state will be assigned run time, but readBytes method checks to immediately return the token stream execution has not been updated, the wait only consume very little CPU resources.

 

 

After entering the value keyboard, then monitor testBusyThread threads, which has been carrying out air circulation, seen from the stack trace has been in line 41 MonitoringTest.java code stays, 41 behavior: while (true). This time thread Runnable state, and did not return action execution thread token, it will run out of space on all execution cycle time until the thread switch, the wait will consume more CPU resources

 

 

testLockThread thread in the event of waiting for the lock object notify or notifyAll method, then thread waiting in the WAITING state, will not be assigned to perform time before being awakened

 

 

 

 Deadlock code samples

public class SynAddRunalbe implements Runnable {
    int a,b;
    public SynAddRunalbe(int a,int b){
        this.a=a;
        this.b=b;
    }

    public void run(){
        synchronized(Integer.valueOf(a)){
            synchronized(Integer.valueOf(b)){
                System.out.println(a+b);
            }
        }
    }
}
public class OOMObjectTest3 {
    public static void main(String[]args){
        for(int i=0;i<100;i++){
            new Thread(new SynAddRunalbe(1,2)).start();
            new Thread(new SynAddRunalbe(2,1)).start();
        }
    }
}

After the thread deadlock appears, click JConsole thread panel "Deadlock detected" button, there will be a new "dead lock" tab.

 

 

2 VisualVM: in-one troubleshooting tool

2.1 Start JConsole

By "jvisualvm.exe" start in JDK / bin directory

 

 

2.2 plugin download

 "Tools" → "widget" → "is the carrier" menu, and then specify the path nbm package dialog box that can be installed, the plug is mounted is stored in JDK_HOME / lib / visualvm / visualvm in

Plug-center address: http: // Visualvm java.net/pluginscenters.html.

 

 

Disclaimer: This article is a personal study notes, content from "in-depth understanding of the Java Virtual Machine · JVM advanced features and best practices" Zhou Zhiming and online articles

Guess you like

Origin www.cnblogs.com/xyzshm/p/12602120.html