How to use Jstat for monitoring the garbage collection in java


Contents of page >
  • What is purpose of using jstat ?
  • 1) First Write java program - (which we will later analyze with jstat )>
  • 2) Use jps to find the vmid (virtual machine id i.e. JVM id)
  • 3) Using jstat - for analyzing and monitoring the garbage collection in java
    • From java docs about jstat
    • Full form of jtsat>
    • Java Virtual Machine Statistics Monitoring Tool
    • Location of jstat?

    • 3.1) Using jstat -gc
    • 3.2) Using jstat -gcutil
    • 3.3) What all more jstat options are available ?
  • 4) Troubleshooting with Jstat >


What is purpose of using jstat ?
Jstat is used for analyzing and monitoring the garbage collection in java. So, we will learn how to use jstat to analyze and monitor the garbage collection in java.

1) First Write java program - (which we will later analyze with jstat )>
import java.util.ArrayList;
import java.util.List;
public class JstatTestingGarbageCollectionExample {
   public static void main(String[] args) {
          List<String> l = new ArrayList<String>();
          for (int i = 0; i < 100000000; i++) {
                 l = new ArrayList<String>();
                 l.add("a");
                 System.out.println(l);
          }
          System.out.println("Done");
   }
}

Execute the above java program. And this program will take long time to complete (Because of for loop).
And while program is executing follow step 2 (i.e. before program execution completes - If we execute below steps after programs execution is over - output will be command not found).


2) Use jps to find the vmid (virtual machine id i.e. JVM id)

What is jps?
JPS is Java Virtual Machine Process Status Tool.
The jps tool lists the HotSpot Java Virtual Machines (JVMs) running on system.

Location of jps?
jps is located in {javaHome\bin}

In my system jps is located in C:\Program Files\Java\jdk1.8.0_05\bin\jps.exe


Go to CMD and type below commands >
C:\>cd C:\Program Files\Java\jdk1.8.0_05\bin

C:\Program Files\Java\jdk1.8.0_05\bin>jps
16048
1108 JstatTestingGarbageCollectionExample
23208 Jps

C:\Program Files\Java\jdk1.8.0_05\bin>


1108 is the vmid which we need.


3) Using jstat - for analyzing and monitoring the garbage collection in java

From java docs about jstat
The jstat command displays performance statistics for an instrumented Java HotSpot VM. The target JVM is identified by its virtual machine identifier, or vmid option.

Full form of jtsat>
Java Virtual Machine Statistics Monitoring Tool

Location of jstat?
jstat is located in {javaHome\bin}

In my system jps is located in C:\Program Files\Java\jdk1.8.0_05\bin\jps.exe

3.1) Using jstat -gc

Go to CMD and type jstat -gc 1108 2000
Where 1108 is the vmid  (picked from above). And,
2000 (2 seconds) is the time in which garbage collection details will auto update garbage collection data in cmd.

Statistics of the behavior of the garbage collected heap >

C:\Program Files\Java\jdk1.8.0_05\bin>jstat -gc 1108 2000
S0C    S1C    S0U   S1U      EC          EU           OC          OU        MC        MU     CCSC  CCSU  YGC   YGCT  FGC  FGCT   GCT
512.0  512.0   0.0    32.0  31232.0  18740.0   175104.0    304.1    4864.0  2435.9  512.0   266.9     297    0.591   0      0.000    0.591
512.0  512.0   0.0    64.0  31232.0  19989.0   175104.0    304.1    4864.0  2435.9  512.0   266.9     301    0.591   0      0.000    0.591
512.0  512.0   32.0   0.0   31232.0   4372.6    175104.0    304.1    4864.0  2435.9  512.0   266.9     310    0.603   0      0.000    0.603
512.0  512.0   32.0   0.0   31232.0  13117.7   175104.0    304.1    4864.0  2435.9  512.0   266.9     314    0.609   0      0.000    0.609





Column
Description
S0C
Current survivor space 0 capacity (KB).
S1C
Current survivor space 1 capacity (KB).
S0U
Survivor space 0 utilization (KB).
S1U
Survivor space 1 utilization (KB).
EC
Current eden space capacity (KB).
EU
Eden space utilization (KB).
OC
Current old space capacity (KB).
OU
Old space utilization (KB).
PC
Current permanent space capacity (KB).
PU
Permanent space utilization (KB).
YGC
Number of young generation GC Events.
YGCT
Young generation garbage collection time.
FGC
Number of full GC events.
FGCT
Full garbage collection time.
GCT
Total garbage collection time.


3.2) Using jstat -gcutil

Go to CMD and type jstat -gcutil 1108 2000

Statistics of the behavior of the garbage collected heap >

C:\Program Files\Java\jdk1.8.0_05\bin>jstat -gcutil 1108 2000
 S0      S1        E          O        M      CCS    YGC   YGCT  FGC  FGCT   GCT
 6.25   0.00  82.00   0.17  50.08  52.12    438    0.761     0     0.000    0.761
 0.00   6.25   0.00    0.17  50.08  52.12    443    0.762     0     0.000    0.762
 0.00   6.25  24.00   0.17  50.08  52.12    447    0.764     0     0.000    0.764
 0.00  12.50  46.00  0.17  50.08  52.12    451    0.771     0     0.000    0.771



Garbage-collected heap statistics >
Column
Description
S0
Survivor space 0 utilization as a percentage of the space's current capacity.
S1
Survivor space 1 utilization as a percentage of the space's current capacity.
E
Eden space utilization as a percentage of the space's current capacity.
O
Old space utilization as a percentage of the space's current capacity.
P
Permanent space utilization as a percentage of the space's current capacity.
YGC
Number of young generation GC events.
YGCT
Young generation garbage collection time.
FGC
Number of full GC events.
FGCT
Full garbage collection time.
GCT
Total garbage collection time.




3.3) What all more jstat options are available ?
Jstat Option
Displays
Statistics of the behavior of the garbage collected heap.
Summary of garbage collection statistics.
Statistics on the behavior of the class loader.
Statistics of the behavior of the HotSpot Just-in-Time compiler.
Statistics of the capacities of the generations and their corresponding spaces.
Summary of garbage collection statistics (same as -gcutil), with the cause of the last and current (if applicable) garbage collection events.
Statistics of the behavior of the new generation.
Statistics of the sizes of the new generations and its corresponding spaces.
Statistics of the behavior of the old and permanent generations.
Statistics of the sizes of the old generation.
Statistics of the sizes of the permanent generation.
HotSpot compilation method statistics.





4) Troubleshooting with Jstat >

Solve few error may may face - jstat system cannot find specified path
Please ensure jstat in your classpath. I.e.
Environment variable you have set is same jdk path mentioned above (i.e. C:\Program Files\Java\jdk1.8.0_05\bin) and vmid (i.e. xxx) you are passing is using the same jdk (java version).



Summary -

So in this core java tutorial we learned how to use Jstat for monitoring the garbage collection in java.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java



No comments:

Post a Comment