Optimizing JVM performance with 'jstat'

Optimizing JVM performance with 'jstat'

What could you do if you realise no tools been instrumented for Jvm performance diagnosis and analysis !!

One of the solution is , 'jstat' - simple CLI based tool bundled along with jdk.

It's extremely useful in environments not managed, designed by you.

jstat provide various options for jvm/gc insights.


How to launch jstat:

Simply go to command line and run jstat commands.

Yes, That's it !!


Key usage of jstat:

  1. Real time GC stats
  2. Memory utilisation
  3. Identify GC Cause
  4. Class loading
  5. Compilation stats


Let's have a look at them one by one.


1. Real time GC monitoring

Help gain insights into young and full GC events, to assess if the application is spending too much time in garbage collection, which can indicate potential memory pressure.

How to check gc stats using jstat:

jstat -gc -t <pid> <time-interval(in ms)> <sample-count>

Example: jstat -gc -t 80594 1000 5

The complete output , stats capapcity/ size and utilisation of :

  • Eden space
  • Survivor areas both S0 and S1
  • Old generation
  • Meta space

And provides old /young GC event and time it took for those events.

Article content
jstat GC stats

GC stats only: Last columns indicate GC stats:

Article content
Indicating only GC event stats

For sake of simplicity which is much needed here, consider

suffix 'C' stands for capacity i.e. size and 'U' stands for Utilisation both metrics in KB

Different metrics can be interpreted as below:

  • S0C: Size of Survivor Space 0 (in KB).
  • S1C: Size of Survivor Space 1 (in KB).
  • S0U: Utilized space in Survivor Space 0 (in KB).
  • S1U: Utilized space in Survivor Space 1 (in KB).
  • EC: Size of Eden Space (in KB).
  • EU: Utilized space in Eden Space (in KB).
  • OC: Size of Old Generation Space (in KB).
  • OU: Utilized space in Old Generation Space (in KB).
  • MC: Size of Metaspace (in KB).
  • MU: Utilized space in Metaspace (in KB).
  • CCSC: Size of Compressed Class Space (in KB).
  • CCSU: Utilized space in Compressed Class Space (in KB).
  • YGC: Number of Young Generation Garbage Collection events.
  • YGCT: Time spent in Young Generation Garbage Collection (in seconds).
  • FGC: Number of Full Garbage Collection events.
  • FGCT: Time spent in Full Garbage Collection (in seconds).
  • GCT: Total time spent on garbage collection (in seconds).


2. JVM Memory utilisation:

How to check memory utilisation stats using jstat.

Basically same command as for gc stats only you need to focus on initial columns

jstat -gc -t <pid> <time-interval(in ms)> <sample-count>

Example: jstat -gc -t 80594 1000 5

The complete output , stats capapcity/ size and utilisation of :

  • Eden space
  • Survivor areas both S0 and S1
  • Old generation
  • Meta space


Article content
Memory (heap) utilisation stats


3. Identify GC Cause:

jstat can also indicate gc cause for each event.

How to check this:

jstat -gccause -t <pid> <time-interval(in ms)> <sample-count>

Example: jstat -gccause -t 80594 1000 5

Article content
Stats indicating GC cause

4. Class Loading:

Stats for the number of classes loaded and unloaded, help understand how dynamic class loading impacts memory usage.

How to check class loading stats

jstat -class <pid> <time-interval(in ms)> <sample-count>

Example: jstat -class 80594 100 10

Article content

Where:

  • Loaded: Number of classes currently loaded.
  • Bytes: Total memory used by loaded classes (in kilobytes).
  • Unloaded: Number of classes unloaded since the JVM started.
  • UBytes: Total memory freed by unloading classes (in kilobytes).
  • Time: Total time spent on class loading and unloading (in seconds).


5. Compilation stats

Statistics related to the Just-In-Time compiler's behavior, including compilation counts and times.

How to check JIT Compilation stats

jstat -compiler <pid> <time-interval(in ms)> <sample-count>

Example: jstat -compiler 80594 100 10

Article content
JIT Compilation stats


Pros:

Advantages of using jstats:

  1. Real time monitoring
  2. No need for any configuration
  3. Lightweight , Minimal overhead
  4. Flexible , provides various options to diagnose


Limitations :

Things to keep in mind while relying on jstat:

  1. Historical data is missing , only provide real time data
  2. Manual analysis is required as with any cli tool
  3. Remote analysis not feasible , you have to login to server
  4. Lack of granularity
  5. Provides aggregate data unlike gc logs which can help you with individual gc event details


Conclusion:

It's a powerful and handy tool. Shall be use while no jvm tool is configured and instrumented for diagnosis and analysis.


To view or add a comment, sign in

More articles by Prateek Jain

Others also viewed

Explore content categories