The JPS and JSTAT tuning tools were covered earlier. Today we’re going to talk about the other four tools. These tools are in the JDK bin directory.

! [](https://p1-tt-ipv6.byteimg.com/img/pgc-image/a6dec14ee44c4af49f5b5edbfae9b8b6~tplv-tt-shrink:640:0.image)

How do I use the JInfo tool

Jinfo –help: jinfo –help: jinfo –help:

! [](https://p1-tt-ipv6.byteimg.com/img/pgc-image/961ec7cf60ba4e9da7f6e1221bc3fba2~tplv-tt-shrink:640:0.image)

The option that

No option Displays all parameters and system properties

-flag name Displays parameters corresponding to the name

– flag [+ | -] open or close the name corresponding to the name of the parameter

-flag name=value Sets parameters of the corresponding name

-flags Displays all parameters

-sysprops Outputs system attributes

We can use JPS to find the PID first.

! [](https://p6-tt-ipv6.byteimg.com/img/pgc-image/14cccf5c5c78423d8d4b0063d8b33b03~tplv-tt-shrink:640:0.image)

Jinfo PID outputs a bunch of related information

! [](https://p9-tt-ipv6.byteimg.com/img/pgc-image/feb12edea7cb4a21b7fb6ca92888af01~tplv-tt-shrink:640:0.image)
! [](https://p3-tt-ipv6.byteimg.com/img/pgc-image/b9debd1c1d2b4ceba9cec8d83e2d2064~tplv-tt-shrink:640:0.image)

jinfo -flags pid

Used to output all parameters of the JVM

! [](https://p1-tt-ipv6.byteimg.com/img/pgc-image/0c3657199365421c846c51120c0573c4~tplv-tt-shrink:640:0.image)

jinfo -flag name pid

Using this command, you can view the value of the specified name as the JVM parameter.

For example, check whether the current JVM process is enabled to print ·GC logs.

! [](https://p6-tt-ipv6.byteimg.com/img/pgc-image/a00e47a6b62d43fb97d883b56a50a627~tplv-tt-shrink:640:0.image)

The same thing works

jinfo -flag [+|-]name pid

To turn on or off the parameters corresponding to the name.

You can also use

jinfo -sysprops pid

To output all system properties of the current JVM

How do I use the JMap tool

The jmap(Java Memory Map) command is used to print the shared object Memory Map or heap Memory details of a specified Java process (or core file or remote debug server). Jmap to generate dump files for Java programs. You can also view statistics on sample objects in the heap, view ClassLoader information, and finalizer queues.

The jmap command takes a snapshot of the heap of a running JVM, allowing you to analyze the heap offline to check for memory leaks, to check for the creation of large objects that have a significant impact on performance, to check what objects are in the system the most, how much memory each object occupies, and so on. Heap dumps can be generated using Jmap.

= Direct Memory +JVM Memory (MaxPermSize +Xmx);

Jmap –help Prints auxiliary information

! [](https://p3-tt-ipv6.byteimg.com/img/pgc-image/64b3220b937c4c248b974e8b9dfc4bda~tplv-tt-shrink:640:0.image)

The option that

Pid: Java process id, the ps command – ef | grep Java access, or the JPS utility

Executable: generates a Java executable for core dump

Core: the core file whose configuration information needs to be printed

Remote-hostname-or-ip: indicates the hostname or IP address for remote debugging

Server-id: unique ID. If multiple debugging servers are running on the same remote host, use this parameter to identify the server

-dump:[live,]format=b,file= Outputs the HEAP content of the JVM to a file using hprof binary format. The =. Live suboption is optional. If live is specified, only live objects are output to a file.

– finalizerInfo Prints information about objects that are waiting to be reclaimed.

-heap Displays the summary information about the heap, the algorithm used by GC, the heap configuration, and the wise Heap usage.

-histo[:live] Displays the number of instances, memory usage, and full name of each class. JVM internal class names are prefixed with “*”. If the live subparameter is added, only the number of live objects is counted.

-CLSTATS (replacing permstat, which printed classloader information prior to JDK8) prints information about classloads and JVM heap persistence layers. Contains the name of each classloader, liveliness, address, parent ClassLoader, and number of classes loaded.

Use names to view heap information for the JVM

jmap -heap pid

Output Java heap details;

! [](https://p1-tt-ipv6.byteimg.com/img/pgc-image/06168f461a06467a9c611f8f46569df3~tplv-tt-shrink:640:0.image)

use

jmap -histo:live pid

Outputs statistics about objects in the heap.

The first column is the ordinal number,

The second column is the number of objects,

The third column is the object size byte,

The fourth column is the class name

! [](https://p6-tt-ipv6.byteimg.com/img/pgc-image/4124fec0437f4abdbdb969be5367cf45~tplv-tt-shrink:640:0.image)

use

jmap -finalizerinfo pid

Output information about the objects awaiting termination

! [](https://p6-tt-ipv6.byteimg.com/img/pgc-image/49a0c4a934de494fafb56f5026c935e7~tplv-tt-shrink:640:0.image)

You can also use

jmap -clstats pid

To output classloader information.

! [](https://p3-tt-ipv6.byteimg.com/img/pgc-image/3a59f20a6ec8435d900276353745f8ef~tplv-tt-shrink:640:0.image)

How to use the JStack tool

The jstack command is used to generate thread dump files, which record CPU information at a certain point in time.

Jstack is used to generate a thread snapshot of the Java VIRTUAL machine at the current time. A thread snapshot is a stack of methods being executed by each thread in the Current Java VIRTUAL machine. The main purpose of a thread snapshot is to locate the cause of a thread’s long pause.

Such as deadlocks between threads, dead loops, long waits caused by requests for external resources, etc.

By looking at the call stack of each thread when it pauses, you can see what the unresponsive thread is doing in the background, or what resources it is waiting for. Jstack is useful if the Java program running now appears hung.

! [](https://p3-tt-ipv6.byteimg.com/img/pgc-image/8fec3c55b127494e9cbe3776d70a7cff~tplv-tt-shrink:640:0.image)

Option a written

-f: Forces the thread stack to be outputted when a normally outputted request is not answered. -l: prints additional lock information in addition to the stack. You can use jstack -l PID to observe lock holding in the event of a deadlock. -m: displays the C/C++ stack if a local method is called

jstack pid

The jStack command prints out all threads, including user-started threads and JVM background threads, focusing on user threads;

! [](https://p9-tt-ipv6.byteimg.com/img/pgc-image/7420a932aef04c13bb0835a3ce175111~tplv-tt-shrink:640:0.image)
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)
Copy the code

Http-nio-8080-exec-8: specifies the thread name

Daemon indicates whether a thread is a daemon thread

Prio represents the priority we set for the thread

Os_prio specifies the thread priority of the operating system. Not all operating systems support thread priorities. Therefore, this parameter may be set to 0

Tid is the Java id for this thread

The nID is the operating system local thread ID for this thread. Each Java thread has a corresponding operating system thread

Wait on condition indicates that the current thread is in the wait state, but no specific reason is listed

Java.lang.thread.StatC:\Users\Administrator\Desktop\ WAITING (parking) For example, parking here indicates that the locksupport. park method was called to cause the wait

Common commands

2jstack 13324 > jstatck_13324 3# Decimal conversion to hexadecimal 4printf "%x\n" 21742Copy the code

How do I use the JHAT tool

The JVM Heap Analysis Tool command is used in conjunction with Jmap to analyze dump results generated by Jmap. Jhat has a mini-HTTP /HTML server built in. After dump Analysis results are generated, you can view them in a browser. Note that the dump file generated by the server is usually copied to local or other machines for analysis because jHAT is a time-consuming and resource-consuming process.

Commonly used way

5jhat -baseline dump2.phrof dump1.phrof 5jhat -baseline dump2.phrofCopy the code

Code case

1public class JhatTest {2 public static void main(String[] args) {3 while(true) {4 String String = new String(" old "); 5 System.out.println(string); 6} 7} 8}Copy the code

Run, using JPS to get process PID

! [](https://p26-tt.byteimg.com/img/pgc-image/d6115d63a6a74d558d1ccc7d26f10895~tplv-tt-shrink:640:0.image)

Jmap -dump:format=b,file=heapDump PID

! [](https://p26-tt.byteimg.com/img/pgc-image/f53abfdfc2f644ec83963a85986325e4~tplv-tt-shrink:640:0.image)

Run the heapDump package in the heapDump package directory

jhat heapDump

! [](https://p6-tt-ipv6.byteimg.com/img/pgc-image/3e95d83908a247f0a6786db82b046932~tplv-tt-shrink:640:0.image)

Then you can access it

http://localhost:7000/

! [](https://p9-tt-ipv6.byteimg.com/img/pgc-image/06ab7f678c0049edb3792b0932f0430c~tplv-tt-shrink:640:0.image)

There are two main parts to check for heap exceptions:

Show instance counts for all classes (excluding platform), The diagram below:

! [](https://p3-tt-ipv6.byteimg.com/img/pgc-image/0d0cafe728904a91bbb78a9e142f583e~tplv-tt-shrink:640:0.image)

The Show heap histogram shows the heap situation as a tree graph

! [](https://p1-tt-ipv6.byteimg.com/img/pgc-image/f8a5cdd607f84988a505cde9918b472e~tplv-tt-shrink:640:0.image)

When troubleshooting specific problems, we need to combine the code and observe whether a large number of objects that should be recycled are constantly referenced or whether there are too many objects that cannot be recycled.