Wednesday 5 June 2019

jstat: Monitor Java heap, threads, classes


Jstat is a command line virtual machine statistics monitoring tool available as part of Java installation.

If you install java, open command line or terminal and execute the command ‘jstat’.

$jstat
invalid argument count
Usage: jstat -help|-options
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

Definitions:
  <option>      An option reported by the -options option
  <vmid>        Virtual Machine Identifier. A vmid takes the following form:
                     <lvmid>[@<hostname>[:<port>]]
                Where <lvmid> is the local vm identifier for the target
                Java virtual machine, typically a process id; <hostname> is
                the name of the host running the target Java virtual machine;
                and <port> 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.
  <lines>       Number of samples between header lines.
  <interval>    Sampling interval. The following forms are allowed:
                    <n>["ms"|"s"]
                Where <n> is an integer and the suffix specifies the units as 
                milliseconds("ms") or seconds("s"). The default units are "ms".
  <count>       Number of samples to take before terminating.
  -J<flag>      Pass <flag> directly to the runtime system.

I am going to use below application to experiment with jstat command.


App.java
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class App {

 private static class BigObject {
  int i;
  int ii;
  int iii;
  int iiii;
  int iiiii;
  int iiiiii;
  int iiiiiii;
  int iiiiiiii;
  int iiiiiiiii;
  int iiiiiiiiii;

  public BigObject() {
   i = 1;
   ii = 11;
   iii = 111;
   iiii = 1111;
   iiiii = 11111;
   iiiiii = 111111;
   iiiiiii = 1111111;
   iiiiiiii = 11111111;
   iiiiiiiii = 111111111;
   iiiiiiiiii = 1111111111;
  }

 }

 private static void printGarbageCollectorBeansInfo() {
  System.out.println("---------------------------------------------");
  List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans();

  for (GarbageCollectorMXBean bean : beans) {
   System.out.println("Name : " + bean.getName());
   System.out.println("Collection Count : " + bean.getCollectionCount());
   System.out.println("Collection Time : " + bean.getCollectionTime());

   String[] memoryPoolNames = bean.getMemoryPoolNames();
   for (String poolName : memoryPoolNames) {
    System.out.println(poolName);
   }

  }
  System.out.println("---------------------------------------------");
 }

 public static void main(String args[]) throws InterruptedException {

  RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
   String jvmName = bean.getName();
   long pid = Long.valueOf(jvmName.split("@")[0]);
         System.out.println("PID  = " + pid);
         
  int counter = 0;
  
  List<BigObject> bigObjs = new ArrayList<> ();
  

  while (true) {
   BigObject obj = new BigObject();
   
   counter++;
   
   bigObjs.add(obj);
   
   //System.out.println(counter);

   if (counter % 100000 == 0) {
    bigObjs = new ArrayList<> ();
    counter = 0;
    TimeUnit.SECONDS.sleep(30);
    printGarbageCollectorBeansInfo();
   }
  }

 }
}

Run App.java

It prints the Process Id of the application
$javac App.java
$java App
PID  = 86867

You can even get the process id of java application by executing the command  ‘jps’.

Open another terminal and experiment with jstat command.

a. class : Get Statistics on the behavior of the class loader
Syntax
jstat -class <PID>

$ jstat -class 86867
Loaded  Bytes  Unloaded  Bytes     Time  
  1483  2750.5        0     0.0       0.18

Below table summarizes the columns of output.
Column
Description
Loaded 
Number of classes loaded.
Bytes
Number of Kbytes loaded.
Unloaded 
Number of classes unloaded.
Bytes
Number of Kbytes unloaded.
Time 
Time spent performing class load and unload operations.

b. -compiler : Just-In-Time compiler statistics
Syntax
jstat -compiler <PID>

$ jstat -compiler 86867
Compiled Failed Invalid   Time   FailedType FailedMethod
     517      0       0     0.25          0     

Below table summarizes the columns of output.
Column
Description
Compiled 
Number of compilation tasks performed.
Failed 
Number of compilation tasks that failed.
Invalid
Number of compilation tasks that were invalidated.
Time 
Time spent performing compilation tasks.
FailedType 
Compile type of the last failed compilation.
FailedMethod  
Class name and method for the last failed compilation.

c. -gc : Garbage Collected heap statistics

Syntax
Jstat -gc <PID>

$ jstat -gc 86867
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT  
10752.0 10752.0 6080.9  0.0   65536.0  37294.3   175104.0     16.0    8576.0 8143.4 1152.0 974.6       2    0.009   0      0.000    0.009

Below table summarizes the columns of output.

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.

d. -gccapacity: Memory Pool Generation and Space Capacities
Syntax
jstat -gccapacity <PID>

$ jstat -gccapacity 86867
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC
 87040.0 1397760.0  87040.0 10752.0 10752.0  65536.0   175104.0  2796544.0   175104.0   175104.0      0.0 1056768.0   8576.0      0.0 1048576.0   1152.0      3     0

Below table summarizes the columns of output.

Column
Description
NGCMN
Minimum new generation capacity (KB).
NGCMX
Maximum new generation capacity (KB).
NGC
Current new generation capacity (KB).
S0C
Current survivor space 0 capacity (KB).
S1C
Current survivor space 1 capacity (KB).
EC 
Current eden space 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).
PGCMN
Minimum permanent generation capacity (KB).
PGCMX
Maximum Permanent generation capacity (KB).
PGC
Current Permanent generation capacity (KB).
PC 
Current Permanent space capacity (KB).
YGC
Number of Young generation GC Events.
FGC
Number of Full GC Events.

e. – gcutil : Summary of garbage collection statistics

Syntax
Jstat -gcutil <PID>

$ jstat -gcutil 86867
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT  
 66.82   0.00  64.62   0.02  95.21  84.65      4    0.016     0    0.000    0.016

Below table summarizes the columns of output.
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.


e. -gccause
Displays same summary as gcutil, apart from this it also includes the causes of the last garbage collection event and (if applicable) the current garbage collection event.

Syntax
Jstat -gccause <PID>

$ jstat -gccause 86867
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC                
  0.00  92.43  37.99   0.02  95.24  84.65      5    0.019     0    0.000    0.019 Allocation Failure   No GC

Below table summarizes the columns of output.
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.
LGCC 
Cause of last Garbage Collection.
GCC
Cause of current Garbage Collection.

f. -gcnew : New Generation statistics

Syntax
jstat -gcnew <PID>

$ jstat -gcnew 86867
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT 
7680.0 5632.0    0.0 5205.9  6  15 7680.0  64000.0  58188.8      5    0.019

Below table summarizes the columns of output.
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).
TT 
Tenuring (Old Generation) threshold.
MTT
Maximum tenuring threshold.
DSS
Desired survivor size (KB).
EC 
Current eden space capacity (KB).
EU 
Eden space utilization (KB).
YGC
Number of young generation GC events.
YGCT 
Young generation garbage collection time.

g. -gcnewcapacity: New Generation Space Size Statistics

Synatx
jstat –gcnewcapacity <PID>

$ jstat -gcnewcapacity 86867
  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC      YGC   FGC
   87040.0  1397760.0    78848.0 465920.0   8704.0 465920.0   1536.0  1396736.0    61440.0     7     0

Below table summarizes the columns of output.
Column
Description
NGCMN
Minimum new generation capacity (KB).
NGCMX
Maximum new generation capacity (KB).
NGC
Current new generation capacity (KB).
S0CMX
Maximum survivor space 0 capacity (KB).
S0C
Current survivor space 0 capacity (KB).
S1CMX
Maximum survivor space 1 capacity (KB).
S1C
Current survivor space 1 capacity (KB).
ECMX 
Maximum eden space capacity (KB).
EC 
Current eden space capacity (KB).
YGC
Number of young generation GC events.
FGC
Number of Full GC Events.

h. -gclod: Old and Permanent Generation Statistics

Syntax
jstat -gcold <PID>

$ jstat -gcold 86867
   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT  
  8576.0   8179.3   1152.0    975.2    175104.0      1668.1      8     0    0.000    0.026

Below table summarizes the columns of output.

Column
Description
PC 
Current permanent space capacity (KB).
PU 
Permanent space utilization (KB).
OC 
Current old space capacity (KB).
OU 
old space utilization (KB).
YGC
Number of young generation GC events.
FGC
Number of full GC events.
FGCT 
Full garbage collection time.
GCT
Total garbage collection time.

i. –gcoldcapacity: Old Generation Statistics
Syntax
jstat -gcoldcapacity <PID>
$ jstat -gcoldcapacity 86867
   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT  
   175104.0   2796544.0    175104.0    175104.0     9     0    0.000    0.027

Below table summarizes the columns of output.

Column
Description
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 garbage collection time.
GCT
Total garbage collection time.

j. -printcompilation: HotSpot Compiler Method Statistics
Syntax
jstat -printcompilation <PID>

$ jstat -printcompilation 86867
Compiled  Size  Type Method
     642     34    1 java/net/AbstractPlainSocketImpl isClosedOrPending

Below table summarizes the columns of output.
Column
Description
Compiled 
Number of compilation tasks performed by the most recently compiled method.
Size 
Number of bytes of bytecode of the most recently compiled method.
Type 
Compilation type of the most recently compiled method.
Method 
Class name and method name identifying the most recently compiled method. Class name uses "/" instead of "." as namespace separator.




Previous                                                 Next                                                 Home

No comments:

Post a Comment