Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Arthas to troubleshoot JVM memory

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use Arthas to troubleshoot JVM memory", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Arthas to troubleshoot JVM memory.

What is Arthas?

When our system encounters problems such as JVM or memory overflow, how to effectively monitor and check our programs, we find several commonly used tools, such as jconsole and jvisualvm, which come with JDK, and a best tool-- jprofiler, but this is a charge, or except for rich companies, few people will use this, and another is our protagonist today-- Arthas. Why do we focus on this today?

Official website address: http://arthas.gitee.io/ GitHub address: https://github.com/alibaba/arthas/

Arthas is Alibaba open source Java diagnostic tool, using command-line interaction mode, provides a wealth of features, mainly or it is free of charge inside is easy to use and more powerful plug-in for JVM troubleshooting, after understanding this sharp tool, it is found that it is quite easy to use, and the features supported are more comprehensive, so what can Arthas do for us after all?

Provide performance Kanban, including thread, cpu, memory and other information, and will refresh regularly.

View thread snapshots according to various conditions. Find out the n threads with the highest cpu occupancy

Output all kinds of information about jvm, such as gc algorithm, jdk version, ClassPath, etc.

Unable to debug online due to problems, hot deployment plus log replacement directly

View the static properties of a class, or execute some statements through the ognl syntax

View the details of the loaded class, from which jar package the class was loaded, and view the information about the methods of the class

The bytecode of the dump class to the specified directory

Directly decompile the specified class

Quickly locate the hot spots of the application and generate the flame diagram

The real-time running status of JVM can be monitored.

In the past, when you encountered these problems, most of the solutions were to modify the code and go online again. But in large companies, the process of going online is very tedious, and it is undoubtedly very troublesome to rerelease the version in order to add an extra line of logs. But Alibaba's open source Arthas has a more elegant method of online debugging.

Arthas supports JDK6 and can be run on Linux/Mac/Windows. Automatic Tab completion makes it more convenient for us to locate and diagnose problems.

Download address: https://arthas.gitee.io/download.html you can download the zip package I downloaded is arthas-packaging-3.5.0-bin.zip or through the command to download

Wget https://alibaba.github.io/arthas/arthas-boot.jar

User manual 1. Quick start

After we download it, we can java-jar arthas-boot.jar directly with the command, but before that, we need to connect to the Arthas through the detected code.

Import java.math.BigDecimal;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class FullGCTest {/ / simulated bank card class private static class CardInfo {/ / Bank card information records of small farmers BigDecimal price = new BigDecimal (10000000.0); String name = "herdsmen and small farmers"; int age = 18 Date birthdate = new Date (); public void m () {}} / / timed thread pool / / 50 thread pools, then set reject policy private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor (50, new ThreadPoolExecutor.DiscardOldestPolicy ()); public static void main (String [] args) throws Exception {executor.setMaximumPoolSize (50); for (;;) {modelFit () Thread.sleep;}} / * risk assessment for bank cards * / private static void modelFit () {List taskList = getAllCardInfo () / take out each message and taskList.forEach (info-> {/ / do something executor.scheduleWithFixedDelay (()-> {/ / call M method info.m ();}, 2,3, TimeUnit.SECONDS);} private static List getAllCardInfo () {List taskList = new ArrayList () / / for (int I = 0; I) for each query of 100 cards.

< 100; i++) { CardInfo ci = new CardInfo(); taskList.add(ci); } return taskList; }} 这个是上篇文章讲述的案例,感兴趣的可以了解一下。 首先我们需要使用javac 命令将Java文件进行编译javac FullGCTest.java进行编译,然后打印GC日志,进行风险监控打印GC日志: java -Xms200M -Xmx200M -XX:+PrintGC FullGCTest Arthas启动命令:java -jar arthas-boot.jar

We see the FullGCTest application we just started. We enter the number 1 to enter, so we attach the Arthas to our program, and then we just need to do the corresponding command operation.

Command details document: https://arthas.aliyun.com/doc/commands.html

two。 The function list command details jvm view current JVM information thread view current JVM thread stack information watch method execution data observation dashboard current system real-time data panel trace method internal call path, and output time-consuming stack on each node on the method path output the current method called call path tt method execution data space-time tunnel, record the input parameters and return information of each call of the specified method And can observe the vmoption view of these different time calls, update JVM loaded class information sc view JVM loaded class information sm view loaded class method information jad decompiled specified loaded class source code classloader view classloader inheritance tree, urls, class loading information heapdump similar to the heapdump function jvm of the jmap command

OPERATING-SYSTEM: system-related parameters

THREAD related:

COUNT: the number of threads currently active in JVM

DAEMON-COUNT: the number of daemon threads currently active in JVM

PEAK-COUNT: the maximum number of threads that have been alive since JVM was started

STARTED-COUNT: the total number of threads started since JVM startup

DEADLOCK-COUNT: the number of threads currently deadlocked in JVM

MEMORY

FILE-DESCRIPTOR (file descriptor dependent):

Maximum number of file descriptors that can be opened by the MAX-FILE-DESCRIPTOR-COUNT:JVM process

Number of file descriptors currently open by OPEN-FILE-DESCRIPTOR-COUNT:JVM

Thread command

Parameter description:

Command details id thread id [n:] specify the first N busiest threads and print the stack [b] to find out which threads are currently blocking other threads [I] specify the sampling interval for cpu utilization statistics in milliseconds. The default value is 200 [--all] to show all matching threads.

Print the current busiest N threads and print stack thread-n 3

Thread view all threads

Thread 17: displays the running stack of the specified thread

Thread-I: specify the sampling interval

Thread-I 1000: counts the CPU time of threads in the recent 1000ms. Thread-n 3-I 1000: list the 3 busiest thread stacks in 1000ms

Dashboard command

When you run the program, it displays real-time information about the current program, such as qps, rt, number of errors, thread pool information, and so on.

Data description:

ID: thread ID at Java level

NAME: thread name

GROUP: thread group name

PRIORITY: thread priority, a number between 1 and 10. The higher the priority, the higher the priority.

STATE: the state of the thread CPU%: the cpu utilization of the thread. For example, if the sampling interval 1000ms and the increment cpu time of a thread is 100ms, then the cpu utilization rate is 100 Universe 1000mm 10%.

DELTA_TIME: incremental CPU time for threads to run since the last sampling. Data format is seconds.

TIME: the total CPU time of the thread running. The data format is minutes: seconds.

INTERRUPTED: the current interrupt bit state of the thread

DAEMON: whether it is a daemon thread

Parameter description:

The parameter name details the time interval (ms) for id to refresh real-time data, and the default number of times 5000ms [n:] refreshes real-time data sc command

Look at the class information that JVM has loaded. Through SC, we can see the details of our class, including which jar package it is read from, whether it is an interface / enumerated class, and even which class loader it is loaded from.

Parameter description:

Parameter name details class-pattern class name expression match method-pattern method name expression match [d] output details of the current class, including the source of the original file loaded by this class, the declaration of the class, the loaded ClassLoader, and so on. If a class is loaded by multiple ClassLoader, multiple times [E] turns on regular expression matching. The default is wildcard matching.

Sc-d * CardInfo: print the details of the class

Sc-d-f * CardInfo: print the Fiedld information of the class

Heapdump + jhat analysis

Heapdump: similar to the jmap command

Create it under the specified folder:

[arthas@365564] $heapdump / usr/local/mxn/dump.hprofDumping heap to / usr/local/mxn/dump.hprof... Heap dump file created

After the creation is successful, we can see the corresponding dump file under the specified folder, and then use the command jhat dump.hprof to generate the file. After success, we can access it through the IP+ port.

Visit:

Then we can access it through the IP+ port. There is an other in it. We pull it to the bottom and find Show instance counts for all classes (including platform).

From the following, we can analyze which class contains the most objects and which class produces objects.

The most powerful function in this is still called Execute Object Query Language (OQL) query, which can show which objects are there, how many bytes and references there are, and you can observe which objects have problems. As shown in the following figure, all the objects corresponding to String are displayed.

After entering the search point, we can also see how many bytes the object occupies and how many references point to the Object. The syntax of this OQL is also very flexible. We can use the where condition to filter.

Jad

Jad: decompile a class, or decompile a method of a class, and the problem of dynamic proxy generation of the class locates the third-party version of the class (observe the code) (determine whether your newly submitted version is used)

Some people may ask what's the use of this? don't I know the source code by myself? Because sometimes we are not sure whether the package in the online or test environment has been modified, we can use jad decompilation to see if it is the latest code.

Redafine

Redafine: hot replacement, dynamic code update, no need to restart jvm. At present, there are some restrictions: you can only change the method implementation (the method has been run), the method name cannot be changed, and the attribute m ()-> mm () cannot be changed.

For example, our online environment has a class to confirm that there is a problem, and if we want to replace it, we can only stop the server and reissue it. This is OK in an ordinary small company, but in a large-scale company like JD.com Taobao, we can't stop, because the whole process is very complicated, so what should we do? You can see the following case

First, let's create a new test case:

Public class T {public static void main (String [] args) throws Exception {for (;;) {System.in.read (); new TT () .m ();}} public class TT {public void m () {System.out.println (2);}}

Use the command javac * .java, compile to a class file, and then run the T file

[root@VM-0-7-centos t] # java Ta22

Print 2 when we enter a, but when we go online, we find that we need to output 1, this is if we want to republish the local change, for this modification, it is obviously not worth it, but if we use redafine hot deployment, it can help us directly replace, do not have to reissue jvm

Then we attach the T program to Arthas.

Then we directly modify the TT.java program vi TT.java to change the value of print 2 to 1.

Public class TT {public void m () {System.out.println (1);}}

Then compile and execute ```javac TT.java ````

Execute redefine / usr/local/mxn/fuccGc/t/TT.class on the Arthas to which we are attached

[arthas@398842] $redefine / usr/local/mxn/fuccGc/t/TT.classredefine success, size: 1, classes:TT

As you can see from the successful execution, we successfully replaced the class file without restarting

Watch

Watch: data observation performed by the method. You can monitor a class through watch instructions. After monitoring, run your function and reproduce the scenario. Arthas will provide you with specific input and output parameters to help you troubleshoot.

Trace

Output method call path, and output time-consuming, this instruction is very useful for optimizing the code, you can see the specific execution time of each method, if it is repeated statements such as for loops, you can also see the maximum time-consuming, minimum time-consuming, and average time-consuming in n loops, perfect!

Tt

After we enable tt for a method, we record the input parameters and return parameters for each call (we can set the maximum number of monitoring times), and we can observe these downgrades at different times.

[arthas@405136] $tt-t FullGCTest modelFit

The command parameter parsing-t tt command has many primary parameters, and-t is one of them. This parameter indicates that you want to record each execution of the print method of class * Test. -n 3 when you execute a method with a low amount of call, you may still have enough time to interrupt the recording process of the tt command with CTRL+C, but if you encounter a method with a very large amount of call, you can burst your JVM memory in an instant.

At this point, you can use the-n parameter to specify the number of times you need to record. When the number of records is reached, Arthas will actively interrupt the recording process of the tt command to avoid manual operations that cannot be stopped.

At this point, I believe you have a deeper understanding of "how to use Arthas to troubleshoot JVM memory". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report