A preface.
Arthas, ali’s open source Jvm tuning tool, is a powerful tool that you’ve all heard of. It’s not just a tuning tool, it has a lot of power. Such as hot loading classes, dynamically updating memory variables and so on.
Jdk built-in tuning commands
When it comes to tuning tools, Jdk built-in commands are also necessary, but this is a brief note that is not the focus of this article. Arthas is much more convenient when it comes to actually seeing the Jvm in action.
- To view which Java programs are currently running:
jps
- To check the MEMORY of the Jvm, run the jmap command
Jmap-histo process ID You can enter the jmap-histo process ID in the file (jmap-histo process ID > test.txt) and open the file. Jmap ‐dump:format=b,file=test.hprof process ID Format :format,file: output file. Jvisualvm can be opened by jVisualVM, jVisualVM is also a JDK tool (graphical), if there is a JDK environment variable configuration, you can directly enter jVisualVM in Windows CMD open. Can also configure the following Jvm parameters derived: – XX: XX: + HeapDumpOnOutOfMemoryError HeapDumpPath =.. / configuration, when the Jvm will heap memory leak, export the dump file, convenient follow-up screening lead to failure of memory
- View Jvm running information: jinfo
1 Startup parameter: jinfo-flag Process ID 2 Running environment information: jinfo-sysprops process ID
- Look at the Jvm stack information: JStack
Jstack process ID = jstack process ID = jstack process ID = jstack process ID
1.Top-p Indicates the id of a process// Lists information about the specified process 2.Press H (capital)// Lists all threads for the process 3.Convert the thread ID with the highest CPU usage to16Base, because Java thread ID is used16It's expressed in base, and top has converted it to decimal representation. Such as:28071 = 6da7 4.Jstack process id | grep - A20 6da7 // Print out nearly 20 lines of code that the thread is executing Copy the code
- View garbage collection statistics: jstat
Jstat -gc process id jstat -gc process id
There are no screenshots except the last one, please try by yourself. All of the above commands are in the JDK /bin directory, and you can view the help through -h, such as jmap -h
Arthas tools
After a long time, I finally arrived at Arthas. I’m not going to write too much. I’ll just list a few commonly used commands, because the online tutorial on the official website is so seamless
Arthas website: arthas.gize. IO /
Here is a look at the common functions (only record, do not do the primary use of the tutorial, see the official website for entry). :
Support for some basic commands, such as cat, PWD, grep, echo, history, etc., those who know Linux should be familiar with these commands. By the way, “>” is also supported to append the output to a file. There is also a session where you can view the number of the currently entered Java process.
Vmoption: View diagnostic parameters for the Jvm, all by default.
(1) You can only see one of them by adding the parameter name, for example: vmOption PrintGCDetails (2) You can change it by using this parameter: vmOption PrintGCDetails true
- Thread: Views resource usage of threads in the Jvm
Thread-n 3 prints the thread stack instead of searching for and converting to the base of the Jvm’s original jstack. You can also run the thread -n 3 -i 1000 command to check the 3 busiest threads in 1 second (2). Thread b displays the resource usage of the current Java process. (3) Thread B displays the deadlock status or lock blocking status. Or you can run the thread –state WAITING command to view the thread in the specified state. The state of the BLOCKED thread is: BLOCKED
- Powerful OGNL command. Arthas’s OGNL command allows you to execute Java code dynamically or modify the values of some variables. This is a very cool feature, application scenarios… I had no idea that when the anti-legitimate code was released into a formal environment, everything would be blocked.
Ognl ‘@[email protected](“hello ognl”)’ calls System out.println. Mean, using the name of the class the full path, and then write your own method name, can call. (2) check the value of the static variables: ognl – c class loader hash @ com. Example. The demo. Arthas. User. UserController @ logger; This command needs to add the hash value of the class loader, so first use the sc -d classpath to find the class loader that loads the class, for example: Sc – d com. Example. Demo. Arthas. User. UserController | grep classLoaderHash can add – x parameter specifies the need of the layer (when a static variable as the object, (3) ognl expression, official example: ognl ‘#value1=@System@getProperty(“java.home”), #value2=@System@getProperty(“java.runtime.name”), {#value1, #value2}’ Declare value1 and value2 temporary variables and assign values to them, and return a List. More I did not go to understand, because did not use, estimate learned also forget quickly.
- View Jvm information:
Jvm
, you can view heap memory allocation, and gc (including GC algorithm)- Dump heap memory information:
heapdump /tmp/dump.hprof
- View the Jvm Monitoring panel:
dashboard
, you can see the resource usage of the thread, as well as the heap memory usage and GC.- Quick check to see if the method works properly:
tt -t -n 10 demo.MathGame primeFactors
-n 10: Prints 10 times- View the time taken to execute a method:
trace demo.MathGame run -n 10
If you want to filter, you can add an expression:trace demo.MathGame run -n 10 '#cost > 10'
; If you want to observe the execution of multiple methods on multiple classes (typically by checking the call chain), you can use an expression:trace -E com.test.ClassA|org.test.ClassB method1|method2|method3
Arthas hot update class
This one is the most used, so write it out separately. There are several ways to hot-update a class:
- Method 1: Compile the class locally, upload it to the server, and then go to Arthas and execute the command:
Define/TMP/testdemo.class re-define/TMP/testdemo.class If you try to fix one bug and two bugs appear… You need to restore the native code again, compile it, upload it, and re-define it again. retransform /tmp/TestDemo.class, (3) retransform -l shows a list of classes loaded by retranform, and (4) retransform -d id removes the specified classes. The ID is obtained from the previous command. You can also deleteAll hot loaded classes using retransform –deleteAll. Retransform –classPattern com.test.TestDemo retransform –classPattern com.testdemo I personally prefer re-define, local test can be done. There!!!!!! Re-define or ReTransform cannot hot-update a class that has changed a method signature or a class member variable. Method signature: method scope, return value, method name, method parameters
- Method 2: the standard process of the official website, the intermediate steps can be omitted according to the situation:
(1) jad — source.testdemo > / TMP /TestDemo | grep classLoaderHash find hash value of the class loader, general printing is as follows: Java -d/TMP Compilers the modified Java file to the specified directory. -c specifies the classloader. -d Specifies the compile directory. (5) Run the re-define or retransform command to update the class
The end of the full text, but still hope that small partners to the official website online try, absolutely benefit. Then you are also welcome to correct ~