Saturday, 19 March 2016

How to check JVM statistics using JSTAT ?


The jstat (Java Virtual Machine Statistics Monitoring Tool) tool displays performance statistics for JVM.
The target JVM is identified by its virtual machine identifier, or vmid option.

1. Get the process ID of virtual machine

a. Get Process ID on Linux machine
If working on Linux machine, there is no need of writing Java program to know virtual machine ID (vmid) or process ID.
Just use this command :
$ ps -aef | grep java

It will give you the all java processes with their process ID.
jonas 11514 1 0 07:31 pts/2 00:01:26 /opt/jdksun64/1.6.0_24/bin/java -Xms512m -Xmx512m -XX:MaxPermSize=256m -XX:PermSize=256m 
.... 
-cp /opt/application/myapp/G03R05C03/jonas5201/00/conf:/opt/jonas/5.2.0.1/lib/bootstrap/felix-launcher.jar:/opt/jonas/5.2.0.1/lib/bootstrap/jonas-commands.jar/opt/jonas/5.2.0.1/lib/bootstrap/jonas-version.jar org.ow2.jonas.commands.admin.ClientAdmin 
-start

Here, the process ID is 11514 for the running java application.


b. Get Process ID using Java code 
In your startup java file, add the code to get the process Id (Virtual machine ID)
If you are using web application, add code in the startup listener java file.
import java.lang.management.ManagementFactory;
String pname = ManagementFactory.getRuntimeMXBean().getName();
String pid = pname;
int i = pname.indexOf("@");
if (i!=-1) {
   pid = pname.substring(0,i);
}
System.out.println("process name = " + pname + " | process id = " + pid);
System.out.println("Copy the process ID and use jstat command");
System.out.println("Example: jstat -gc "+ pid +" 500");


Start the application and check logs to know the process ID (VM ID)
Every time you start the application, process ID will be different each time.


2. Run jstat command

Set the path of JDK / bin - OR - go to JDK bin directory and run the JSTAT command
jstat -gc [process-id] [sampling-interval--in-milliseconds]

Example
jstat -gc 2452 500

It will show the JVM statistics, that can be used for analysis.
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
17472.0 17472.0 0.0 0.0 139776.0 4164.4 349568.0 69220.0 262144.0 82708.3 12 0.921 24 16.035 16.956



-gc Option 

Garbage-collected heap statistics 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.



Troubleshooting

Problem 1. Getting error on windows
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.Exception: Could not open PerfMemory
at sun.misc.Perf.attach(Native Method)
at sun.misc.Perf.attachImpl(Perf.java:253)
at sun.misc.Perf.attach(Perf.java:183)
at sun.management.ConnectorAddressLink.importFrom(ConnectorAddressLink.java:66)

If this problem is due to an ACL problem on your Windows system.
Create a new temporary directory and just update the user variables TMP and TEMP with this temporary directory in your environment.
(Using Control Panel : System : Advanced : Environment Variables)

Restart the command prompt and try jstat command again.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.