I have already talked about jps and jstat tuning tools . Today we continue to talk about the other 4 tools. These tools are in the bin directory of jdk.
How to use jinfo tool
jinfo
Display virtual machine configuration information, we can see the corresponding parameters through jinfo --help:
option description
no option output all parameters and system attributes
-flag name output the parameter corresponding to the name
-flag [+|-]name turn on or off the parameter corresponding to the name
-flag name=value set the parameter corresponding to the name
-flags output all parameters
-sysprops
Output system properties
We can use jps
find first pid
.
jinfo pid
Will output a bunch of relevant information
jinfo -flags pid
JVM
All parameters used for output
jinfo -flag name pid
Use this command to view JVM
the value of the specified name as a parameter.
Such as: check JVM
whether printing and GC
log are enabled in the current process .
The same can be used
jinfo -flag [+|-]name pid
To turn on or turn off the parameter of the corresponding name.
Can also use
jinfo -sysprops pid
To output JVM
all the current system properties
How to use jmap tool
jmap(Java Memory Map)
Command, mainly used to print the shared object memory map or heap memory details of the specified Java process (or core file, remote debugging server). jmap
To generate java
the dump file of the program, you can also view the statistical information of the object samples in the heap, the viewed ClassLoader
information, and the finalizer
queue.
jmap
Commands can get JVM
a snapshot of the running heap, so that you can analyze the heap offline to check for memory leaks, check the creation of some large objects that seriously affect performance, check which objects are the most in the system, the size of the memory occupied by various objects, etc. . You can use jmap to generate Heap Dump.
Memory = direct memory (direct memory) + JVM memory(MaxPermSize +Xmx)
;
jmap --help
Print auxiliary information
option description
pid
: Java process id, commandps -ef | grep java
acquisition, orjps
tool acquisitionexecutable:
java
executable file that generates core dumpcore: The core file that needs to print configuration information
remote-hostname-or-ip: hostname or ip for remote debugging
server-id: Optional unique id. If multiple debugging servers are running on the same remote host, use this option parameter to identify the server
-dump:[live,]format=b,file=Using hprof binary format, output the heap content of jvm to file=. The live sub-option is optional. If the live option is specified, only live objects are output to the file.
-finalizerinfo Print information about objects waiting to be collected.
-heap Print the summary information of the heap, the algorithm used by the GC, the configuration of the heap and the usage of the wise heap.
-histo[:live]
Print the number of instances of each class, the memory usage, and the full name of the class.JVM
The internal class name will be prefixed with "*". If the live sub-parameter is added, only the number of live objects will be counted.
-clstats
(Replaces theJDK8
previous printing of class loader informationpermstat
) Printclassload
andjvm heap
permanent layer information. Contains eachclassloader
name, activity, address, parentclassloader
and number of classes loaded.
Use named JVM
heap information
jmap -heap pid
Output detailed Java heap information;
use
jmap -histo:live pid
Output statistics about objects in the heap.
The first column is the serial number,
The second column is the number of objects,
The third column is the object size byte,
The fourth column is the class name
use
jmap -finalizerinfo pid
Output information about objects waiting to be terminated
Can also use
jmap -clstats pid
To output class loader information.
How to use jstack tool
jstack
The biggest function of the command is to generate a thread dump file. The thread dump file records the CPU information at a certain time.
jstack
Mainly used to generatejava
a thread snapshot of the virtual machine at the current moment. Thread snapshot isjava
a collection of method stacks being executed by each thread in the current virtual machine. The main purpose of generating thread snapshots is to locate the cause of the thread's long pause.Such as deadlock between threads, infinite loop, long wait caused by requesting external resources, etc.
When a thread is stalled, by
jstack
looking at the call stack of each thread, you can know what the unresponsive thread is doing in the background or what resources it is waiting for. It is very useful if the currently runningjava
program shows the hung statejstack
.
option written
-F: When the request for normal output is not responded, the thread stack is forced to be output.
-l: In addition to the stack, additional lock information will be printed. When a deadlock occurs, you can use jstack -l pid to observe the lock holding.
-m: If you call a local method, you can display the C/C++ stack
jstack pid
The jstack command will print out all threads, including the threads and JVM
background threads started by the user . Our main concern is user threads;
1"http-nio-8080-exec-8" #26 daemon prio=5 os_prio=0 tid=0x000000005b940000 nid=0x1e2c waiting on condition [0x000000005c0be000] java.lang.Thread.State: WAITING (parking)
http-nio-8080-exec-8
: Thread namedaemon indicates whether the thread is a daemon thread
prio represents the priority we set for the thread
os_prio indicates the priority of the corresponding operating system thread. Since not all operating systems support thread priority, it may be set to 0.
tid is the id of this thread in java
nid is the operating system native thread id corresponding to this thread, and each java thread has a corresponding operating system thread
wait on condition indicates that the current thread is in a waiting state, but the specific reason is not listed
java.lang.Thread.StatC:\Users\Administrator\Desktop\ WAITING (parking) also means that it is in a waiting state. The content in the brackets explains the reason for the waiting. For example, the parking description here is because the LockSupport.park method is called Cause waiting
Common commands
1# 查询进程的线程信息 输出到jstatck_13324文件中
2jstack 13324 > jstatck_13324
3# 十进制转换为十六进制
4printf "%x\n" 21742
How to use jhat tool
JVM Heap Analysis Tool
The command is jmap
used in conjunction with it to analyze the jmap
generated dump. jhat
A mini HTTP/HTML server is built in. After generating the dump analysis result, it can be viewed in the browser. It should be noted here that the analysis is generally not performed directly on the server, because jhat is a time-consuming and hardware resource consuming process. Generally, the dump file generated by the server is copied to the local or other machine for analysis.
Common way
1#分析 dump 文件
2jhat -J-Xmx512M dump.phrof
3
4#对比 dump 文件
5jhat -baseline dump2.phrof dump1.phrof
Code case
1public class JhatTest {
2 public static void main(String[] args) {
3 while(true) {
4 String string = new String("老田");
5 System.out.println(string);
6 }
7 }
8}
Run, use the jps
get processpid
reusejmap -dump:format=b,file=heapDump pid
Then go to the package directory corresponding to heapDump to execute
jhat heapDump
Then you can access
http://localhost:7000/
Generally, viewing the heap abnormal situation mainly depends on these two parts:
Show instance counts for all classes (excluding platform) , all object information outside the platform. As shown below:
Show heap histogram displays the heap situation in the form of a tree diagram
When troubleshooting specific problems, you need to combine the code and observe whether a large number of objects that should be recycled are being referenced all the time or whether there are objects that take up a lot of memory that cannot be recycled.