Overview
jstat is a small but very useful JDK tool for checking how JVM memory is being used, especially heap regions and class-loading activity. In JDK 1.8, it can also show different garbage collection statistics, metaspace information, and JIT compilation behavior.
The basic syntax is:
jstat [-命令选项] [vmid] [间隔时间/默认单位是ms毫秒] [查询次数]
One important note before using the examples below: the command output and field meanings here are based on JDK 1.8.
Checking the available syntax and options
If you want to see the complete command format, run jstat -help:
[root@root ~]# jstat -help
Usage: jstat -help|-options
jstat - [-t] [-h] [ []]
Definitions:
An option reported by the -options option
Virtual Machine Identifier. A vmid takes the following form:
[@[:]]
Where is the local vm identifier for the target
Java virtual machine, typically a process id; is
the name of the host running the target Java virtual machine;
and is the port number for the rmiregistry on the
target host. See the jvmstat documentation for a more complete
description of the Virtual Machine Identifier.
Number of samples between header lines.
Sampling interval. The following forms are allowed:
["ms"|"s"]
Where is an integer and the suffix specifies the units as
milliseconds("ms") or seconds("s"). The default units are "ms".
Number of samples to take before terminating.
-J Pass directly to the runtime system.
To see which option values are available, use jstat -options:
[root@infra2-test-k8s ~]# jstat -options
-class
-compiler
-gc
-gccapacity
-gccause
-gcmetacapacity
-gcnew
-gcnewcapacity
-gcold
-gcoldcapacity
-gcutil
-printcompilation
These options correspond to different JVM views:
-class: statistics for class loading behavior.-compiler: statistics for the HotSpot JIT compiler.-gc: overall garbage-collected heap statistics.-gccapacity: capacities of the young generation, old generation, metaspace, and related spaces.-gccause: similar to-gcutil, but also includes the cause of the most recent GC and the current GC when applicable.-gcmetacapacity: metaspace size statistics.-gcnew: new generation behavior statistics.-gcnewcapacity: size statistics for the new generation and its spaces.-gcold: old generation and metaspace behavior statistics.-gcoldcapacity: old generation size statistics.-gcutil: utilization percentages across memory regions plus GC summary data.-printcompilation: HotSpot method compilation statistics.
First, find the target JVM process
To make the examples more direct, you can use jps to list Java processes and their process IDs:
[root@root ~]# jps -l
1 org.apache.catalina.startup.Bootstrap
17546 sun.tools.jps.Jps
In this case, the target JVM process ID is 1.
The jps command format is:
jps命令格式:jps [options ] [ hostid ]
[options]选项 : -q:仅输出VM标识符,不包括classname,jar name,arguments in main method -m:输出main method的参数 -l:输出完全的包名,应用主类名,jar的完全路径名 -v:输出jvm参数 -V:输出通过flag文件传递到JVM中的参数(.hotspotrc文件或-XX:Flags=所指定的文件 -Joption:传递参数到vm,例如:-J-Xms512m
Once you have the PID, you can pass it to jstat.
jstat -class: class loading statistics
[root@root ~]# jstat -class 1
Loaded Bytes Unloaded Bytes Time
24301 45978.0 277 411.9 44.91
Field meanings:
- Loaded: number of classes loaded.
- Bytes: total size of loaded classes, in KB.
- Unloaded: number of classes unloaded.
- Bytes: total size of unloaded classes, in KB.
- Time: time spent on class loading and unloading.
jstat -compiler: JIT compiler statistics
[root@root ~]# jstat -compiler 1
Compiled Failed Invalid Time FailedType FailedMethod
31073 3 0 182.63 1 com/mysql/jdbc/AbandonedConnectionCleanupThread run
Field meanings:
- Compiled: number of compilation tasks completed.
- Failed: number of compilation tasks that failed.
- Invalid: number of compilation tasks invalidated.
- Time: total compilation time.
- FailedType: compile type of the last failed compilation.
- FailedMethod: class and method of the last failed compilation.
jstat -gc: heap and GC behavior
This view is one of the most commonly used because it gives a broad picture of heap usage and GC activity.
[root@root ~]# jstat -gc 1
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
17472.0 17472.0 0.0 1248.6 139776.0 7359.2 349568.0 233485.7 149632.0 144590.5 16768.0 15863.7 1119 11.856 11 2.871 14.727
Field meanings:
- S0C: current capacity of Survivor 0, in KB.
- S1C: current capacity of Survivor 1, in KB.
- S0U: used space in Survivor 0, in KB.
- S1U: used space in Survivor 1, in KB.
- EC: current Eden capacity, in KB.
- EU: used Eden space, in KB.
- OC: current old generation capacity, in KB.
- OU: used old generation space, in KB.
- MC: metaspace capacity, in KB.
- MU: used metaspace, in KB.
- CCSC: compressed class space capacity, in KB.
- CCSU: used compressed class space, in KB.
- YGC: number of young generation GC events.
- YGCT: total time spent in young generation GC.
- FGC: number of Full GC events.
- FGCT: total time spent in Full GC.
- GCT: total GC time.
jstat -gccapacity: memory pool capacities
This option focuses on the size of each generation and space rather than current utilization.
[root@root ~]# jstat -gccapacity 1
NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
10880.0 174720.0 174720.0 17472.0 17472.0 139776.0 21888.0 349568.0 349568.0 349568.0 0.0 1181696.0 149632.0 0.0 1048576.0 16768.0 1119 11
Field meanings:
- NGCMN: minimum new generation capacity (KB).
- NGCMX: maximum new generation capacity (KB).
- NGC: current new generation capacity (KB).
- S0C: current Survivor 0 capacity (KB).
- S1C: current Survivor 1 capacity (KB).
- EC: current Eden capacity (KB).
- OGCMN: minimum old generation capacity (KB).
- OGCMX: maximum old generation capacity (KB).
- OGC: current old generation capacity (KB).
- OC: current old space capacity (KB).
- MCMN: minimum metaspace capacity (KB).
- MCMX: maximum metaspace capacity (KB).
- MC: current metaspace capacity (KB).
- CCSMN: minimum compressed class space capacity (KB).
- CCSMX: maximum compressed class space capacity (KB).
- CCSC: current compressed class space capacity (KB).
- YGC: number of young generation GC events.
- FGC: number of Full GC events.
jstat -gccause: GC summary with causes
This output is similar to -gcutil, but it also tells you why the last GC happened and whether a GC is currently in progress.
[root@root ~]# jstat -gccause 1
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT LGCC GCC
6.67 0.00 90.76 66.79 96.66 94.62 1130 11.949 11 2.871 14.820 Allocation Failure No GC
Field meanings:
- S0: Survivor 0 utilization as a percentage.
- S1: Survivor 1 utilization as a percentage.
- E: Eden utilization as a percentage.
- O: old generation utilization as a percentage.
- M: metaspace utilization as a percentage.
- CCS: compressed class space utilization as a percentage.
- YGC: number of young generation GC events.
- FGC: number of Full GC events.
- LGCC: cause of the most recent GC.
- GCC: cause of the current GC.
jstat -gcnew: new generation statistics
[root@root ~]# jstat -gcnew 1
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
17472.0 17472.0 0.0 1350.4 15 15 8736.0 139776.0 73826.1 1131 11.957
Field meanings:
- S0C: Survivor 0 capacity (KB).
- S1C: Survivor 1 capacity (KB).
- S0U: used Survivor 0 space (KB).
- S1U: used Survivor 1 space (KB).
- TT: tenuring threshold.
- MTT: maximum tenuring threshold.
- DSS: desired survivor size (KB).
- EC: Eden capacity (KB).
- EU: used Eden space (KB).
- YGC: number of young generation GC events.
- YGCT: total young generation GC time.
jstat -gcnewcapacity: new generation size details
[root@root ~]# jstat -gcnewcapacity 1
NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC
10880.0 174720.0 174720.0 17472.0 17472.0 17472.0 17472.0 139776.0 139776.0 1131 11
Field meanings:
- NGCMN: minimum new generation capacity (KB).
- NGCMX: maximum new generation capacity (KB).
- NGC: current new generation capacity (KB).
- S0CMX: maximum Survivor 0 capacity (KB).
- S0C: current Survivor 0 capacity (KB).
- S1CMX: maximum Survivor 1 capacity (KB).
- S1C: current Survivor 1 capacity (KB).
- ECMX: maximum Eden capacity (KB).
- EC: current Eden capacity (KB).
- YGC: number of young generation GC events.
- FGC: number of Full GC events.
jstat -gcold: old generation and metaspace usage
[root@root ~]# jstat -gcold 1
MC MU CCSC CCSU OC OU YGC FGC FGCT GCT
149632.0 144638.0 16768.0 15866.4 349568.0 233488.9 1132 11 2.871 14.841
Field meanings:
- MC: metaspace capacity (KB).
- MU: used metaspace (KB).
- CCSC: compressed class space capacity (KB).
- CCSU: used compressed class space (KB).
- OC: old generation capacity (KB).
- OU: used old generation space (KB).
- YGC: number of young generation GC events.
- FGC: number of Full GC events.
- FGCT: Full GC time.
- GCT: total GC time.
jstat -gcoldcapacity: old generation capacity
[root@root ~]# jstat -gcoldcapacity 1
OGCMN OGCMX OGC OC YGC FGC FGCT GCT
21888.0 349568.0 349568.0 349568.0 1132 11 2.871 14.841
Field meanings:
- OGCMN: minimum old generation capacity (KB).
- OGCMX: maximum old generation capacity (KB).
- OGC: current old generation capacity (KB).
- OC: current old space capacity (KB).
- YGC: number of young generation GC events.
- FGC: number of Full GC events.
- FGCT: Full GC time.
- GCT: total GC time.
jstat -gcmetacapacity: metaspace capacity
[root@root ~]# jstat -gcmetacapacity 1
MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC FGCT GCT
0.0 1181696.0 149632.0 0.0 1048576.0 16768.0 1131 11 2.871 14.828
Field meanings:
- MCMN: minimum metaspace capacity (KB).
- MCMX: maximum metaspace capacity (KB).
- MC: current metaspace capacity (KB).
- CCSMN: minimum compressed class space capacity (KB).
- CCSMX: maximum compressed class space capacity (KB).
- CCSC: current compressed class space capacity (KB).
- YGC: number of young generation GC events.
- FGC: number of Full GC events.
- FGCT: Full GC time.
- GCT: total GC time.
jstat -gcutil: utilization summary
If you want a compact view focused on percentages and GC counts, this is often the easiest option to read.
[root@root ~]# jstat -gcutil 1
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
6.68 0.00 39.54 66.79 96.66 94.62 1132 11.970 11 2.871 14.841
Field meanings:
- S0: Survivor 0 usage percentage.
- S1: Survivor 1 usage percentage.
- E: Eden usage percentage.
- O: old generation usage percentage.
- M: metaspace usage percentage.
- CCS: compressed class space usage percentage.
- YGC: young generation GC count.
- YGCT: young generation GC time.
- FGC: Full GC count.
- FGCT: Full GC time.
- GCT: total GC time.
jstat -printcompilation: recently compiled method statistics
[root@root ~]# jstat -printcompilation 1
Compiled Size Type Method
31174 1055 1 sun/reflect/GeneratedMethodAccessor214 invoke
Field meanings:
- Compiled: number of compilation tasks performed by the most recently compiled method.
- Size: bytecode size of the most recently compiled method.
- Type: compilation type of the most recently compiled method.
- Method: class name and method name of the most recently compiled method. HotSpot uses
/instead of.in the class name.
Sampling output repeatedly
jstat can also print statistics at intervals instead of only once. For example, -h3 tells it to reprint the header every three lines, and 1s sets the sampling interval to one second:
[root@root ~]# jstat -gcnew -h3 1 1s
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 51283.7 1133 11.980
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 51283.8 1133 11.980
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 51674.1 1133 11.980
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 51816.1 1133 11.980
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 51824.8 1133 11.980
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 51828.9 1133 11.980
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 52158.8 1133 11.980
17472.0 17472.0 0.0 1359.4 15 15 8736.0 139776.0 52312.8 1133 11.980
This kind of output is handy when you want to watch Eden growth, survivor movement, or GC counters changing over time.
Notes on reading the metrics
A few patterns are especially useful when looking at jstat output on JDK 8:
- If E or EU rises quickly and YGC keeps increasing, the application is generating many short-lived objects.
- If O or OU stays high and FGC increases, pressure may be moving into the old generation.
- If M or MU is close to its limit, metaspace usage deserves attention.
-gccauseis helpful when you want to know not just that GC happened, but what triggered it.-gcutilis usually the fastest way to get an overall impression, while-gc,-gcnew, and-gcoldare better when you need more detail.
For day-to-day troubleshooting, a common flow is: use jps to find the target JVM, use jstat -gcutil or jstat -gc for a quick health check, and then switch to more specific options depending on whether you are investigating young generation pressure, old generation growth, metaspace, or compilation behavior.