JVM combat: JVM command line monitoring tool

insert image description here

introduce

In the production environment, various performance problems are often encountered, so it is necessary to master the most basic JVM command-line monitoring tools

name main function
jps View running Java processes
jstack print thread snapshot
jmap Export heap memory image file
jstat View jvm statistics
jinfo View and modify jvm configuration parameters in real time
jhat Used to analyze heapdump files

jps: View running Java processes

jps can list the running Java processes and display the name of the virtual machine execution main class (Main Class, the class where the main() function is located) and the process id

If you want to see a command, add the -help parameter after it

[root@VM-0-14-centos ~]# jps -help
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

Definitions:
    <hostid>:      <hostname>[:<port>]

It can be seen that remote services can be monitored, but based on security considerations, it is generally not used

Common options are as follows

Options effect
-q output only process id
-m Output the parameters passed to the main class main function
-l Output the full class name of the main class, if the process executes a Jar package, output the name of the jar package
-v JVM parameters specified at program startup
cis@mt002:~$ jps
70208 KmpService
183525 LinkAnalysisServer
25160 MipSerachServer
cis@mt002:~$ jps -l
70208 com.st.kmp.main.KmpService
183525 com.st.cis.main.LinkAnalysisServer
25160 com.st.cis.main.MipSerachServer

jstack: print thread snapshot

View the status of all threads in a Java process. It is generally used to locate the reason for the long pause of the thread, such as the occurrence of an infinite loop, deadlock, requesting external resources to wait for a long time, etc.!

public class DeadLockDemo {
    
    

    private static Object lockA = new Object();
    private static Object lockB = new Object();


    public static void main(String[] args) {
    
    

        Thread threadA = new Thread(() -> {
    
    
            synchronized (lockA) {
    
    
                try {
    
    
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                System.out.println("get lockA");
                synchronized (lockB) {
    
    
                    System.out.println("threadA run finish");
                }
            }
        });

        Thread threadB = new Thread(() -> {
    
    
            synchronized (lockB) {
    
    
                try {
    
    
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                System.out.println("get lockB");
                synchronized (lockA) {
    
    
                    System.out.println("threadB run finish");
                }
            }
        });

        threadA.setName("myThreadA");
        threadB.setName("myThreadB");
        threadA.start();
        threadB.start();
    }
}

I wrote an example of deadlock, execute jps after startup to find the process id is 19457

peng@pengdeMacBook-Pro ~ % jps
19457 DeadLockDemo
19458 Launcher
2658 
19459 Jps

Then execute the following command to list the execution status of each thread of this process

jstack 19457

insert image description here
It can be seen from the figure that there are many threads, GC threads, myThreadA, myThreadB, etc. The thread status of myThreadA and myThreadB is BLOCKED, and the location of the deadlock is indicated at the end

jmap: export heap memory image file

jmap is mainly used to export heap memory image files to see if memory leaks occur.

In the production environment, we generally configure the following parameters to allow the virtual machine to automatically generate a dump file after an OOM exception occurs.

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/peng

Execute the following command to manually obtain the dump file

jmap -dump:file=文件名.dump 进程id

There are many tools for analyzing heap memory, such as Java VisualVM, jhat, etc. But personally I think the best one is Eclipse Memory Analyzer, none of them

jstat: View jvm statistics

jstat can display running data such as class loading, memory, garbage collection, JIT compilation, etc. in local or remote virtual machine processes

Use jstat to view class loading information. I personally rarely use this command. It is not as convenient to read garbage collection information on the command line as to see the graphical interface, so I won't introduce it further.

[root@VM-0-14-centos ~]# jstat -class 19402
Loaded  Bytes  Unloaded  Bytes     Time   
 10229 19679.1       52    76.0       5.33

The meaning is as follows

Loaded explain
Loaded number of loaded classes
Bytes The number of bytes to load the class
Unloaded Number of uninstalled classes
Bytes Bytes of unloaded classes
Time time spent

jinfo: View and modify jvm configuration parameters in real time

The role of jinfo is to view and modify various parameters of the virtual machine in real time.

Use the -v parameter of the jps command to view the list of parameters explicitly specified when the virtual machine starts , but if you want to know the system default values ​​of parameters that are not explicitly specified, you can only use jinfo's -flag in addition to looking for information The option is queried (if it is limited to JDK 1.6 or above, it is also a good choice to use java -XX:+PrintFlagsFinal to view the default value of the parameter)

insert image description here
jinfo flags pid can only be used normally in high version, I can't use it normally in jdk1.8 version

The application sets the following parameters

-Xmx10m -Xms10m

Check the maximum heap memory and whether to print the GC log. You can see that the GC log is not printed.
Then set to print the GC log, check it again, and print the GC log.

peng@pengdeMacBook-Pro ~ % jinfo -flag MaxHeapSize 20253         
-XX:MaxHeapSize=10485760
peng@pengdeMacBook-Pro ~ % jinfo -flag PrintGCDetails 20253
-XX:-PrintGCDetails
peng@pengdeMacBook-Pro ~ % jinfo -flag +PrintGCDetails 20253
peng@pengdeMacBook-Pro ~ % jinfo -flag PrintGCDetails 20253 
-XX:+PrintGCDetails

Xmx is an alias for MaxHeapSize

Reference blog

[1] https://www.cnblogs.com/yueshutong/p/9812464.html

Guess you like

Origin blog.csdn.net/zzti_erlie/article/details/123383455