Overview

11 Analyzing potential JVM issues with GC logs

Garbage Collection logs are positioned as a practical, approachable lens into the JVM’s memory behavior—turning what first looks like inscrutable output into an everyday troubleshooting ally. The chapter opens with a narrative reminder that neglecting GC logs can hide the true causes of latency, CPU spikes, and thread churn, while a quick, focused read of the logs (often aided by an AI assistant) can reveal misconfigured heaps, excessive stop-the-world pauses, and poorly tuned collectors. The overarching goal is to build intuition: understand what the JVM is doing, when, and why—so performance anomalies become explainable patterns rather than mysteries.

From there, the text walks through enabling and structuring GC logging (-Xlog:gc*), capturing it in the console for learning and in files for real diagnostics, and formatting entries with time, uptime, level, and tags to make analysis efficient. It emphasizes operational hygiene: write logs to files, add timestamps, and use rotation (filecount/filesize) to avoid loss or bloat; choose log levels wisely (error/warning in production, info/debug/trace for deeper dives elsewhere). A guided tour explains how to read initialization lines (JVM version, GC type, heap region sizes, worker counts) and event lines (event type, phases, and durations), how object movement across Eden/Survivor/Old appears in logs, and what humongous object patterns imply. For scale, the chapter encourages using specialized tools and AI to sift large volumes, highlight anomalies, and surface likely root causes faster.

The heart of the analysis covers recurring problem patterns and concrete remedies. For performance lags, look for rising GC pause times, increasing frequency, and prolonged stop-the-world events; mitigate by right-sizing the heap (Xms/Xmx), reducing allocation churn, adjusting young/old dynamics, or choosing a more suitable algorithm (e.g., ZGC/Shenandoah for low latency). To spot memory leaks, watch for steadily increasing post-GC usage and Full GCs that reclaim little; confirm with heap dumps and roll back recent changes if needed. To distinguish insufficient memory from leaks, note that memory is reclaimed but GC occurs too often—pointing to under-provisioning rather than retention. Finally, the chapter shows how to tune G1’s parallelism (ParallelGCThreads, ConcGCThreads) to avoid underutilization or CPU contention, urging an iterative, measured approach and production discipline: keep logs lean in live systems, reproduce issues in staging, and let data—not guesswork—drive tuning.

Enabling the GC logs in IntelliJ IDEA running configuration to display the GC logs in the execution console.
Adding a VM property to store GC logs for the application's execution in a specified file.
Properly formatted GC logs are stored in the specified file, as defined by the VM parameter.
Using tools such as GCeasy or AI chat assistants to help you troubleshoot the GC logs.
A comparison between the memory consumption graph of an application with normal memory usage and one affected by a memory leak.

Summary

  • Garbage Collector logs are crucial for troubleshooting JVM memory management issues. They provide insights into memory allocation, garbage collection pauses, and heap utilization.
  • Enabling GC logs is the first step in analyzing JVM memory behavior. The -Xlog:gc* VM option enables GC logging, helping developers track GC activity in real-time or store logs for later analysis.
  • Understanding GC log structure is essential. Logs include details on GC events, pause times, heap size changes, and memory allocation patterns.
  • GC logs help diagnose excessive GC pauses. Long GC pause times (typically above 50ms) can indicate performance issues, while frequent Full GC events suggest memory pressure.
  • Heap memory organization impacts GC performance. Objects move through Eden, Survivor, and Old Generation memory regions. Inefficient memory usage can cause frequent GC interruptions.
  • Frequent Full GC events may indicate insufficient memory or a memory leak. Logs showing frequent Full GC events with minimal memory reclamation suggest a leak, while effective but frequent Full GCs indicate an under-provisioned heap.
  • Storing GC logs in files is recommended for deeper analysis. Use -Xlog:gc*:file=gc.log:time,uptime,level,tags to redirect logs to a file for structured storage and easier troubleshooting.
  • Log rotation prevents excessive storage use. Configuring file count and file size limits ensures logs do not consume excessive disk space.
  • Choosing the right logging level improves troubleshooting efficiency. Error logs capture critical failures, while Info and Debug levels provide additional insights into memory management.
  • Tuning GC parallelism can reduce pause times. Adjusting -XX:ParallelGCThreads and -XX:ConcGCThreads optimizes CPU usage for better performance.
  • AI tools and third-party services can simplify GC log analysis. Uploading logs to tools like GCEasy or using AI assistants can speed up troubleshooting.

FAQ

How do I enable GC logging for my Java application?Use the JVM flag -Xlog:gc* when starting your app. For example: java -Xlog:gc* -jar app.jar. In an IDE, add -Xlog:gc* to the run configuration VM options.
How can I write GC logs to a file and include useful metadata?Redirect logs with -Xlog:gc*:file=gc.log:time,uptime,level,tags. This adds timestamps, JVM uptime, log level, and tags to each entry. Ensure the process has write permissions to the target directory.
How do I rotate GC log files to avoid huge single logs?Use filecount and filesize in the -Xlog option. Example: -Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=10M. The JVM creates rolling files up to filecount and overwrites the oldest when full.
Which GC log levels should I use in production vs. debugging?- Production: warning or error to minimize overhead. - Default/monitoring: info is usually enough. - Deep troubleshooting: debug or trace in non-production to gather detailed data. Levels are cumulative (warning includes error, etc.).
What do the initialization GC log lines tell me, and why are they important?They show GC type (e.g., G1), JVM version, CPU count, memory, heap region size, initial/max heap, and GC worker threads. These set expectations for behavior and help correlate performance with environment and configuration.
How do I read a GC event entry, and what should I focus on?Key points: - Event type (Young, Mixed, Full, System.gc, Allocation Failure). - Pause duration and per-phase timings (e.g., Evacuate Collection Set). - Workers used. - Heap/region changes before→after (Eden, Survivor, Old, humongous). Outliers in duration or frequent STW pauses are red flags.
What GC pause times are concerning, and what can I do about them?Pauses under ~50 ms are typically fine; sustained pauses above this or spikes growing over time warrant attention. Actions: adjust -Xms/-Xmx, tune young/old sizing, reduce allocation pressure, consider low-latency collectors (ZGC, Shenandoah), or refine application object lifecycles.
How do GC logs help distinguish a memory leak from insufficient heap size?- Memory leak: after-GC heap trends upward; Full GC reclaims little; pauses may worsen; eventually OOME. - Insufficient heap: frequent GCs reclaim meaningful memory (sawtooth pattern), but workload outgrows provisioned memory; increasing heap or scaling out helps.
How can I tune GC parallelism, and what symptoms indicate misconfiguration?Use -XX:ParallelGCThreads for STW phases and -XX:ConcGCThreads for background phases. Symptoms: - Too few threads: long pauses, “Using N of M workers” shows underuse. - Too many threads: high CPU contention and longer pauses. Tune iteratively and observe effects.
How can tools or AI assist with large GC logs?Store logs to files and upload to GC analysis tools (e.g., GCeasy) for summaries and visualizations. Use AI assistants to highlight patterns, anomalies, and likely root causes, and to suggest JVM flags or next diagnostic steps. Keep logs separate from app output for easier sharing and review.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Troubleshooting Java, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Troubleshooting Java, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Troubleshooting Java, Second Edition ebook for free