jmap tool
Generating Heap Dump
Use the -dump
option to generate a heap dump file, which is very useful for analyzing memory leaks and other memory-related issues:
jmap -dump:format=b,file=<filename> <PID>
Example:
jmap -dump:format=b,file=heap_dump.hprof 12345
This will generate a heap dump file named heap_dump.hprof
for subsequent analysis.
Printing Heap Summary
Use the -heap
option to print a detailed summary of the heap, including garbage collectors, heap configuration, and usage:
jmap -heap <PID>
Example output:
Attaching to process ID 12345, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 1.8.0_201
using thread-local object allocation.
Garbage-First (G1) GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 1073741824 (1024.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 17592186044415 MB
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
G1 Heap:
regions = 2048
capacity = 1090519040 (1038.75MB)
used = 1048576 (1.0MB)
free = 1089470464 (1037.75MB)
0.0962471971272651% used
Field explanations:
- MinHeapFreeRatio / MaxHeapFreeRatio: Minimum and maximum heap free ratios.
- MaxHeapSize: Maximum heap size.
- NewSize / MaxNewSize: Initial and maximum size of the young generation.
- OldSize: Initial size of the old generation.
- NewRatio: Ratio of the young generation to the old generation.
- SurvivorRatio: Ratio of the Eden space to the Survivor space.
- MetaspaceSize / MaxMetaspaceSize: Initial and maximum size of the Metaspace.
- CompressedClassSpaceSize: Size of the compressed class space.
- G1HeapRegionSize: Size of the heap regions for the G1 garbage collector.
- Heap Usage: Shows the current usage of heap memory, including the number of regions, total capacity, used space, and free space.
Printing Histogram of Objects in Heap
Use the -histo
option to print a histogram of objects in the heap, including the number of instances and total bytes for each type of object:
jmap -histo <PID>
Example output:
num #instances #bytes class name
----------------------------------------------
1: 57420 915048 java.lang.String
2: 18763 301048
Displaying Heap Configuration and Usage
Use the -heap
option to display the heap configuration and usage, including garbage collector type, initial and maximum heap size, and usage:
jmap -heap <PID>
Example output:
Attaching to process ID 12345, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 1.8.0_201
using thread-local object allocation.
Garbage-First (G1) GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 1073741824 (1024.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 17592186044415 MB
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
G1 Heap:
regions = 2048
capacity = 1090519040 (1038.75MB)
used = 1048576 (1.0MB)
free = 1089470464 (1037.75MB)
0.0962471971272651% used
Eden Space:
regions = 2048
capacity = 10485760 (10.0MB)
used = 5242880 (5.0MB)
free = 5242880 (5.0MB)
50.0% used
Survivor Space:
regions = 512
capacity = 5242880 (5.0MB)
used = 2621440 (2.5MB)
free = 2621440 (2.5MB)
50.0% used
Old Space:
regions = 1024
capacity = 52428800 (50.0MB)
used = 26214400 (25.0MB)
free = 26214400 (25.0MB)
50.0% used
Metaspace:
capacity = 104857600 (100.0MB)
used = 52428800 (50.0MB)
free = 52428800 (50.0MB)
50.0% used
Compressed Class Space:
capacity = 10485760 (10.0MB)
used = 5242880 (5.0MB)
free = 5242880 (5.0MB)
50.0% used
Field explanations:
- Heap Configuration: Shows heap memory configuration parameters such as minimum and maximum heap free ratios, maximum heap size, young generation size ratio, etc.
- Heap Usage: Displays the current usage of heap memory, including the number of regions, total capacity, used space, and free space.
- Eden Space: Usage of the young generation Eden space.
- Survivor Space: Usage of the young generation Survivor space.
- Old Space: Usage of the old generation.
- Metaspace: Usage of the Metaspace (method area).
- Compressed Class Space: Usage of the compressed class space.
Displaying Memory Usage of Class Loaders
Use the -clstats
option to display memory usage statistics of class loaders:
jmap -clstats <PID>
Example output:
class_loader classes_loaded classes_unloaded bytes_loaded bytes_unloaded
--------------------------------------------------------------------------------------
bootstrap 500 10 2048000 102400
system 200 5 1024000 51200
Field explanations:
- class_loader: Name of the class loader.
- classes_loaded: Number of classes loaded.
- classes_unloaded: Number of classes unloaded.
- bytes_loaded: Number of bytes occupied by loaded classes.
- bytes_unloaded: Number of bytes freed by unloaded classes.
Printing Histogram of Objects in Heap
Use the -histo
option to print a histogram of objects in the heap, including the number of instances and total bytes for each type of object:
jmap -histo <PID>
Example output:
num #instances #bytes class name
----------------------------------------------
1: 57420 915048 java.lang.String
2: 18763 301048 java.lang.Integer
3: 9500 152000