How to monitor and analyze the garbage collection in 10 ways in java




Contents of page >

1) What is monitoring and analyzing the garbage collection in java?
2) How to monitor and analyze the garbage collection in java ?
3) 10 different tools and ways that can be used to generate the garbage collection dumps and analyze it
  1. JSTACK - Java stack traces
  2. Jstat  - Java Virtual Machine Statistics Monitoring Tool
  3. JHAT - Java Heap Analysis Tool.
  4. jconsole
  5. hprof
  6. eclipse plugin
  7. JFR (Java Flight Recorder)



1) What is monitoring and analyzing the garbage collection in java?
Monitoring and analyzing garbage collection in java can be used to get  following information >
  1. Understanding the garbage collection process.
  2. Understanding how JVM is currently working.
  3. What type of garbage collection algorithms are used
  4. Analyzing Java heap dumps,
  5. Monitoring live Java applications,
  6. Analyze profiling data,
  7. Detecting Memory leak for classes and arrays,
  8. Detecting Thread deadlock,
  9. Detecting abnormal thread termination,
  10. Detecting outOfMemoryError problems,
  11. Finding System and process CPU utilization thresholds,
  12. Find Heap usage thresholds.
  13. Find time taken in Garbage collection and Finalizer queue length.


2) How to monitor and analyze the garbage collection in java ?
We can use different tools to generate the garbage collection information and then analyze it.

3) 10 different tools and ways that can be used to generate the garbage collection dumps and analyze it >


VisualVM is most popular way to generate Thread Dump and is most widely used by developers. It’s important to understand usage of VisualVM for in depth knowledge of VisualVM. I’ll recommend every developer must understand this topic to become master in multi threading.
It helps us in analyzing threads performance, thread states, CPU consumed by threads, garbage collection and much more.

For more details please read :

VisualVM - Thread dumps - Generating and analyzing Thread Dumps using VisualVM - step by step detail to setup VisualVM with screenshots



  1. JSTACK - Java stack traces
jstack is very easy way to generate Thread dump and is widely used by developers. For creating Thread dumps we need not to download any jar or any extra software.

For more details please read :

JSTACK - Thread dumps - Generating and analyzing Thread Dumps using - step by step detail to setup JSTACK with screenshots




It can used be handy in monitoring and analyzing garbage collection in java.

Understanding Garbage Collection (GC) in java using -verbose:gc VM argument -
  • [GC 325407K->83000K(776768K), 0.2300771 secs]
    • GC - GC indicates minor Garbage Collection (i.e. in young generation).
    • 325407K - The combined size of live objects before gc(garbage collection).
    • 83000K - The combined size of live objects after gc(garbage collection).
    • (776768K) - the total available space, not counting the space in the permanent generation, which is the total heap minus one of the survivor spaces.
    • 0.2300771 secs - time it took for gc(garbage collection) to occur.

  • [Full GC 325407K->83000K(776768K), 0.2300771 secs]
    • Full GC - Full GC Indicates major garbage collection (i.e. in tenured generation).

For more details please read : How to use -verbose:gc VM argument


  1. Jstat  - Java Virtual Machine Statistics Monitoring Tool

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.

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.

For preliminary steps please read : Jstat program used

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.


  1. JHAT - Java Heap Analysis Tool.
The jhat command parses a Java heap dump file and starts a web server. The jhat command lets you to browse heap dumps with your favorite web browser. The jhat command supports pre designed queries such as show all instances of a known class MyClass, and Object Query Language (OQL). OQL is similar to SQL, except for querying heap dumps. Help on OQL is available from the OQL help page shown by the jhat command. With the default port, OQL help is available at http://localhost:7000/oqlhelp/

Jhat is used for analyzing the garbage collection dump in java.

For preliminary steps please read : JHAT program used

Now we will type jhat command to analyze the heap dump >
jhat E:\heapDump.jmap

C:\>jhat E:\heapDump.jmap
Reading from E:\heapDump.jmap...
Dump file created Mon Nov 07 23:59:19 IST 2016
Snapshot read, resolving...
Resolving 241865 objects...
Chasing references, expect 48 dots................................................
Eliminating duplicate references................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

By default, it will start http server on port 7000.
Then we will go to http://localhost:7000/



  1. jconsole - jconsole option can be used to obtain a heap dump via HotSpotDiagnosticMXBean at runtime;

  1. hprof - HPROF can be also used to analyze the garbage collection because it can show you CPU usage, heap allocation statistics, and monitor contention profiles. In addition, it can also report complete heap dumps and states of all the monitors and threads in the Java virtual machine.

  1. eclipse plugin  - You may also use eclipse plugin for generating, analyzing and monitoring garbage collection and heap memory in java.

  1. HPjmeter - HPjmeter can diagnose performance problems of Java applications. It can Improve garbage collection performance, Analyze Java heap dumps, Monitor live Java applications and analyze profiling data, detect Memory leak for classes and arrays, detect Thread deadlock, detect abnormal thread termination, detect out of memory error, find System and process CPU utilization thresholds, find Heap usage thresholds, time taken in Garbage collection and Finalizer queue length.

  1. JFR (Java Flight Recorder) can be used for detecting memory leak.

Summary -

So in this core java tutorial we learned how to analyze and monitor 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



 

Monitor, analyze garbage collection and fix MEMORY LEAK >

>Detecting and fixing memory leak in java


TOOLS for Monitoring garbage collection >

>How to use -verbose:gc VM argument

>How to use Jstat for monitoring the garbage collection in java

>What is vmid - and how to find it using JPS

>How to use JHAT to analyze the heat dump - Using jmap as well

>How to generate Heap dump when OutOfMemoryError is thrown in java?


eEdit
Must read for you :