jconsole introduction

Article source: https://blog.csdn.net/qq_31156277/article/details/80035430

@JConsole Introduction

1.1 JConsole description

Jconsole (Java Monitoring and Management Console), a visual monitoring and management tool based on JMX.

1.2 Start JConsole

  • Click under the JDK/bin directory jconsole.exeto start
  • Then it will automatically search for all virtual machine processes running on this machine.
  • Select one of the processes to start monitoring

JConsole connection


1.3 Basic introduction to JConsole

JConsole Basic includes the following basic 概述functions: 内存, 线程, , VM概要, ,MBean

Run the following program, and then use JConsoleto monitor; pay attention to setting the virtual machine parameters

import java.util.ArrayList;
import java.util.List;

/**
 *  设置虚拟机参数:
 *  -Xms100M -Xms100m -XX:+UseSerialGC -XX:+PrintGCDetails
 */
public class JConsoleTool {
    
    

    static class OOMObject {
        public byte[] placeholder = new byte[64 * 1024];
    }

    public static void fillHeap(int num) throws InterruptedException {
        Thread.sleep(20000); //先运行程序,在执行监控
        List<OOMObject> list = new ArrayList<OOMObject>();
        for (int i = 0; i < num; i++) {
            // 稍作延时,令监视曲线的变化更加明显
            Thread.sleep(50);
            list.add(new OOMObject());
        }
        System.gc();
    }

    public static void main(String[] args) throws Exception {
        fillHeap(1000);
        while(true){
            //让其一直运行着
        }

    }
}

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

1.3.1 Memory Monitoring

Compared with the visual jstat command, the memory tab is used to monitor the memory of the virtual machine managed by the collector.

Options description
Eden Space the size of 27328KB
used is using
submitted 27328KB
Max 27328KB
0.120s on copy (3 collections) The new generation uses the assignment algorithm (copy), 0.120s, a total of three times
0.037 (1 collection) on MarkSweepCompact The old age uses mark clearing and sorting, which takes 0.037, a total of one time

The corresponding GC log.

[GC (Allocation Failure) [DefNew: 27277K->3392K(30720K), 0.0349173 secs] 27277K->14749K(99008K), 0.0350411 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 

[GC (Allocation Failure) [DefNew: 30691K->3378K(30720K), 0.0446635 secs] 42049K->39217K(99008K), 0.0447387 secs] [Times: user=0.03 sys=0.01, real=0.04 secs] 

[GC (Allocation Failure) [DefNew: 30679K->3372K(30720K), 0.0408609 secs] 66518K->64734K(99008K), 0.0409604 secs] [Times: user=0.02 sys=0.02, real=0.04 secs] 

[Full GC (System.gc()) [Tenured: 61362K->66352K(68288K), 0.0372192 secs] 67024K->66352K(99008K), [Metaspace: 9535K->9535K(1058816K)], 0.0373411 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.3.2 Thread monitoring

If the "Memory" tab above is equivalent to the visual jstat command, the function of the "Thread" tab is equivalent to the visual jstack command. You can use this tab to perform monitoring and analysis when a thread is stalled. The main thread long pause are: 等待外部资源(database connectivity, network resources, equipment, capital
sources, etc.), , 死循环(live锁等待 locks and deadlocks)


The following three methods are waiting for console input, infinite loop demonstration, thread lock waiting for demonstration

/**
  * 等待控制台输入
  * @throws IOException
  */
 public static  void waitRerouceConnection () throws IOException {
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     br.readLine();
 }
/**
 * 线程死循环演示
 */
 public static void createBusyThread() {
     Thread thread = new Thread(new Runnable() {
         @Override
         public void run() {
             while (true)   // 第41行
                 ;
         }
     }, "testBusyThread");
     thread.start();
 }

 /**
  * 线程锁等待演示
  */
 public static void createLockThread(final Object lock) {
     Thread thread = new Thread(new Runnable() {
         @Override
         public void run() {
             synchronized (lock) {
                 try {
                     lock.wait();
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
         }
     }, "testLockThread");
     thread.start();
 }
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

(Two) thread deadlock demonstration

This code opens 200 threads to calculate the values ​​of 1+2 and 2+1 respectively. In fact, the for loop can be omitted. Two threads may also cause deadlock. However, the probability is too small and you need to try to run many times. To see the effect. Generally speaking, if the version with for loop runs at most 2 to 3 times, it will encounter thread deadlock and the program cannot end. The reason for the deadlock is that the Integer.valueOf() method is based on the consideration of reducing the number of object creations and saving memory. The number between [-128, 127] will be cached [3], when the valueOf() method passes in the parameter here Within the scope, the object in the cache will be returned directly. In other words, the Integer.valueOf() method was called 200 times in the code and only two different objects were returned. If a thread switch occurs between the two synchronized blocks of a certain thread, then thread A is waiting for Integer.valueOf(1) held by thread B, and thread B is waiting for thread A to hold Integer.valueOf (2), we have the results appear
not run down the scene.

package com.jvm;

/**
 * 线程死锁验证
 */
public class JConsoleThreadLock {
    
    

    /**
     * 线程死锁等待演示
     */
    static class SynAddRunalbe implements Runnable {
        int a, b;
        public SynAddRunalbe(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public void run() {
            synchronized (Integer.valueOf(a)) {
                synchronized (Integer.valueOf(b)) {
                    System.out.println(a + b);
                }
            }
        }
    }

    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();
        }
    }


}

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

Thread deadlock

Result description: shows that the thread Thread-53is waiting for an Thread-66Integer object held by the thread , and clicking on Thread Thread-66 shows that it is also waiting for an Integer object Thread-53held by the thread , so that the two threads are stuck with each other, and there is no waiting until the lock is released Hope


Two, VisualVM introduction

VisualVM (All-in-One Java Troubleshooting Tool); The most powerful operation monitoring and troubleshooting program

2.1 Function description

  • VM process and display process 配置, 环境信息( jps, jinfo).
  • For monitoring applications CPU, , GC, method area (1.7 and earlier), 元空间(JDK1.8 and later) and 线程information (jstat, jstack).
  • dump and analysis 堆转储快照(jmap, jhat).
  • At the method level 程序运行性能分析, find the method that is called the most and has the longest running time .
  • Offline program snapshot: collect the program's runtime configuration, thread dump, memory dump and other information to create a snapshot

2.2 Tutorial

How to use it, just check the official website and this book tutorial.


See information

Guess you like

Origin blog.csdn.net/aiwaston/article/details/104936444