How to use the command ‘jmap’ (with examples)
The ‘jmap’ command is a tool provided by Java that allows you to obtain memory-related information for a Java process. It can help you analyze and troubleshoot memory issues in your Java applications. This article will illustrate different use cases of the ‘jmap’ command and provide examples for each of them.
Use case 1: Print shared object mappings for a Java process
Code:
jmap java_pid
Motivation: The motivation for using this use case is to get a list of shared object mappings for a specific Java process. This can help in understanding the memory layout of the Java process and identifying any memory-related issues.
Explanation:
java_pid
: This is the Process ID of the Java process for which you want to print the shared object mappings.
Example output:
Address Kbytes RSS Anon Mapped Shared Swap Mode Mapping
...
7fa3cb800000 2048 1440 - - - - r-x-- ld-2.17.so
7fa3dd1ca000 392 220 - - - - rwx-- ld-2.17.so
7fa3dd25d000 220 1024 - - - - rwx-- jvm.so
...
Use case 2: Print heap summary information
Code:
jmap -heap java_pid
Motivation: The motivation for using this use case is to get a summary of the heap memory usage in a Java process. This can help in analyzing memory allocation and identifying any potential memory leaks.
Explanation:
-heap
: This flag tells the ‘jmap’ command to print the heap summary information.java_pid
: This is the Process ID of the Java process for which you want to print the heap summary.
Example output:
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 1300234240 (1240.0MB)
NewSize = 1048576 (1.0MB)
MaxNewSize = 17592186044415 MB
OldSize = 4194304 (4.0MB)
...
Use case 3: Print histogram of heap usage by type
Code:
jmap -histo java_pid
Motivation: The motivation for using this use case is to obtain a histogram of the heap usage by type in a Java process. This can help in identifying which types of objects are taking up the most memory in the heap.
Explanation:
-histo
: This flag tells the ‘jmap’ command to print the histogram of heap usage.java_pid
: This is the Process ID of the Java process for which you want to print the histogram.
Example output:
num #instances #bytes class name
----------------------------------------------
1: 1862 22908272 [C
2: 154972 12080496 java.lang.String
3: 89788 3584984 [B
...
Use case 4: Dump contents of the heap into a binary file for analysis with jhat
Code:
jmap -dump:format=b,file=path/to/file java_pid
Motivation: The motivation for using this use case is to take a snapshot of the heap memory in a Java process and save it in a binary file format. This file can then be analyzed using the ‘jhat’ tool for troubleshooting memory-related issues.
Explanation:
-dump:format=b,file=path/to/file
: This flag tells the ‘jmap’ command to dump the contents of the heap in binary format and save it to the specified file.java_pid
: This is the Process ID of the Java process for which you want to dump the heap.
Example output:
Dumping heap to /path/to/file ...
Heap dump file created
Use case 5: Dump live objects of the heap into a binary file for analysis with jhat
Code:
jmap -dump:live,format=b,file=path/to/file java_pid
Motivation: The motivation for using this use case is to take a snapshot of only the live objects in the heap memory of a Java process and save it in a binary file format. This file can then be analyzed using the ‘jhat’ tool to identify any memory leaks.
Explanation:
-dump:live,format=b,file=path/to/file
: This flag tells the ‘jmap’ command to dump only the live objects in the heap in binary format and save it to the specified file.java_pid
: This is the Process ID of the Java process for which you want to dump the live objects in the heap.
Example output:
Dumping heap to /path/to/file ...
Heap dump file created
Conclusion:
The ‘jmap’ command is a useful tool for analyzing and troubleshooting memory-related issues in Java applications. It provides various options to obtain different memory-related information and perform heap analysis. By using the provided use cases and examples, you can effectively utilize the ‘jmap’ command for your memory analysis needs.