Thursday, July 12, 2012

jstat - Java Virtual Machine Statistics Monitoring Tool

If you suspect something wrong in a long-running application, one thing to check is to see if garbage collection is working healthily or not.

In this article, we will introduce you one way to monitor the health of JVM's garbage collection activities.

jstat

You can find the Java Virtual Machine Statistics Monitoring Tool named jstat in the following folder:
  • ${JDK_INST}/bin/jstat
The jstat utility uses the built-in instrumentation in the HotSpot VM to provide information on performance and resource consumption of running applications. The tool can be used when diagnosing performance issues, and in particular issues related to heap sizing and garbage collection. The utility does not require that the application be started with any special options as instrumentation is enabled by default.

jstat provides different options which determine the statistics information that jstat displays. To find the list of options for a particular platform installation that jstat displays, you type:
  • jstat -option
For example, it displays the following supported options for my Linux platform:
  • -class
    • displays statistics on the behavior of the class loader
  • -compiler
    • displays statistics of the behavior of the HotSpot Just-in-Time compiler
  • -gc
    • displays statistics of the behavior of the garbage collected heap
  • -gccapacity
    • displays statistics of the capacities of the generations and their corresponding spaces
  • -gccause
    • displays summary of garbage collection statistics (same as -gcutil), with the cause of the last and current (if applicable) garbage collection events
  • -gcnew
    • displays statistics of the behavior of the new generation
  • -gcnewcapacity
    • displays statistics of the sizes of the new generations and its corresponding spaces
  • -gcold
    • displays statistics of the behavior of the old and permanent generations
  • -gcoldcapacity
    • displays statistics of the sizes of the old generation
  • -gcpermcapacity
    • displays statistics of the sizes of the permanent generation
  • -gcutil
    • displays summary of garbage collection statistics
  • -printcompilation
    • displays HotSpot compilation method statistics

-gcutil Option

In this demonstration, we will choose -gcutil to display garbage collection statistics.  You can find the meaning of displayed columns below:

Summary of Garbage Collection Statistics
ColumnDescription
S0Survivor space 0 utilization as a percentage of the space's current capacity.
S1Survivor space 1 utilization as a percentage of the space's current capacity.
EEden space utilization as a percentage of the space's current capacity.
OOld space utilization as a percentage of the space's current capacity.
PPermanent space utilization as a percentage of the space's current capacity.
YGCNumber of young generation GC events.
YGCTYoung generation garbage collection time.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

The Finding

In our case, we have found the following statistics information:
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    11  416.676  471.872
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    11  416.676  471.872
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    11  416.676  471.872
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    11  416.676  471.872

Note that 9852 is the process id that jstat retrieves statistics from. You can also use `pgrep java` to retrieve the process id directly. For example, you can specify the above command line as:
  • jstat -gcutil `pgrep java` 

Then, after a little while, we print the statistics information again:
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    25 1008.989 1064.186
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    25 1008.989 1064.186
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    25 1008.989 1064.186
$ jstat -gcutil 9852
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00 100.00 100.00  23.68    447   55.197    25 1008.989 1064.186

This has helped us to find out that our process was stuck because it is out of heap and all it is doing is garbage collection. In other words, O (old space) and E (eden space) are continually full.

References

  1. jstat - Java Virtual Machine Statistics Monitoring Tool
  2. Java 2 Platform, Standard Edition 5.0 "Trouobingshooting and Diagnostic Guide"
  3. Monitoring Java garbage collection with jstat

No comments: