How to Perform Java Garbage Collection Analysis? (3 Easy Steps)
Learning how to do Garbage Collection analysis is a valuable lifetime skill to acquire. Garbage Collection performance has a significant impact on your overall application’s performance. Besides that, studying Garbage Collection performance helps to diagnose memory problems, plan capacity efficiently, reduce computing costs, and offers several other benefits. Let’s learn how to acquire this valuable skill.
GC Analysis Steps
You can do GC analysis by following 3 simple steps:
Let’s dive into each step.
1. Capture GC Log
The best way to study GC performance is by using the GC Log. Simply pass the following arguments to the JVM, and it will generate a GC log at the specified file location.
Java 9 and above version
-Xlog:gc*:file=<gc-log-file-path>
Example:
-Xlog:gc*:file=/opt/tmp/myapp-gc.log
Java 8 and below version
-XX:+PrintGCDetails -Xloggc:<gc-log-file-path>
Example:
-XX:+PrintGCDetails -Xloggc:/opt/tmp/myapp-gc.log
Note 1: Enabling GC logging on your application adds almost zero overhead. Here is a white paper that showcases the zero overhead added by enabling GC Logs. Here is a good post that highlights the best practices to follow when enabling GC Logging in your application. Note 2: While APM (Application Performance Monitoring) tools provide GC performance metrics, they are not comprehensive enough. They don’t give as much detail as the GC log. The GC log rich set of information, such as timestamps of GC events, types of events (Young GC, Full GC, Mixed GC), duration of events, sizes of internal JVM memory regions (Young Gen, Old Gen, Metaspace) before and after the event, time spent in the JVM and Kernel during GC events, number of GC threads used, duration each GC thread took to complete, string deduplication details… Thus it’s recommended to use GC log for analysis.
2. Use GC Log analysis Tools
GC logs are text files. You can read them in a text editor. However, reading them manually in a text editor can become a tedious & cumbersome process. Also GC log formats are not consistent. They vary by the JVM vendor (OpenJDK, IBM, HP, Azul, …), Java version (5, 6, 7, 8, 9, 10, 11, 22, …), and GC algorithm type (Serial, Parallel, CMS, G1, ZGC, …). Therefore, you want to use GC log analysis tools like GCeasy, IBM GC Visualizer, HP JMeter, Garbage Cat to analyze your GC logs.
Note: If you are looking to automate GC log analysis for advanced monitoring capabilities, REST API is also available.
3. Study the Key GC Metrics
Once you upload the GC log, the tools like GCeasy will show the memory/GC problems faced by your application. It will report issues such as: memory leaks, long GC pauses, back-to-back GC events, poor throughput, saturation of internal memory regions… Additionally, the tool will also provide appropriate recommendations to improve your GC performance.
If you are interested in tuning the GC performance, here is a good white paper: ‘9 tips to improve your GC performance‘.
Conclusion
Effective GC analysis not only improves your Java application’s performance but also makes you a superhero in your organization. Happy GC tuning.