executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
performance-memory-leak-detection-gc-tuningJVM Performance: Memory Leak Detection and GC Tuning
Independent GitHub snapshot · Automated classification · Original documentation governs
This skill provides comprehensive guidance for diagnosing memory leaks and tuning garbage collection (GC) in Java applications. It covers key techniques and tools available in JDK 17 and newer for optimizing memory management and ensuring system stability.
Gather Diagnostic Data: Collect heap dumps and GC logs before making changes. This data is critical for understanding the application's memory usage patterns.
# Identify your Java process
jps -l
# Capture thread dump
jcmd <pid> Thread.print > thread_dump.txt
# Capture heap dump for analysis
jcmd <pid> GC.heap_dump /tmp/heap_dump.hprof
# Start JFR recording for profiling while the application is running
jcmd <pid> JFR.start name=profile duration=5m filename=/tmp/profile.jfr
Checkpoint: Verify all files are created and contain data.
Analyze Heap Dump with MAT: After capturing the heap dump, load it into MAT for analysis. Use the Leak Suspects report to identify potential memory leaks.
// Load heap dump into Eclipse MAT
``
// Generate Leak Suspects report
// Check if MAT reveals any dominant objects or suspicious patterns that might indicate memory leaks.
Checkpoint: Confirm the leak suspect report provides actionable insights.
Tuning the Garbage Collector: Choose the correct GC based on performance requirements. For applications needing low latency, configure ZGC or Shenandoah; for balance, use G1GC.
// Best tuning practices include:
-XX:+UseG1GC // For balanced performance
-XX:+UseZGC // For low-latency requirements
-Xms4g -Xmx4g // Set fixed heap sizes
Checkpoint: Validate no existing memory leaks exist before proceeding to tuning.
Monitor and Validate Changes: After tuning the GC settings, monitor the application's performance through GC metrics, latency, and responsiveness.
# Monitor GC logs with specification
jcmd <pid> VM.flags | grep GC
Checkpoint: Confirm that application performance meets expected metrics under typical load conditions. Track improvements or regressions closely.
Document Findings and Recommendations: After analysis, compile findings into a report detailing any memory leaks discovered, GC tuning strategies employed, and their respective impacts on application performance. Checkpoint: Validate adherence to monitoring protocols and successful remediation actions.
jcmd to extract historical performance metrics.
jcmd <pid> VM.gc
# Start JFR with the profiling settings
jcmd <pid> JFR.start name=profile duration=10m filename=/tmp/profile.jfr
Checkpoint: Ensure essential metrics are being recorded accurately during peak operations.# Open HPROF file in MAT for inspection