Java problem diagnosis and troubleshooting tools

I. Introduction

In the digital world, Java, as a popular programming language, is widely used to develop various types of applications. However, in the development process, various problems inevitably arise. This article explores some tools for diagnosing and troubleshooting Java issues.

2. Java problem diagnosis and troubleshooting tools

1. JDK comes with tools

There are many command line tools in the bin directory of JDK: Insert image description here
you can see that the size of each tool is basically stable at around 27kb. This is not intentional by the JDK development team, but because most of these tools are jdk\lib\tools The .jar class library is just a thin layer of packaging, and their main function codes are implemented in the tools class library.

The advantage of the command line tool is that when the application is deployed to the production environment, there will be restrictions on whether it directly contacts the physical server or remotely Telnets to the server. With the help of the interface in the tools.jar class library, we can directly implement powerful monitoring and analysis functions in the application program.

2. Commonly used commands

Insert image description here
1. jps: View local java process information

2. jstack: Print the stack information of the thread and create a thread dump file

3. jmap: Print memory mapping information and create heap dump files

4. jstat: performance monitoring tool

5. jhat: Memory analysis tool, used to parse heap dump files and display them in a human-readable manner.

6. jconsole: a simple JVM visualization tool

7. jvisualvm: a more powerful JVM visualization tool

8. javap: View bytecode

3、JAVA Dump:

JAVA Dump is a snapshot of the virtual machine running, and saves the state and information of the virtual machine running in the file, including:

Thread dump: contains the running status of all threads, in plain text format

Heap dump: contains the state of all heap objects, in binary format

3.1、jps

The command to display the pid of all current java processes. We can use this command to check how many java processes have been started (because each java program will occupy an exclusive java virtual machine instance). However, jps has the disadvantage that it can only display the current user's pid. Process id, to display other users can only use the ps command of linux.
Insert image description here

Executing the jps command will list all running java processes, where the jps command is also a java program. The number in front is the id of the process. This id is very useful and will be introduced later.

jps -help:
Insert image description here
jps -l outputs the complete package name of the application main.class or the full path name of the application jar file

Insert image description here

jps -v outputs the parameters passed to the JVM

Insert image description here

jps invalid

We will encounter such a situation in the process of locating the problem, the process id cannot be viewed with jps, but the started java process can be seen with ps -ef | grep java.

To explain this phenomenon, first understand the implementation mechanism of jps:

After the java program is started, several files will be generated in the directory /tmp/hsperfdata_{userName}/. The file name is the pid of the java process. Therefore, when jps lists the process ID, it just lists the names of the files in this directory. As for the system Parameters are to read the contents of the file.

Let's think about it: If these files cannot be created because the disk is full, or the user does not have read permissions for these files. Or these files or directories may be cleared for some reason. If these situations occur, the jps command will fail.

If the jps command fails and we want to get the pid, we can use the following two methods:

1、top | grep java
2、ps -ef |grep java

3.2、jstack

Mainly used to generate thread snapshots of the current moment of the specified process. Thread snapshots are a collection of method stacks being executed by each thread of the current Java virtual machine. The main purpose of generating thread snapshots is to locate the cause of long pauses in threads, such as threads. Deadlocks, infinite loops, and requesting external resources lead to long waits.
Insert image description here

3.3、jmap

It is mainly used to print the shared object memory map or heap memory details of the specified java process.

Heap Dump is a memory image that reflects heap usage, which mainly includes system information, virtual machine attributes, complete thread dump, status of all classes and objects, etc. Generally, in the case of insufficient memory, GC exception, etc., we will suspect a memory leak, and at this time, we will print the heap dump.

3.3.1. jmap -heap pid: View heap usage

Insert image description here

3.3.2. jmap -histo pid: View the number and size of objects in the heap

Insert image description here
The printed information is: serial number, number of objects, memory usage of these objects, and fully qualified names of classes to which these objects belong.

If it is an internal class, * will be added at the beginning of the class name. If a live sub-parameter is added, such as jmap -histo: live pid, this naming will trigger a FUll GC and only count living objects.

3.3.3. jmap -dump:format=b,file=heapdump pid: Output the details of memory usage to a file

Then use the jhat command to view the file: jhat -port 4000 file name, visit http:localhost:4000/ in the browser

3.4、to stand

jstat is a command-line tool for monitoring the performance of the Java Virtual Machine (JVM). It can provide garbage collection (GC), compilation tasks and other runtime statistics about Java applications. The jstat tool is command line-based and can be used to generate and display JVM performance statistics.

How to use the jstat tool is as follows:

  1. Open a command line terminal.
  2. Enter the following command format:
jstat -<option> [<interval> [<count>]] <vmid>

Among them, <option>refers to the statistical information you want to view, such as the number of compilation tasks available -compiler, garbage collection available -gc, etc. <interval>and <count>are used to collect data continuously and repeatedly. For example, you can set data to be collected every 500 milliseconds for a total of 10 times. <vmid>Is the virtual machine identifier, which is an integer value and can be jpsviewed through the command.

For example, if we want to view garbage collection statistics for Java application number 1234, we can enter the following command:

jstat -gc 1234

The running results will display various statistical information about garbage collection, including the usage of the new generation and old generation, the number and time of garbage collection, etc.

jstat is a very practical tool that can help us diagnose and solve performance problems in Java applications. By using jstat, we can better understand the operation of the JVM and thereby optimize the performance of the application.

3. Summary

This command is applicable to scenarios where the program has insufficient memory or frequent GC. In this case, there is likely to be a memory leak. Use the above command to check the heap usage, a large number of objects being continuously referenced, etc.

If this blog is of some help to you, please remember to leave a message + like + favorite.

Guess you like

Origin blog.csdn.net/wmj20001225/article/details/132669905