Jdk14 performance management tool: introduction to jmap and jhat

brief introduction

In the process of writing code, we often encounter the problem of memory leakage, such as the objects in a collection are not recycled, or the memory grows for unknown reasons. These are all problems that need us to locate. We can use jmap and jhat to analyze memory objects in Java programs.

Jmap (JAVA memory map) is a tool provided with JDK. It is used to print or output the information in the memory of a java program to a file, and then analyze the output file through jhat (Java heap analysis tool) tool to find possible problems.

More highlights:

Next, enter our jmap and jhat tour.

jmap

    jmap -clstats <pid>
        to connect to running process and print class loader statistics
    jmap -finalizerinfo <pid>
        to connect to running process and print information on objects awaiting finalization
    jmap -histo[:[<histo-options>]] <pid>
        to connect to running process and print histogram of java object heap
    jmap -dump:<dump-options> <pid>
        to connect to running process and dump java heap

Jmap has the following four options available:

clstats

The full name of clstats is called class loader statistics, which uses the output class to load relevant statistics.

For example:

jmap -clstats 8820

The output results are as follows:

finalizerinfo

Finalizerinfo lists the objects ready for finalization.

jmap -finalizerinfo 8820

If there are no objects waiting to be finalized, the following will be output:

No instances waiting for finalization found

histo

Histo is used to output histograms of Java heap objects. You can add a live option to output live objects.

jmap -histo:live 8820

Output results:

Num is the number of the object, instances is the number of objects, bytes is the size of the object, and class name is the class name of the object.

dump

Finally, let's talk about dump. Dump is used to dump the entire Java heap. Dump can take three parameters:

jmap -dump:live,file=dump.log 8820

Here is dump The log file is very large and difficult to analyze with the naked eye. Let's introduce the jhat (Java heap analysis tool) command to analyze the objects from dump.

jhat

Note that jhat was removed from jdk9 (JEP 241: remove the jhat tool). Now the analysis tools officially recommended by Oracle are eclipse memory analyzer tool (MAT) and visualvm. These two tools will be explained in detail later.

Today, first use jhat in jdk8 to analyze the above dump files.

Let's first look at the command format of jhat:

Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>

	-J<flag>          Pass <flag> directly to the runtime system. For
			  example,-J-mx512m to use a maximum heap size of 512MB
	-stack false:     Turn off tracking object allocation call stack.
	-refs false:      Turn off tracking of references to objects
	-port <port>:     Set the port for the HTTP server.  Defaults to 7000
	-exclude <file>:  Specify a file that lists data members that should
			  be excluded from the reachableFrom query.
	-baseline <file>: Specify a baseline object dump.  Objects in
			  both heap dumps with the same ID and same class will
			  be marked as not being "new".
	-debug <int>:     Set debug level.
			    0:  No debug output
			    1:  Debug hprof file parsing
			    2:  Debug hprof file parsing,no server

Because this command has been abandoned, we won't explain its parameters here. Generally speaking, jhap will parse the dump file and start a web server locally. We can view the dump data through the web page. By default, the port of the web server is 7000.

jhat dump.log
Reading from dump.log...
Dump file created Mon May 11 21:13:43 CST 2020
Snapshot read,resolving...
Resolving 197989 objects...
Chasing references,expect 39 dots.......................................
Eliminating duplicate references.......................................
Snapshot resolved.

Open localhost: 7000, and we can see that the home page shows the instance and address information of the classes in each package:

Click the class link on the home page to jump to the specific information page of the class:

The class information page contains a lot of information, including details such as parent class, class loader, signature, security domain, subclass, instance, reference, etc.

It is very useful for us to analyze memory leaks and memory exceptions.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>