Overview

We have gone through different cause of error OutOfMemoryError , If a program is plagued with slow service, the system unexpectedly crashes and is prone to hanging and pausing on its own, or takes up excessive CPU power and memory; the odds are that there are certain bugs in it that need to be removed as soon as possible. However, for debugging, it is necessary that we have the requisite data before we commence any rectification procedures.

Fortunately, there exists a vast number of tools that may potentially help provide all the necessary information that we need to be able to fix the program. Some of these tools may be part of the JVM itself, while some tools have been developed by third-party specialists.

Today we will discuss jmap JVM utility, It as an application that prints shared object memory maps or heap memory details of a given process, core file, or remote debug server.

How to Run jmap

In order to run the jmap utility successfully, it is necessary to know the required Process Identifier (PID) of the application that we want to run this tool against. This may be speedily accomplished by using a tool called Java Virtual Machine Process Status Tool (JPS), which is already available in the JVM. Basically, JPS may be used to create a list of each and every JVM process currently running on the system along with every process’s very own PID. We can also use the ps -ef | grep java command on Linux platforms, which will give you the PIDs of Java processes as shown here:

The PID can be retrieved with the jps command as well. See the following:

The jmap command-line utility will print the memory-related statistics. The jmap tool can also be used to collect information from a remote machine using the jsadebugd demon. However, it takes a long time to print the output.

There are various options you can use with jmap such as –heap, -histo, -permstat, and so on. If no option is provided, then it will print the list of shared objects loaded. We can also store the output in a file for which we will need JDK 7 or higher. Use the -dump:format=b,file= option with jmap, which will dump the Java heap in the binary HPROF format. We can use third party tools to analyze this file.

Let’s take an example to understand profiling in depth. We have test class JavaMapTest which is creating a 1000 copies of objects and then running in while loop.

1. jmap -heap

The -heap option is used to obtain the following Java heap information:

Information specific to the garbage collection (GC) algorithm, including the name of the GC algorithm (for example, parallel GC) and algorithm-specific details (such as number of threads for parallel GC).

Heap configuration that might have been specified as command-line options or selected by the VM based on the machine configuration.

Heap usage summary: For each generation (area of the heap), the tool prints the total heap capacity, in-use memory, and available free memory. If a generation is organized as a collection of spaces (for example, the new generation), then a space specific memory size summary is included.

Let’s run it for our test program, As per above jps command PID for example is 9665.

2. jmap -histo

-heap input will not provide any details related to class, The jmap command with the -histo option can be used to obtain a class specific histogram of the heap. Depending on the parameter specified, the jmap -histo command can print out the heap histogram for a running process or a core file.

When the command is executed on a running process, the tool prints the number of objects, memory size in bytes, and fully qualified class name for each class. Internal classes in the Java HotSpot VM are enclosed in angle brackets. The histogram is useful in understanding how the heap is used. To get the size of an object, you must divide the total size by the count of that object type.

Let’s run -histo gainst process id 9665.

3. jmap -permstat

The permanent generation is the area of the heap that holds all the reflective data of the virtual machine itself, such as class and method objects. This area is also called “method area” in The Java Virtual Machine Specification.

Configuring the size of the permanent generation can be important for applications that dynamically generate and load a very large number of classes (for example, Java Server Pages or web containers). If an application loads too many classes, then it is possible it will terminate with the following error:java.lang.OutOfMemoryError: PermGen space

To get further information about the permanent generation, We can use the -permstat option of the jmap command to print statistics for the objects in the permanent generation.

Let’s run -permstat against process id 9665.

We can get many useful information with -permstat, Some of them are.

  • The address of the class loader object at the snapshot when the utility was run
  • The number of classes loaded
  • The approximate number of bytes consumed by metadata for all classes loaded by this class loader
  • The address of the parent class loader (if any)
  • A live or dead indication of whether the loader object will be garbage collected in the future
  • The class name of this class loader

jmap is slow when we are debugging issue with remote system, The release of JDK 8 introduced Java Mission Control, Java Flight Recorder, and jcmd utility for diagnosing problems with JVM and Java applications. It is suggested to use the latest utility, jcmd instead of the previous jmap utility for enhanced diagnostics and reduced performance overhead.

That’s all for jmap, Hope you like it, Keep reading and sharing !! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *