After the project is deployed online, the online project cannot be easily modified, and the difficulty of locating problems will become greater. Therefore, monitoring is a very important link, with monitoring, we can better locate the problems in the system, so as to investigate. There are many kinds of monitoring tools, but the Java command line monitoring tool is a must to master.

jps

Other commands typically use JPS to view the process number and then get JVM process information based on the thread number

JPS -m looks at the JVM process with arguments. JPS -v looks at arguments passed to the JVM

The official explained the JPS docs.oracle.com/javase/7/do…

jstat

Jstat – [-t] [-h] [[]] For example, to view JIT compilation information, GC information, and class information in the JVM.

To explain the meaning of the columns in the -GC option: -GC looks at the information in the garbage collector, mainly the runtime data area statistics for the JVM. The suffix C represents the current capacity, the suffix U represents how much capacity has been used, and the suffix T represents the elapsed time

Capacity of VIABILITY zone 0 in S0C (KB) Capacity of viability zone 1 in S1C (KB) Space used by viability zone 0 in S0U (KB). Space used by viability zone 1 in S1U (KB) OU Used capacity of the old age (KB). PC Current capacity of the permanent band (KB). PU Used capacity of the permanent band (KB). YGC Number of times that Young GC occurs GC collection time GCT Total GC time.

The official jstat explanation: docs.oracle.com/javase/7/do…

jstack

View thread stack information. Use this command to find deadlocks when deadlocks occur or to troubleshoot dead loops when deadlocks occur.

Jstack vmpID prints stack information for the thread. Using the stack to see what code is being executed by a particular thread, Xiamen demonstrates two commands that use jStack to check for deadlocks and deadloops.

Deadlock code:

public class DeadLock {

    private static Object o1 = new Object();
    private static Object o2 = new Object();
    private static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {
        new Thread(){
            @Override
            public void run(a) {
                synchronized (o1){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread 1 acquires lock 1");
                    synchronized (o2){
                        System.out.println("Thread 1 acquires lock 2");
                        countDownLatch.countDown();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run(a) {
                synchronized (o2){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread 2 acquires lock 2);
                    synchronized (o1){
                        System.out.println(Thread 2 acquires lock 1);
                        countDownLatch.countDown();
                    }
                }
            }
        }.start();

        countDownLatch.await();
        System.out.println("Executed"); }}Copy the code

After the deadlock: You can see that JStack has found the deadlock for us.

The Top command is used together with the infinite loop command. As the infinite loop leads to continuous CPU surge, run the Top command to view the commands with high CPU usage.

Run the top-h-P 24278 command to check which thread in the process has the highest CPU usage

As you can see, the thread 24279 is in base 10 in top, and the thread printed in jstack is in base 16, so do a conversion. 24279 => 5ed7 then view the thread:

Then we can see that the specific line of instruction is running all the time.

Jstack official documentation

Jinfo looks at the parameter values that were set when the JVM started

Jinfo can view system properties of the current JVM thread configuration, as well as parameter values set at run time.

Use jinfo directly

  1. The first half is a property of the system

  2. The second half is the JVM’s parameters

We can also use jinfo directly to view a specific parameter value:

Jinfo official explanation

Jmap analysis of pile

When OME occurs, jMap is used to analyze exactly what the problem is in the heap to better solve the problem. Jmap is usually used in conjunction with MAT.

In general, at the start of the Java development project, it is best to add the following command, when the memory overflow can be viewed through the log information. -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/java/dump

Of course, you can also use jmap-heap jvmpID to view the object memory mapping while the project is running.

Demo memory overflow

Heap memory overflow code

public class DealCycle {
    public static LinkedList<Integer> linkedList = new LinkedList<>();
    public static void main(String[] args) {
        int i = 0;
        while (true){ linkedList.add(i); }}}Copy the code

Run:

You can go to the website to download Java Mat binary compression package, learn

Online analysis of hprof file: heaphero.io/

If it’s too late to export files every time they run out of memory, you can export them directly using JMap

Jmap official explanation

The last

This article focuses on Java’s own command-line tools, which give you a good idea of the current state of the JVM.