6 JDK comes with JVM tuning tools, do you think you really can?

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.

194f45015a96f546ea2edbcb322b23ed.png

How to use jinfo tool34c6646acf84e2ffbc053d57a6e8c84d.png

jinfoDisplay virtual machine configuration information, we can see the corresponding parameters through jinfo --help:

061c126d1934f642c4b3144dbe3d91be.png

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 jpsfind first pid.

36fe245f0347fc79cd135e74f2be915f.png

jinfo pidWill output a bunch of relevant information

498762b397412055437729fc9adad6e9.png


21ec30972fa119a3f5b224c96f7ff449.png

jinfo -flags pid

JVMAll parameters used for output

55a9abd718c4a9472081a1e33e71215e.png

jinfo -flag name pid

Use this command to view JVMthe value of the specified name as a  parameter.

Such as: check JVMwhether printing and GClog are enabled in the current  process .

b316a065f9755004075e3d19b1636c1f.png

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 JVMall the current  system properties

How to use jmap tool34c6646acf84e2ffbc053d57a6e8c84d.png

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). jmapTo generate  javathe 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.

jmapCommands can get JVMa 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 --helpPrint auxiliary information

9a3bb4f02f74b9dd9a2aaa24d1c83e1b.png

option description

pid: Java process id, command ps -ef | grep javaacquisition, or jpstool acquisition

executable: javaexecutable file that generates core dump

core: 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.  JVMThe 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 the JDK8previous printing of class loader information permstat) Print classloadand jvm heappermanent layer information. Contains each classloadername, activity, address, parent classloaderand number of classes loaded.

Use named JVMheap information

jmap -heap pid

Output detailed Java heap information;

ff7abc194ba5f248b2bae525e700b6f4.png

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

5a1387a65d283ac04d301d344ff478f8.png

use

jmap -finalizerinfo pid

Output information about objects waiting to be terminated

8ef2f7bbd700c1d236d38483a2438b7b.png

Can also use

jmap -clstats pid

To output class loader information.

ad138605e7bfb4df94f310df90246ec6.png


How to use jstack toolca61290997ff15086d34d60f1741a04f.png

jstackThe biggest function of the command is to generate a thread dump file. The thread dump file records the CPU information at a certain time.

jstackMainly used to generate javaa thread snapshot of the virtual machine at the current moment. Thread snapshot is javaa 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 jstacklooking 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 running javaprogram shows the hung state jstack.

34c6646acf84e2ffbc053d57a6e8c84d.png

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 JVMbackground threads started by the user . Our main concern is user threads;

4fc4c1c7bb6977f40ce34512f141e403.png


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 name

daemon 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 tool34c6646acf84e2ffbc053d57a6e8c84d.png

JVM Heap Analysis ToolThe command is jmapused in conjunction with it to analyze the jmapgenerated dump. jhatA 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 jpsget processpid

0019d28cfcd21f0b5d1e2a054b1ab1c5.png

reusejmap -dump:format=b,file=heapDump pid

4130993585ee55bbfbb701f63646275c.png

Then go to the package directory corresponding to heapDump to execute

jhat heapDump

dfa51802e4e78397d7ba5ac3a6a21e2f.png

Then you can access

http://localhost:7000/

e4b52350786df5fbc25d9444a097a76a.png

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:

5c1b7a1c2ae3e9eed4ae3ec521ba6fce.png

Show heap histogram   displays the heap situation in the form of a tree diagram

ea8e18c64256d79da9be60a64deaa6df.png

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. 

Guess you like

Origin blog.51cto.com/10983206/2551893