jmap Command in Linux



The jmap command is a versatile tool for memory management in Java applications running in Linux. It's part of the Java Development Kit (JDK) and provides valuable insights into memory usage, which is crucial for performance tuning and troubleshooting memory leaks.

The jmap command is a powerful ally in managing and diagnosing memory usage in Java applications.

Table of Contents

Here is a comprehensive guide to the options available with the jmap command −

Understanding the jmap Command

The jmap command is a versatile tool for memory management in Java applications running in Linux. It's part of the Java Development Kit (JDK) and offers various options to help developers and system administrators analyze and troubleshoot memory usage.

jmap
Understanding jmap Command

How to Use jmap Command?

The jmap command is a powerful utility in Linux that allows you to inspect the heap space of a running Java process. It provides valuable information about the objects and data structures present within the heap, aiding in troubleshooting memory-related issues and performance bottlenecks.

Options jmap Command

Heres an in-depth look at the jmap command and its options −

Options Description
Memory Mapping (jmap <pid>) This basic form of the command prints memory-related information for the Java process identified by the process ID (pid). It's useful for getting a quick overview of the memory layout of a Java application.
Heap Summary (jmap -heap <pid>) The -heap option provides a summary of the heap memory usage, which includes details about the Java Virtual Machine (JVM) memory configuration and garbage collection.
Finalizer Statistics (jmap -finalizerinfo <pid>) With -finalizerinfo, jmap provides information about the objects awaiting finalization. This can reveal issues with objects not being properly finalized.
Heap Dump (jmap -dump:[live,]format=b,file=<path> <pid>) The -dump option allows you to create a heap dump of the Java process. Specifying live will only dump live objects, and format=b indicates a binary format.
Heap Histogram (jmap -histo[:live] <pid>) Using -histo, you can generate a histogram of the object count and memory size for each class. Adding :live will only consider live objects.
Help (jmap -h) For a quick reference, -h displays the help message, listing all the available options and their descriptions.
Class Loader Statistics (jmap -F <pid>) The -F option forces jmap to attach to the process using the HotSpot Serviceability Agent when the standard attach mechanism fails.
Shared Object Mappings (jmap -clstats <pid>) By using -clstats, you can print shared object mappings, which is useful for understanding the memory footprint of the class loaders in the JVM.

Examples of jmap Command in Linux

Take a look at the following examples to get a better understanding of how the jmap command works in Linux −

  • Printing Shared Object Mappings
  • Printing Heap Summary Information
  • Generating a Histogram of Heap Usage by Class
  • Dumping Heap to a File
  • Live Object Counting
  • Dumping Heap for a Remote Server Process
  • Printing Finalizer Information
  • Printing Class Loader Statistics
  • Generating Heap Dumps
  • Viewing Heap Statistics
  • Listing Classes and Their Instances
  • Finding Finalizable Objects
  • Viewing Remote JVM Information

Printing Shared Object Mappings

To view the shared object mappings for a Java process, use the following command −

jmap 9466
Printing Shared Object Mappings

Replace java_pid with the process ID of your Java application.

Printing Heap Summary Information

For a summary of the heap memory usage, the command is −

jmap -heap 1234
Printing Heap Summary Information Using jmap

This flag -heap instructs jmap to print details about the Java heap configuration, such as the sizes of the generations and memory usage.

Generating a Histogram of Heap Usage by Class

To get a histogram of the heap usage by class, use −

jmap -histo 1234
Generating a Histogram of Heap Usage by Class

The -histo option will list down classes, the number of instances, and the memory footprint, which helps in pinpointing which classes consume the most memory.

Dumping Heap to a File

For deeper analysis, you might want to dump the heap to a file −

jmap -dump:format=b,file=/home 1234
Dumping Heap to a File Using jmap

The -dump option followed by format=b and file=<path_to_file> tells jmap to create a binary dump of the heap, which can be analyzed later with tools like jhat.

Live Object Counting

You can also count the number of live objects in the heap −

jmap -histo:live 1234
Live Object Counting Using jmap

Adding :live to the -histo option will only count the live objects, which is helpful for identifying memory leaks.

Dumping Heap for a Remote Server Process

If you need to dump the heap from a remote server process, you can use −

jmap -dump:format=b,file=/home hostname:80
Dumping Heap for a Remote Server Process

Ensure that the remote server is configured to accept remote debugging connections.

Printing Finalizer Information

To print objects awaiting finalization, use −

jmap -finalizerinfo 1234
Printing Finalizer Information Using jmap

The -finalizerinfo option provides a list of objects in the finalizer queue.

Printing Class Loader Statistics

For class loader statistics, the command is −

jmap -clstats 1234
Printing Class Loader Statistics in jmap

The -clstats option gives information about class loaders, which is useful for diagnosing class loading issues.

Generating Heap Dumps

To create a snapshot of the heap at a specific point in time −

jmap -dump:format=b,file=heapdump.hprof 1234
Generating Heap Dumps Using jmap

Replace <pid> with the process ID of the Java application you want to analyze.

  • The -dump option specifies that a heap dump should be generated.
  • format=b indicates that the dump should be in binary format (HPROF).
  • file=heapdump.hprof sets the filename for the dump.

Viewing Heap Statistics

To display information about the heap, such as used and free memory −

jmap -heap 1234
Viewing Heap Statistics Using jmap

This command provides details like the maximum heap size, current heap usage, and garbage collection statistics.

Listing Classes and Their Instances

To list all classes loaded by the Java process and the number of instances of each class −

jmap -histo 1234
Listing Classes and Their Instances in jmap

This output helps identify classes that are consuming a significant amount of memory.

Finding Finalizable Objects

To find objects that are waiting to be finalized by the garbage collector −

jmap -finalizerinfo 1234
Finding Finalizable Objects Using jmap

This can be useful for diagnosing memory leaks caused by objects that are not being properly released.

Viewing Remote JVM Information

To connect to a remote JVM and perform analysis −

jmap -remote ubuntu:80 ls
Viewing Remote JVM Information Using jmap

Replace <host:port> with the hostname and port.

By understanding and effectively utilizing the jmap command, you can gain valuable insights into the memory behavior of your Java applications and troubleshoot memory-related problems more efficiently.

Conclusion

The jmap command is a powerful ally for any developer or system administrator working with Java applications in Linux. By understanding and utilizing the various options provided by jmap, one can gain deep insights into the memory usage patterns and potential issues within Java applications.

Always remember to replace <java_pid> with the actual process ID and <path_to_file> with the desired file path when using these commands.

Advertisements