Introduction to the

When writing code, we often encounter memory leaks, such as objects in a collection not being reclaimed or memory growing for unknown reasons. These are the problems we need to locate, we can use JMap and JHAT to analyze the memory objects in Java programs.

The Java Memory Map (JMAP) is a built-in Tool of the JDK. It is used to print or output information in the Memory of a Java program to a file, and then analyze the output file using the Java Heap Analysis Tool (JHAT) to locate possible problems.

More highlights:

  • Blockchain from getting started to Giving up series tutorials – with ongoing updates covering cryptography, Hyperledger, Ethereum,Libra, Bitcoin and more
  • Spring Boot 2.X Series tutorials: Learn Spring Boot from Scratch in seven days – continuous updates
  • Spring 5.X Series tutorials: Everything you can think of in Spring5 – constantly updated
  • Java Programmer from Handyman to Expert to God (2020 edition) – Ongoing updates with detailed articles and tutorials

For more, visit www.flydean.com

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 heapCopy the code

Jmap has the following four options available:

clstats

Clstats, which stands for class Loader Statistics, loads relevant statistics with the output class.

Here’s an example:

jmap -clstats 8820
Copy the code

The following output is displayed:

  • Index-class number
  • Super – Number of the parent class
  • InstBytes – Size of bytes for each instance
  • KlassBytes – The size of bytes of this class
  • Annotations – Size of annotations
  • CpAll – The sizes of constants, tags, cache, and Operands in each class
  • MethodCount – Number of methods in a methodclass
  • The size of bytecodes-byte codes
  • Methodall-method, CONSTMETHOD, Stack map, and method data sizes
  • ROAll – Specifies the size of the class metadata that can be placed in memory
  • RWAll – Size of class metadata that can be placed in read/write memory
  • Total – ROAll + RWAll
  • ClassName – class name

finalizerinfo

Finalizerinfo lists objects that are ready for finalization.

jmap -finalizerinfo 8820
Copy the code

If there are no objects waiting to be finalized, print:

No instances waiting for finalization found
Copy the code

histo

Histo is used to output the histogram of a Java Heap object. You can add a live option to output live objects.

jmap -histo:live 8820
Copy the code

Output result:

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

dump

Finally, dump is used to dump the entire Java heap. Dump takes three arguments:

  • Live-dump live objects
  • Format =b – Dump in binary hprof mode
  • File = filename-dump an object to a file
jmap -dump:live,file=dump.log 8820
Copy the code

The dump. Log file is very large and difficult to analyze with the naked eye. The jhat (Java Heap Analysis Tool) command is used to analyze the dumped objects.

jhat

Note that jhat was removed from JDK9 (JEP 241: Remove the Jhat Tool). The Eclipse Memory Analyzer Tool (MAT) and VisualVM are now officially recommended analysis tools for Oracle. We’ll talk more about these tools later.

Jhat = jhat = jhat = jhat = jhat

Jhat command format

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
Copy the code

Since this command is deprecated, we won’t go into its arguments here, but generally jHAP parses the dump file and starts a local Web server that can view the dump data through a Web page. The default 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.
Copy the code

When we open localhost:7000, we can see that the first page shows the instance and address information of each class in the package:

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

The class info page contains a lot of information, including details about parent classes, class loaders, signatures, security domains, subclasses, instances, references, and more.

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

conclusion

This article introduces the use of JMAP and JHAT.

Author: Flydean program stuff

Link to this article: www.flydean.com/jdk14-jmap-…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!