Abstract: Easy to learn and master Java tools in the form of a question and answer.
Learn Java tools in a question-and-answer format
Q: What tools do you use to check for memory leaks? A: Jmap generates dump files and JHAT visualizes them.
Q: What tool can I use to check whether the CPU usage of a process is always full? A: Top-HP PID finds the thread that occupies the most CPU and jStack finds the stack that that thread is on to determine where the problem occurred.
§ Detailed introduction of each tool
§ jstack
JVM Stack Trance Function: Displays the Stack status of a Java process, which can be used to check deadlocks, I/O waits, and dead loops. Command usage:
- jstack pid
- For an example of deadlocks, see the following figure and find the wait and locked locks
- See waiting IO examples:
§ jstat
Full name: Function: View memory usage in a process, but can only give some simple statistics
- Jstat-class PID counts how many classes are loaded and how much space is occupied
- Count how many files are compiled with jstat-Compiler 10
Q: jstat -gcutil {pid} 2000 Displays memory usage statistics for the PID process every two seconds. The output of gcutil is as follows
Which percentage is at risk for OMM if it is between 99% and 100% for a long period of time? (OutOfMemoryError) A: If E and O are always full in the new generation and the old generation, OMM risks may occur. Like this one down here.
§ jmap
When you need to see what is taking up too much Memory in a process, you can print a heap snapshot using JMap. Command usage:
- Run the following command to print heap snapshots: jmap -dump:file=./dumpfile.dump Process PID
- Check the situation of a particular class occupies: jmap – histo: live process pid | grep class name
Q: I used Jmap to check memory leakage instances for 3 times. Each 1h I called Jmap to check the memory instances of the process and check the number of objects at that time. The first check is as follows:
After the second interval of 2 hours, check the situation as follows:
Which object may be at risk of memory leak? A: ObjectA is probably leaking because it has the fastest increasing number of objects, which means that objects keep growing and many cannot be released by GC.
§ the jhat
Full name: JVM Heap Analysis Tool and Jmap together, can parse jMAP generated Heap snapshot, support to generate a Web process for us to analyze and view. Command usage:
- Jhat-j-xmx515m dumpfile.dump will start a webServer, and then we can access it
§ JDB
Debugger used to debug core files and running Java processes in real time, similar to GDB in c++.
- Start the process and debug: jdb-classpath.test
- Some process to secondary debugging: JDB – attach 8000 – sourcepath/Users/wefit/Development/study/Java/jtest/SRC /
§ JCMD
Purpose: multifunctional tool, you can use it to export heap, view Java process, export thread information, perform GC, but also for sampling analysis, can be understood as a performance tuning tool. Common commands:
- Query information about all JVM processes on the current machine: jcmd-l
- To view performance statistics for the specified process: JCMD pid perfCounter.print
- Lists the operations that the currently running Java process can perform: JCMD PID help
- To view the Thread stack information: JCMD PID thread. print
- Run the JCMD PID gC. heap_dump FILE_NAME command to query heap memory information
§ JPS
JVM Process Status Full name: Java Virtual Machine Process Status Tool
- Check the process JVM parameter: JPS -v
- Output the full package name of the program’s main class or the full path name of the program’s JAR file: JPS -l
- Output the argument passed to the main method: JPS -m
§ jinfo
The JVM Infomation function is similar to the JPS function, but supports viewing the specified process based on the specified PIS
- You can view JVM parameters, system parameters, and adjust JVM parameters
- Viewing the memory usage of Java programs is not supported
§ javap
Disassemble Java bytecode files into Java source files.
§ javac
The execution process of Java compilation tool Javac:
§ JDK visualization tools
§ jconsole
You can monitor JVMS, both local and remote, manage applications, check for deadlocks, and many more.
§ jvisualvm
Q:
- Jvisualvm can monitor the health of a remote server
- You can run a Java program after it has run, connecting to a Java process through some protocol.
- You do not need to configure the configuration environment and VM parameters when running.
Click to follow, the first time to learn about Huawei cloud fresh technology ~