In normal development, we always encounter various problems, such as memory leaks, deadlocks, CPU, etc. Problems are not terrible, the key is how we go to check these mistakes, the right medicine is fundamental. For many people, however, the root of these problems is often lost, so this article will give us some tools to analyze what went wrong.
Previous articles focused on the JVM’s memory structure, classloading mechanism, and garbage collection mechanism. The order of the article is also step by step, from this article we are mainly the analysis of JDK tools, the theory applied to practice.
First of all, we will give an overview of several tools to be covered, and then analyze them separately:
I. Overview of performance Monitoring tools
Tools are designed to solve problems, and we have these tools because of performance issues in our programs. In fact, SUN sent us the tools after we downloaded the JDK.
There are many such tools available in our JDK installation directory.
We will find many exe files like this, many of which are performance monitoring tools. Let’s just pick out a few of them.
names | The main role |
---|---|
JPS (JVM Process Status Tool) | Displays all HotSpot VIRTUAL machine processes on the specified system |
Jstat (JVM Statistics Monitoring Tool) | Collect all aspects of HotSpot VIRTUAL machine running data |
Jinfo (Configuration Info for Java) | The VM configuration information is displayed |
Jmap (Memory Map for Java) | Creating a heapdump snapshot of a VM |
Jhat (JVM Heap Dump Browser) | Analyzing memory dump snapshots is not recommended because they consume resources and are slow |
Jstack (Stack Trace for Java) | The thread snapshot of the VIRTUAL machine is displayed |
JConsole | Visual management tools for JMX |
VisualVM | All-in-one fault management tool |
Several common tools are listed here, but there are others that work better than the JDK’s own, and I’ll list them in a future article. OK, let’s take a look at what these tools do and how to use them one by one.
Second, the tool
1. JPS: tool for vm process status
JPS is mainly used to output information about the status of processes running in the JVM. The syntax is as follows:
jps [options] [hostid]
Copy the code
The first parameter: options
-q does not print the class name, Jar name, or arguments passed to the main method
-m Prints the arguments passed to the main method
-l Displays the full name of the main class or Jar
-v prints the parameters passed into the JVM
Copy the code
The second parameter: hostid
Host or server ID. If this parameter is not specified, the current host or server is used by default.
I tested it on Windows10, but of course you can do it on Linux in the same way, and the results may be different. You can select different parameter options to test. Open CMD and enter the corresponding command
2, jStack: stack trace tool
Jstack is used to generate a thread snapshot of the VM at the current time. The syntax is as follows:
jstack [option] vmid
Copy the code
The first parameter: option
options | role |
---|---|
-F | The thread stack is forced to output when normal output requests are not answered |
-l | Out of the stack, additional information about the lock is displayed |
-m | C/C++ stacks can be displayed if local methods are called |
The second parameter: vmID
The vmID is the ID of a Java VIRTUAL machine (VM). On Linux/Unix, it is generally the ID of a process.
Let’s go straight to CMD:
3. Jstat: Monitor VM statistics
Jstat monitors various aspects of virtual machine health information, including class loading, memory, garbage collection, JIT compilation, and other running data in local or remote VIRTUAL machine processes. The syntax is as follows:
jstat [ generalOption | outputOptions vmid [interval] [count]] ]
Copy the code
The first parameter: generalOption | outputOptions
The option represented by this parameter represents the vm information that the user wants to query, which can be classified into three categories: class loading, garbage collection, and runtime compilation.
The second parameter: vmID
The vmID is the ID of a Java VIRTUAL machine (VM). On Linux/Unix, it is generally the ID of a process.
The third parameter: interval
Interval is the sampling interval,
The fourth parameter: count
Count is the number of samples.
Let’s use this tool to open our CMD and type the appropriate command:
4. Jinfo: View and adjust vm parameters in real time
Command format:
jinfo [option] pid
Copy the code
The first parameter: option
options | role |
---|---|
-v | View the specified parameter list displayed during VM startup |
-flag | View the default values of parameters not specified during vm startup |
-sysprops | Prints the contents of system.getProperties () for the virtual machine process |
The second parameter: PID
Specifies the process ID to display.
Test in CMD:
5. Jmap: Generates heapdump snapshots of VMS.
Jmap (Memory Map for Java) is used to generate heapdump or dump files. If not applicable jmap command, you can use – XX: + HeapDumpOnOutOfMemoryError parameters, when the virtual machine can generate a snapshot of memory. The pid can also be generated by using kill -3. Jmap does more than just get dump files. It can query Finalize execution queues, Java heaps, and persistent generation details, such as space usage, which collector is currently in use. The command format is as follows:
jmap [option] vmid
Copy the code
The first parameter:
The second parameter: vmID
The vmID is the ID of a Java VIRTUAL machine (VM). On Linux/Unix, it is generally the ID of a process.
Test in CMD:
6. Jhat: Analyzes memory dump snapshots. It is not recommended and is slow
Since this tool has relatively simple functions and is time-consuming to run, it is not recommended to use. MAT is recommended.
7. JConsole: Visual management tool for JMX
Compared with the previous tools, this tool has high usage and is very important. It is a Java GUI monitoring tool that displays data in a graphical form. And remote server VMS can be monitored through remote connections. GUI program written in Java, used to monitor VM, and can monitor the remote VM, very easy to use, and very strong function.
Enter jConsole in CMD and select the process. (The premise is to create a thread running in the IDE tool first)
After selecting the appropriate option, enter the tool and the following screen will appear
There is a menu on it, we can choose one of them to check, it is ok. This tool is very convenient to use, and it is also the tool I used most before.
VisualVM: All-in-one fault management tool
This tool is also very bility. Jvisualvm and JConsole are both a Graphical interface based JAVA GUI monitoring tool that can be viewed locally and remotely. Jvisualvm can be used in the same way as JConsole. Jvisualvm interface is more beautiful and data is more real-time:
There is also a menu at the top where you can select different options to display. It’s best to try it yourself.
Third, summary
These tools to write so much, in this article we have found that at the beginning, the JDK’s own tool that is super, and along with the continuous renewal of the JDK version, tools and continuously strengthen the trend of increase, each want to grasp it’s too time consuming, what problem we have to search for, and see what tools can be used to.