jstat: Monitoring Class Loading, Memory, and Garbage Collection
The jstat utility provides performance statistics for a specified Java Virtual Machine (JVM). It can display information about class loading, memory usage, and garbage collection (GC) activities.
Common Options:
-class: Displays class loader statistics.-compiler: Displays information about the Just-In-Time (JIT) compiler.-gc: Displays garbage collection heap statistics.-gccapacity: Displays heap capacity statistics.-gcmetacapacity: Displays metaspace capacity statistics.-gcnew: Displays statistics about the young generation.-gcnewcapacity: Displays young generation capacity statistics.-gcold: Displays statistics about the old generation.-gcoldcapacity: Displays old generation capacity statistics.-gcutil: Displays a summary of GC statistics.-gccause: Dispalys GC statistics with causes.-printcompilation: Displays JIT compiler statistics.
Example Usage:
Class Loading Statistics
# jstat -class 12345
Loaded Bytes Unloaded Bytes Time
18950 38765.2 180 285.1 36.12
Interpretation: Shows the number of classes loaded and unloaded, along with the memory they occupy and the time spent on these operations.
Garbage Collection Statistics
# jstat -gc 12345
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
31200 24000 0.0 23456 716800 261000 1550400 640000 271424 118976 16384 15000 4850 94.5 3 3.15 97.65
Interpretation: Displays heap usage, including Eden, Survivor (S0, S1), Old, and Metaspace regions, along with GC counts and times.
Heap Capacity Statistics
# jstat -gccapacity 12345
NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC PGCMN PGCMX PGC PC YGC FGC
74500 74500 74500 31200 30100 71500 19800 19800 19800 19800 27142 54284 27142 27142 4861 3
Interpretation: Shows the minimum, maximum, and current sizes of the young and old generations.
Metaspace Capacity Statistics
# jstat -gcmetacapacity 12345
MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC FGCT GCT
0.0 1200000 145000 0.0 1080000 15600 3680 6 2.48 60.91
Interpretation: Displays metaspace (or permanent generation in older JVMs) capacity and usage.
Garbage Collection Summary
# jstat -gcutil 12345
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
0.00 5.10 84.12 90.12 93.22 87.45 3690 58.23 6 2.45 60.68
Interpretation: Provides a high-level summmary of GC activity as percentages of heap usage.
jmap: Analyzing Java Heap Memory
The jmap command is used to print memory-related data from a running JVM or a core file. It can generate heap dumps, show heap usage, and list objects.
Common Options:
-dump: Dumps the Java heap to a file.-heap: Prints the heap summary.-histo: Prints a histogram of heap usage by class.-finalizerinfo: Prints information about objects waiting for finalization.-clstats: Prints class loader statistics.
Example Usage:
Display Heap Summary
# jmap -heap 12345
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 2375754240 (2266.0MB)
NewSize = 786432000 (750.0MB)
MaxNewSize = 786432000 (750.0MB)
OldSize = 1589248000 (1516.0MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 272629760 (260.0MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 549755813888 (512000.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 781826560 (745.5MB)
used = 291000000 (277.4MB)
free = 490826560 (468.1MB)
37.23% used
From Space:
capacity = 4325376 (4.1MB)
used = 1830000 (1.7MB)
free = 2495376 (2.4MB)
42.31% used
To Space:
capacity = 4325376 (4.1MB)
used = 0 (0.0MB)
free = 4325376 (4.1MB)
0.0% used
PS Old Generation
capacity = 1589248000 (1516.0MB)
used = 448000000 (427.2MB)
free = 1141248000 (1088.8MB)
28.19% used
PS Metaspace
capacity = 272629760 (260.0MB)
used = 122000000 (116.3MB)
free = 150629760 (143.7MB)
44.75% used
Generate a Heap Dump
This command creates a binary snapshot of the heap, which can be analyzed offline using tools like jhat or VisualVM.
# jmap -dump:live,format=b,file=heap_dump.bin 12345
Object Histogram
Lists the number of instances and memory usage for each class.
# jmap -histo 12345
num #instances #bytes class name
-----------------------------------------
1: 450000 60000000 [C
2: 1100000 35200000 com.example.data.Model
3: 16500 33000000 [B
4: 640000 20500000 [Lcom.example.data.Model;
5: 460000 14720000 java.util.HashMap$Node
jinfo: Inspecting and Modifying JVM Flags
The jinfo command prints configuration information for a given Java process, including system properties and JVM flags. It can also be used to dynamically change certain flags at runtime.
Common Options:
-flags: Prints the flags passed to the JVM.-sysprops: Prints system properties.-flag <name>: Prints the value of a specific flag.-flag [+|-]<name>: Enables or disables a boolean flag.-flag <name>=<value>: Sets a flag to a specific value.
Example Usage:
Display All System Properties and Flags
# jinfo 12345
Check a Specific Flag Value
# jinfo -flag MaxHeapSize 12345
-XX:MaxHeapSize=2375754240
Enable a Flag
# jinfo -flag +PrintGCDetails 12345
Modify a Flag Value
Note: Not all flags can be modified at runtime.
# jinfo -flag HeapDumpPath=/opt/dumps 12345
jstack: Diagnosing Thread Issues
The jstack command prints a Java thread dump. A thread dump is a snapshot of all threads runing in the JVM, which is invaluable for debugging performance issues, deadlocks, and thread contention.
Common Options:
-l: Long listing. Includes additional information about locks.-m: Mixed mode. Includes both Java and native frames.-F: Force a thread dump if the process is unresponsive.
Example Usage:
Generate a Thread Dump
# jstack -l 12345
2024-05-21 10:30:00
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.321-b10 mixed mode):
"main" #1 prio=5 os_prio=0 cpu=12.34ms elapsed=120.56s tid=0x00007f8c0c0a9800 nid=0x1 runnable [0x00007f8c0d1ff000]
java.lang.Thread.State: RUNNABLE
at com.example.service.Processor.run(Processor.java:45)
- locked <0x000000076abc3f58> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:834)
"http-nio-8080-exec-8" #12 prio=5 os_prio=0 cpu=89.12ms elapsed=45.78s tid=0x00007f8c0c0b1000 nid=0x2 waiting on condition [0x00007f8c0d3ff000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000076abc4000> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:834)
Detecting Deadlocks
1. Identify the process ID (PID) with high CPU usage using top.
2. Get the thread ID (TID) of the problematic thread.
3. Convert the TID to hexadecimal.
4. Use jstack to find the thread and analyze its stack trace.
# top -H -p 12345
# printf "%x
" 28888
70e8
# jstack 12345 | grep 70e8 -A 20