How do you use jstat with a simple code example?

jstat is a command-line utility included in the Java Development Kit (JDK) that provides information about the Java Virtual Machine's performance statistics. It can be used to monitor various aspects of the JVM, such as garbage collection, memory usage, and class loading.

Simple Example of Using jstat

To use jstat, you can follow these simple steps:

  1. Compile a simple Java program:
public class SampleApp { public static void main(String[] args) { for (int i = 0; i < 1000000; i++) { String temp = "Sample String " + i; } } }
  1. Run the program:
javac SampleApp.java java SampleApp
  1. Open another terminal and use jstat to monitor the JVM performance:
jstat -gc

Replace <pid> with the process ID of the running Java application, which you can find using the command jps.


jstat JVM monitoring performance statistics Java Development Kit garbage collection memory usage