A, install,

Arthas on making a page, the address is alibaba. Making. IO/arthas.

There are several ways to install:

  1. Download a jar package that can be started and start it in Java-jar mode
  2. Use the official as.sh script for one-click installation
  3. Install using RPM

This article introduces the first method because it is simple and super convenient when you want to migrate (after all, you just need to copy the downloaded JAR package).

curl -O https://alibaba.github.io/arthas/arthas-boot.jar

Copy the code

If the download is too slow, use a source on Gitee

curl -O https://arthas.gitee.io/arthas-boot.jar

Copy the code

The curl command downloads arthas-boot.jar directly to the directory you want

[root@localhost ~]# ll -lrt
-rw-r--r--. 1 root root   138880 Jun 22 02:55 arthas-boot.jar

Copy the code

Second, start,

Start directly with a Java command

[root@localhost ~]# java -jar arthas-boot.jar 
[INFO] arthas-boot version: 3.33.
[INFO] Can not find java process. Try to pass <pid> in command line.
Please select an available pid.

Copy the code

But this startup fails because arthas detects the JVM processes running on the machine when it starts up and then lets the user select the process to bind to. The rest is for the selected process.

Here I start with a Java application and then arthas.

[root@localhost ~]# java -jar arthas-boot.jar 
[INFO] arthas-boot version: 3.33.
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1\. Then hit ENTER.
* [1] :2467 jvm0.01.-SNAPSHOT.jar

Copy the code

Here is a list of Java processes running on the machine, waiting for user input. Type 1 and press Enter. If you need to download some necessary files for the first startup, wait until the download is complete.

[root@localhost arthas]# java -jar arthas-boot.jar 
[INFO] arthas-boot version: 3.33.
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1\. Then hit ENTER.
* [1] :2467 jvm0.01.-SNAPSHOT.jar
1
[INFO] arthas home: /usr/local/arthas
[INFO] Try to attach process 2467
[INFO] Attach process 2467 success.
[INFO] arthas-client connect 127.0. 01. 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O  \ |  .--. "' -.. -- '|  The '-'  | /  O  \ '-'|. -. | |The '-'. '. | | | -. | | - | `. ` - | | | | | | \ \ | | | | | | | | | |. -'| ` --' `--'`--' The '-'   `--'` -'  `--'`--' `--'` -- -- -- -- --'                          

wiki      https://alibaba.github.io/arthas                                      
tutorials https://alibaba.github.io/arthas/arthas-tutorials                     
version   3.33.                                                                 
pid       2467                                                                  
time      2020- 06- 22 03:02:31                                                   

[arthas@2467] $Copy the code

If you see this screen, it indicates that the association is successful.

Help command

In the ArthAS interactive environment, you can type the help command and all arthAS supported commands will appear

[arthas@2467]$ help
 NAME         DESCRIPTION                      
 help         Display Arthas Help                                       
 keymap       Display all the available keymap for the specified connection.  
 sc           Search all the classes loaded by JVM       
 sm           Search the method of classes loaded by JVM      
 classloader  Show classloader info 
 jad          Decompile class 
 getstatic    Show the static field of a class.Copy the code

If you do not know the usage of the command, you can enter the command with the parameter –help, for example, you can see the usage of the sc command

[arthas@2467]$ sc --help
 USAGE:                                    
   sc [-c <value>] [-d] [-x <value>] [-f] [-h] [-E] class-pattern                                                                                     
 SUMMARY:                                                                                                                             
   Search all the classes loaded by JVM                                                                                                       
 EXAMPLES:                                   
   sc -d org.apache.commons.lang.StringUtils   
   sc -d org/apache/commons/lang/StringUtils   
   sc -d *StringUtils                          
   sc -d -f org.apache.commons.lang.StringUtils                                        
   sc -E org\\.apache\\.commons\\.lang\\.StringUtils                                                                                  

 WIKI:                                
   https://alibaba.github.io/arthas/sc                                                                                                                             
 OPTIONS:                                                
 -c, --classloader <value>                    The hash code of the special class's classLoader                                        
 -d, -details                                Display the details of class        
 -x, -expand <value>                         Expand level of object (0 by default)
 -f, --field                                  Display all the member variables 
 -h, --help                                   this help                             
 -E, --regex                                  Enable regular expression to match (wildcard matching by default)                       
 <class-pattern>                              Class name pattern, use either '.' or '/' as separator 

Copy the code

Not only does it show what the command is for, the full parameters of the command, but it also provides some specific examples. If you are not used to reading English, you can also go to the address of the WIKI to see the official document, there is a Chinese version.

Arthas (arthas

(1) The CPU usage is too high

Use the thread command to list information about threads

[arthas@2467]$ thread
Threads Total: 28, NEW: 0, RUNNABLE: 11, BLOCKED: 0, WAITING: 14, TIMED_WAITING: 3, TERMINATED: 0                                     
ID         NAME                              GROUP                 PRIORITY   STATE      %CPU        TIME       INTERRUPTE DAEMON     
16         http-nio- 8080.-exec2 -              main                  5          RUNNABLE   99          0:25       false      true       
29         Attach Listener                   system                9          RUNNABLE   0           0:0        false      true       
11         Catalina-utility- 1                main                  1          WAITING    0           0:0        false      false      
12         Catalina-utility2 -                main                  1          TIMED_WAIT 0           0:0        false      false      
28         DestroyJavaVM                     main                  5          RUNNABLE   0           0:4        false      false      
3          Finalizer                         system                8          WAITING    0           0:0        false      true       
2          Reference Handler                 system                10         WAITING    0           0:0        false      true    

Copy the code

This command lists all threads in order of CPU usage from highest to lowest. If there are too many threads, you can specify the number of output lines with the -n argument.

Thread id = 16; thread id = 16; thread id = 16; thread id = 16; thread id = 16

[arthas@2467]$ thread 16
"http-nio-8080-exec-2" Id=16 RUNNABLE
    at com.spareyaya.jvm.service.EndlessLoopService.service(EndlessLoopService.java:19)
    at com.spareyaya.jvm.controller.JVMController.endlessLoop(JVMController.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)...Copy the code

The problem was located in two steps

(2) Deadlock

Using thread again, the argument is -b

[arthas@2997]$ thread -b
"Thread-3" Id=29 BLOCKED on java.lang.Object@3f20bf9 owned by "Thread-4" Id=30
    at com.spareyaya.jvm.service.DeadLockService.service1(DeadLockService.java:27)
    -  blocked on java.lang.Object@3f20bf9
    -  locked java.lang.Object@2fea801a <---- but blocks 1 other threads!
    at com.spareyaya.jvm.controller.JVMController.lambda$deadLock$0(JVMController.java:37)
    at com.spareyaya.jvm.controller.JVMController$$Lambda$456/748979989.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:748)

Copy the code

This command is as simple as jStack deadlock detection, but I think jStack deadlock detection is a little more intuitive.

(3) Memory leakage

We can use the dashboard command to dynamically check memory usage

If the usage of content is increasing and does not decrease after GC, and gc becomes more frequent later on, it is likely a memory leak.

The heapdump command is used to dump snapshots of the memory, just like jmap

[arthas@23581]$ heapdump --live /root/jvm.hprof
Dumping heap to /root/jvm.hprof...
Heap dump file created

Copy the code

Then import the dump file into Eclipse and use the MAT plugin to analyze it.

Arthas other commands

Arthas also provides many commands for monitoring, such as monitoring the execution time of a method, decompilating class files on the line, and even replacing a class without restarting the Java application. The official usage documentation has been written in too much detail, I will not introduce one by one here, you can try it yourself.

6. Talk about MAT tools

We used the same tool to analyze memory leaks in the previous article and this one — MAT. We have installed the MAT plug-in in Eclipse, which can only be used when opening Eclipse. The problem is that very few mobile Internet companies now use Eclipse as a development tool, and we don’t want to start Eclipse every time we analyze a memory snapshot.

So here are a MAT of independent tool, it is independent of the eclipse, download address is www.eclipse.org/mat/downloa… , you can select the version based on your system.

For example, on Windows, memoryAnalyzer.exe can be started by double-clicking on memoryAnalyzer.exe, and then File->Open Heap Dump… To Open a snapshot file, or click Open a Heap Dump in the Welcome window. If your snapshot file is particularly large and you need to adjust the JVM parameters, modify the MemoryAnalyzer.ini file under Windows to set the -xmx parameter to the appropriate value (the default is 1024M).

On the Overview selection card, you can select what you want to analyze. You can click on the Leak Suspects to analyze possible memory leaks, for example, or click on Histogram to see statistics for instance of each class.

And then focus on the number of instances of much more special, or take up much memory (the regular expressions can also set filters, useful in large projects), and then see if these objects are combined with their own code really need, or because scope set too big cause not timely recovery.

In summary, analyzing memory snapshots is a time-consuming and laborious task, and it is important to gain experience in the analysis. The tool is just to improve the efficiency of the analysis.

As for the commercial version of professional JVM analysis tools like JProfile, you can also go to learn more.