JMap usage notes
Notes how to use jmap
program from JDK.
jmap
is a command-line utility provided by the Java Development Kit (JDK) for various memory-related tasks. Here are some of the most common usages of the jmap
command, along with example commands and explanations:
-
Generate a Heap Dump:
- Usage: Create a memory dump (heap dump) of a Java process, which includes information about the Java objects and memory usage.
- Command:
jmap -dump:file=/path/to/dump.hprof <process_id>
- Explanation:
This command generates a heap dump of the Java process identified by
<process_id>
and saves it to the specified file/path/to/dump.hprof
. A heap dump is helpful for analyzing memory-related issues like memory leaks.
-
List Class Histogram:
- Usage: Display a histogram of all classes and their instance counts in the Java process.
- Command:
jmap -histo <process_id>
- Explanation: Running this command provides a list of all loaded classes in the Java process along with the number of instances of each class. It helps identify which classes are consuming the most memory.
-
List Summary Information:
- Usage: Display summary information about the Java heap, including the total heap size, used and free memory, and garbage collector details.
- Command:
jmap -heap <process_id>
- Explanation: This command provides an overview of the heap memory usage, including heap size, utilization, and garbage collector statistics. It’s useful for monitoring the overall memory status of a Java process.
-
Generate a Binary Histo File:
- Usage: Create a binary histogram file, which contains detailed information about class instances and their sizes.
- Command:
jmap -histo:live <process_id> > histo.txt
- Explanation:
This command generates a binary histogram file and redirects the output to a text file (
histo.txt
). The:live
option only includes live objects in the histogram, excluding unreachable objects.
-
List Java Threads and Stack Traces:
- Usage: Display a list of all threads in the Java process and their stack traces.
- Command:
jmap -threads <process_id>
- Explanation: Running this command provides a snapshot of all Java threads and their current stack traces. It’s helpful for diagnosing thread-related issues or deadlocks.
-
Force a Garbage Collection Cycle:
- Usage: Trigger a garbage collection cycle in the Java process.
- Command:
jmap -histo:live <process_id>
- Explanation: This command forces a garbage collection cycle to occur in the target Java process. It can be useful for testing or analyzing the effects of garbage collection on memory usage.
These are some of the most common usages of the jmap
command. Depending on your specific debugging or profiling needs, you can choose the appropriate jmap
command to help analyze and troubleshoot memory-related issues in a Java application.
Page link: /posts/jmap-notes/