This is the third article on the JVM. This is the actual tuning of the JVM, and the last one is the basic theory of tuning. It is mainly about GC.

The main content of this article is as follows, including the commonly used tuning tools of local and online, and a combination of cases to explain, and then is some common practical scenarios, this is also almost big factory interviewers will ask, you can also be directly used when the interview case:

Tuning tool

Ok, let’s start with the common tuning tools. The GUI tool I recommend here is VisualVM. I don’t know which one you use.

VisualVM

Jvisualvm is a JDK tuning tool. You can see the jVisualVM tool in the installation directory jdk1.8.0_40\bin:

Double-click it directly, and the interface opened is as follows:

In the interface that opens, the left hand side is divided into local and remote. Whenever you have a Java process starting locally, it will automatically detect it. For example, DeadLock (PID 8120) is an example of my locally-started DeadLock.

You can also connect to the Java process of the remote server, but the server of the formal environment will not let you connect, our formal environment can only be operated by QA, our developers can only do dev environment, like test environment can only be tested by QA, they can only operate, here we directly use the thick operation.

In the content area on the right, you can see the main areas of concern. For example, the overview shows the JVM parameters and system properties that we set when we started the Java process (the default).

As well as monitoring plate, it shows the GUI interface, as shown in the above shows the CPU usage, the usage of the heap size, Metadata (yuan space usage), and the process of loading the classes and number of threads, so I can pass this interface can be observed at least the health status of the CPU and memory heap size changes.

The third thread is definitely the runtime information of the real thread. You can see that the deadlock is detected directly here. The tool is very smart, and you can see that two of the threads are DeadLock1 and DeadLock2.

Then click on the thread Dump to see the stack information of the thread:

You can see that two threads are waiting for each other, each waiting for the lock, each holding the other at the same time into a deadlock situation.

So you can check for deadlocks through the third thread, and then the sampler and Profiler can look at the CPU’s memory:

The last is Visual GC, this must be installed plug-in, installation is in the navigation bar tools under the plug-in can be installed, specific installation can be baidu, quite simple:

In this section, you can see Compile Time, Class Loader Time, GCTime (total GCTime), Eden, S1, S0, Old Gen, Matadata (meta space size).

For tuning the Java heap, the main pieces to focus on are the following: changes in the size of the Eden, S1, S0, and Old Gen fields, and changes in the XXX Collections and XXXms properties, which indicate how many GC’s have occurred and at what time, respectively.

This section is also shown in the figure below. It can see the ratio of power number in the heap. It can analyze OOM issues and memory leaks.

About the detailed use of Visual VM to say here, the basic development to use, mentioned, detailed oneself can also find a tutorial for in-depth study.

Arthas

Arthas is one of Alibaba’s open source projects and can be used to analyze the specifics of Java processes in production, much like Visual VM, except in the way a Linux environment uses commands.

download

First, download Arthas and use wGET directly:

wget https://alibaba.github.io/arthas/arthas-boot.jar
Copy the code

Once downloaded, it is a JAR project that can be launched directly with a Java command.

For the following examples, such as OOM cases, deadlock cases, CPU high cases, in order to save time, I are direct use of JavAC and Java commands to compile and run, more convenient, no longer create SpringBoot project, you can also configure their own JDK environment variables in advance.

Start the

Once Arthas has been downloaded, you can start it directly with the following command:

java -jar arthas-boot.jar -h
Copy the code

I start my own case OOM first

Then, after starting Arthas again, he will automatically detect the Java process and select the Java process you want to monitor, such as OOM, so type 1:

Enter 1 to enter Arthas successfully:

Here’s a look at the common Arthas commands. I won’t go into details. There are too many commands, but just a few common ones that are used in the case.

dashboard

Typing the dashboard command brings up the monitoring panel: the class diagram interface, which looks at each thread and CPU usage.

For the inside of a field to indicate what means, should not be poor English, also can know, HERE I directly check Baidu cut a picture, not a knock, too much, you can follow the picture:

thread

The thread command looks at information about a thread, including information about the thread stack, the amount of CPU consumed by the thread, and so on.

The parameters for thread are as follows:

If you enter thread directly, you can see that child-thread is the highest CPU usage, and that the name child-thread is the name I gave the thread when it was created.

In the Development manual of Alibaba, it is also mentioned that the best name for the creation of the thread pool, so that when there is a problem, it is convenient to identify the cause:

In addition to some specifications, the development manual also includes some tuning: SQL tuning. If you are interested, you can take a look. Many technical bloggers have posted this PDF in their official accounts.

jvm

The JVM command can be used to view information about the current JVM

For detailed field information in the figure above, please refer to the following figure:

trace

The trace command is used to monitor method invocations within the class, and how long the method invocation takes:

If a method takes too long, there is a good chance that the method is faulty and the original code needs to be reviewed.

Sc-d class name You can also view the class information, as shown in the following:

jad

The jad command decompiles the byte code of the class actually running in the JVM into Java code. It is a powerful command. For example, if you want to check the code online, you can use this command to decompile the code:

watch

The watch command can be used to view the invocation of a specified method, including the return value, the thrown exception, and the incoming parameters.

Specific details of fame and wealth can be referred to as follows:

Native command

In addition to the third-party tools described above, you can also use the original Linux commands to troubleshoot problems, mainly including the following commands:

  • jps
  • top
  • jstack
  • jstat
  • jmap
  • jhat
  • jinfo

jps

JPS lists the running VM processes and displays the names of the Main Class (Main () function of the VM) and the unique Local Virtual Machine Identifiers (LVMids) of these processes.

After finding the Id of the Java process, you can perform subsequent operations, such as checking the thread status within the process.

If you want to know more about the parameters of the JPS command, you can use the man command to query. For example, man JPS:

options role
-q This command outputs only the process PID information, omitting the name of the main class
-m Output the parameters passed to the main() function when the virtual machine starts
-l Prints the full name of the main class and the full path to the package name
-v Output THE JVM parameters when the vm process starts

top

The top command is used to query the CPU usage of each process. It can be used together with the ps command, as shown in the following figure:

jstack

Jstack is used to query thread stack information. It is easy to check for deadlock information. For example, the following figure is used to check for deadlock information.

jstat

The jstat command is used to monitor vm statistics, such as GC information, memory information, and real-time compiler running information.

Jstat has many parameters. The common ones are as follows:

options use
-class Outputs the number of classes loaded, unloaded, total space, and class unloaded time
-gc Output the information about the Java heap, including Eden, S1, S0, old age, capacity of equal space, usage, garbage collection time, and so on
-gccapaccity It also monitors the Java heap, but focuses on the maximum and minimum space used by each area of the Java heap
-gcutil Output the percentage of the total space in the Java heap that is used
-gccause As with the -gcutil function, the reason for the last garbage collection is printed as an additional output
-gcnew Output the situation of the new generation
-gcnewcapacity It only focuses on the new generation, mainly on the maximum and minimum space used
-gcold Output the situation of the old age
-gcoldcapacity Also only focus on the old age, mainly focus on the maximum and minimum space used
-gcpermcapacity Output the maximum and minimum space used by the permanent generation
-compiler Output real-time compiled methods, time, and so on
-printcomilation Output methods that have been compiled

Use jstat -gc PID to see some gc as shown in the figure below

The above fields are denoted as:

The column name role
S0C The size (in bytes) of the first survivor in the young generation
S1C The size (in bytes) of the second survivor in the young generation
S0U First survivor in the young generation Currently used space (bytes)
S1U Second survivor in the young generation Currently used space (bytes)
EC The size of Eden in the young generation (in bytes)
EU Space (bytes) currently used by Eden in the young generation
OC Size of the Old generation (in bytes)
OU Old generation currently used space (bytes)
MC Capacity of the meta space (in bytes)
MU Currently used space (bytes)
YGC The number of GCS in the young generation from application startup to sampling time
YGCT Time spent by GC in the young generation from application startup to sampling time (s)
FGC Number of old generation (full GC) GC’s from application startup to sampling
FGCT Elapsed time (s) for old generation (full GC) GC from application startup to sampling
GCT Total gc time (s) from application startup to sampling time

For a detailed explanation of some of the other parameters of jstat, check the documentation or use man jstat directly.

jmap

Jmap should be used sparsely-because it is used to generate snapshots of heap dumps, but it consumes system resources, so it is generally not used. Instead, use JVM parameters: + HeapDumpOnOutOfMemoryError and – – XX: XX: HeapDumpPath = ${} directory.

And then, I’m going to use the memory analysis tool to do a bunch of heap dump analysis, so this command, you know, it’s good to know, but if you have a small project, if you have a traditional project, you can still use it, but not for Internet projects.

jhat

Jhat is a snapshot analysis tool for heap dumps. It is used to generate heap dumps and analyze them using the GUI interface. It is rare to analyze heap dumps online.

Because it also consumes server performance, and the general analysis process will be relatively long, so the online analysis method is not used.

jinfo

Jinfo is used to view and adjust the parameters of the virtual machine, but we may generally be more concerned about the parameters that we set when the virtual machine starts, which can be queried through JPS -v.

Good, this is a limited to space reasons, first write here, next to understand further the JVM tuning real case analysis, originally wanted to a finish, but the length is estimated to reach more than twenty thousand words, is too long, so two article, there is a need for alibaba development manual, and articles used in the case of the source code, you can add me WeChat: Abc730500468, that’s it for this issue, I’m Li Du, we’ll see you next issue.