As a qualified developer, not only to write a hand code, but also a very important skill is to troubleshoot the problem. The troubleshooting problem mentioned here is not just debug in the coding process, but also the troubleshooting of online issues. Because in the production environment, generally there is no way to debug (in fact, there are some problems, debug also white rip ... , so we need to use some common commands to see what is going on at runtime, including, but not limited to, running logs, exception stacks, heap usage, GC conditions, JVM parameter conditions, thread conditions, and so on.
When it comes to positioning a system, knowledge, experience is the key, data is the basis, tools are the means of using knowledge to process data. To help us troubleshoot and solve problems, Sun has provided us with some common commands. These commands are generally a thin layer of packaging in the Jdk/lib/tools.jar class library. With the installation of the JVM installed in the machine, in the bin directory. Here's a look at these commands and how to use them. The details of all the commands involved in this article can be referred to in the Java Command Learning series .
JPS function
Displays the commands for all current Java process PID.
Common directives
- JPS: Displays the PID of all Java processes for the current user
- Jps-v 3331: Display virtual machine parameters
- Jps-m 3331: Displays the arguments passed to the main () function
- Jps-l 3331: Show the full path of the main class
Detailed Introduction
Jinfo function
View and adjust virtual machine parameters in real time, you can display the default values for the specified parameters that are not displayed (Jps-v is not available).
The command is not supported in JDK8.
Common directives
Jinfo-flag cmsininiatingoccupancyfration 1444: querying cmsininiatingoccupancyfration parameter values
Detailed Introduction
Jstat function
Displays the class load, memory, garbage Collection, JIT compilation, and other running data in the process.
Common directives
- JSTAT-GC 3331 250 20: Query process 2764 garbage collection, query every 250 milliseconds, total query 20 times.
- Jstat-gccause: Additional output last GC reason
- JSTAT-CALSS: Load of things, class unloading, total space, and time spent
Detailed Introduction
Jmap function
Heap dump Snapshot (heapdump)
Common directives
- Jmap-heap 3331: Viewing the Java heap (heap) usage
- Jmap-histo 3331: View the number and size of objects in heap memory (histogram)
- Jmap-histo:live 3331:JVM will trigger the GC before statistics
- Jmap-dump:format=b,file=heapdump 3331: Output The details of memory usage to a file, which is then typically analyzed using other tools.
Detailed Introduction
Jhat function
Typically used in conjunction with JMAP to analyze Jmap generated heap dump files.
Because there are many visualization tools (Eclipse Memory Analyzer, IBM heapanalyzer) can be replaced, so rarely used. However, it is also available on machines that do not have visual tools.
Common directives
Jmap-dump:format=b,file=heapdump 3331 + jhat Heapdump: Parse the Java heap dump file and start a Web server
Detailed Introduction
Jstack function
Generates a thread snapshot at the current moment.
Common directives
- Jstack 3331: Viewing thread conditions
- Jstack-f 3331: When normal output is not responding, use this directive
- Jstack-l 3331: Displays attachment information about the lock in addition to the stack
Detailed Introduction
FAQ location Process Frequent GC problems or memory overflow issues
One, use JPS to view thread ID
Second, use JSTAT-GC 3331 250 20 to view the GC situation, generally pay more attention to the situation in the perm area, to see the growth of GC.
Third, use Jstat-gccause: additional output last GC reason
Iv. generating a heap dump file using Jmap-dump:format=b,file=heapdump 3331
Analyze the heap using Jhat or visualization tools (Eclipse Memory Analyzer, IBM heapanalyzer).
Vi. combine code to resolve memory overflow or leak issues.
Deadlock problem
One, use JPS to view thread ID
Second, use Jstack 3331: View Thread condition
Conclusion
Often using the appropriate virtual machine monitoring and analysis tools can speed up our analysis of data, positioning problem-solving speed, but also know that the tool is always a layer of knowledge and skills packaging, there is no tool is a package of cures.
On-line troubleshooting commands that Java development must master