What is jmap in Java?

jmap is a command-line tool that comes with the Java Development Kit (JDK). It is used to obtain memory-related information from a Java process, which is particularly useful for diagnosing memory-related issues, such as memory leaks. jmap allows you to generate heap dumps, view memory usage statistics, and retrieve information about the Java Virtual Machine (JVM).

Usage of jmap

The basic syntax for using jmap is:

jmap [options]

Where pid is the process ID of the Java application, or executable is the name of the Java executable.

Common jmap Commands

  • jmap -heap : Displays the heap memory usage.
  • jmap -histo : Displays a histogram of the heap.
  • jmap -dump:format=b,file= : Generates a heap dump to a specified file.

Example

To take a heap dump of a Java process with the PID 1234, you would run:

jmap -dump:format=b,file=heap_dump.hprof 1234

jmap Java JVM heap dump memory management performance tuning