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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Real-World Garbage Collection Scenarios and Solutions
  • Using Heap Dumps to Find Memory Leaks
  • Different Garbage Collectors in Java: Exploring the Options

Trending

  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • A Modern Stack for Building Scalable Systems
  • A Complete Guide to Modern AI Developer Tools
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  1. DZone
  2. Coding
  3. Languages
  4. Try to Avoid -XX:+UseGCLogFileRotation

Try to Avoid -XX:+UseGCLogFileRotation

Learn how to avoid the -XX:+UseGCLogFileRotation in this code example.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Updated May. 05, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
36.6K Views

Join the DZone community and get the full member experience.

Join For Free

Developers take advantage of the JVM argument -XX:+UseGCLogFileRotation to rotate GC log files.

Example:

"-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/home/GCEASY/gc.log -
XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M"

As shown above, the JVM will rotate the GC log file whenever its size reaches 20MB. It will generate up to five files, with extensions gc.log.0,  gc.log.1, gc.log.2, gc.log.3, and gc.log.4.

But this approach has a few challenges:

Losing Old GC Logs

Suppose you configured  -XX:NumberOfGCLogFiles=5, then over a period of time, five GC log files will be created:

  • gc.log.0 ← oldest GC Log content

  • gc.log.1

  • gc.log.2

  • gc.log.3

  • gc.log.4 ← latest GC Log content

The most recent GC log contents will be written to gc.log.4 and old GC log contents will be present in gc.log.0.

When the application starts to generate more GC logs than the configured  -XX:NumberOfGCLogFiles, in this case, five, then old GC log contents in gc.log.0 will be deleted. New GC events will be written to  gc.log.0. It means that you will end up not having all the generated GC logs. You will lose the visibility of all events.

Mixed-Up GC Logs

Suppose an application has created five GC log files, including:

  • gc.log.0

  • gc.log.1

  • gc.log.2

  • gc.log.3

  • gc.log.4

Then, let’s say you are restarting the application. Now, new GC logs will be written to gc.log.0 file and old GC log content will be present in gc.log.1, gc.log.2, gc.log.3, gc.log.4, etc.

  • gc.log.0 ← GC log file content after restart

  • gc.log.1 ← GC log file content before restart

  • gc.log.2 ← GC log file content before restart

  • gc.log.3 ← GC log file content before restart

  • gc.log.4 ← GC log file content before restart

So, your new GC log contents get mixed up with old GC logs. Thus, to mitigate this problem, you might have to move all the old GC logs to a different folder before you restart the application.

Forwarding GC Logs to a Central Location

In this approach, the current active file to which GC logs are written is marked with the extension  .current. For example, if GC events are currently written to the file gc.log.3, it would be named as: gc.log.3.current.

If you want to forward GC logs from each server to a central location, then most DevOps engineers use  rsyslog. However, this file naming convention poses a significant challenge to use rsyslog, as described in this blog.

Tooling

Now, to analyze the GC log file using the GC tools such as (GCeasy, GCViewer, etc.), you will have to upload multiple GC log files instead of just one single GC Log file.

Recommended Solution

We can suffix the GC log file with the time stamp at which the JVM was restarted, then the GC Log file locations will become unique. Then, new GC logs will not override the old GC logs. It can be achieved by suffixing %t to the GC log file name, as shown below:

"-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/home/GCEASY/gc-%t.log"


 %t suffixes timestamp to the GC log file in the format:  YYYY-MM-DD_HH-MM-SS. So, the generated GC log file name will start to look like: gc-2019-01-29_20-41-47.log.

This simple solution addresses all the shortcomings of -XX:+UseGCLogFileRotation.

garbage collection

Opinions expressed by DZone contributors are their own.

Related

  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Real-World Garbage Collection Scenarios and Solutions
  • Using Heap Dumps to Find Memory Leaks
  • Different Garbage Collectors in Java: Exploring the Options

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!