DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. What Is a Garbage Collection Log and How Can You Enable and Analyze It?

What Is a Garbage Collection Log and How Can You Enable and Analyze It?

In this article, we cover the basics of garbage collection, and how to analyze logs retrieved from Java-based applications.

Ram Lakshmanan user avatar by
Ram Lakshmanan
CORE ·
Nov. 07, 17 · Opinion
Like (4)
Save
Tweet
Share
52.11K Views

Join the DZone community and get the full member experience.

Join For Free

Objects are created in the memory to service incoming requests. Once requests are serviced, newly created objects will become useless (i.e. garbage). This garbage must be evicted from the memory so that there is enough room created in the memory to service the new incoming requests. If there isn’t sufficient memory, the application can experience poor response times, OutOfMemoryError, and fatal crashes.

In Java, Android, C# etc., garbage collection is automatic, whereas in the several predecessor programming languages (C, C++) the programmer must write code explicitly to release the objects after they are used. So, it’s a major convenience for Java, Android, and C# application developers. But this automatic garbage collection is not free, it comes with a price. Automatic Garbage Collection can have a profound impact on:

  1. Application Response Time
  2. CPU
  3. Memory

Application Response Time

To garbage collect objects automatically, the entire application has to be paused intermittently to mark the objects that are in use and sweep away the objects that are not used. During this pause period, all customer transactions which are in motion in the application will be stalled (i.e. frozen). Depending on the type of GC algorithm and memory settings that you configure, pause times can run from a few milliseconds to a few seconds to a few minutes. Thus, Garbage Collection can affect your application SLA (Service Level Agreement) significantly.

CPU

Garbage collection consumes a lot of CPU cycles. Each application will have thousands/millions of objects sitting in memory. Each object in memory should be investigated periodically to see whether they are in use. If it’s in use, who is referencing it? Are those references still active? If they are not in use, they should be evicted from memory. All these investigations and computation requires a considerable amount of CPU power.

Memory

Of course, poor GC configuration can lead to high memory consumption and vice versa. Most applications saturate memory first before saturating other resources (CPU, network bandwidth, storage). Most applications upgrade their EC2 instance size to get additional memory rather than getting additional CPUs or network bandwidth.

Thus, to have top-notch SLAs and reduce the bill from your cloud hosting provider, your application's garbage collection has to function effectively. In order to study and optimize garbage collection's impact on an application’s performance, one has to enable Garbage Collection Logging. Besides that, garbage collection logs can be used to troubleshoot memory-related problems in the application.

Enabling GC Logs

GC Logging can be enabled by passing in the below-mentioned system properties during application startup.

Until Java 8:

Below is the system property that is supported by all versions of Java until JDK 8.

-XX:+PrintGCDetails -Xloggc:<gc-log-file-path>

Example:

-XX:+PrintGCDetails -Xloggc:/opt/tmp/myapp-gc.log

From Java 9:

Below is the system property that is supported by all versions of Java starting with JDK 9.

-Xlog:gc*:file=<gc-log-file-path>

Example:

-Xlog:gc*:file=/opt/tmp/myapp-gc.log

How to Analyze GC Logs

Here is a sample GC log generated when the above system properties were passed:

What is GC

A GC log has rich information, however, understanding GC logs is not easy. There isn’t sufficient documentation to explain GC log format. On top of that, GC log format is not standardized. It varies by JVM vendor (Oracle, IBM, HP, Azul, …), Java version (1.4, 5, 6, 7, 8, 9), GC algorithm (Serial, Parallel, CMS, G1, Shenandoah), GC system properties that you pass (-XX:+PrintGC, -XX:+PrintGCDetails, -XX:+PrintGCDateStamps, -XX:+PrintHeapAtGC …). Based on this permutation and combination, there are easily 60+ different GC log formats.

Thus, to analyze GC logs, it’s highly recommended to use GC log analysis tools such as GCeasy, HPJmeter. These tools parse GC logs and generate great graphical visualizations of data, reports Key Performance Indicators and several other useful metrics.

Here is a sample GC log analysis report generated by the GCeasy tool.

garbage collection Garbage (computer science) application Analyze (imaging software) Memory (storage engine) Java (programming language) Object (computer science) Log analysis Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT Prompts for Agile Practitioners
  • Top 10 Secure Coding Practices Every Developer Should Know
  • Load Balancing Pattern
  • Top 5 Node.js REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: