GC Tuning Flags and JVisualVM Profiling
Set heap size flags, enable GC logging, and use JVisualVM to profile heap usage in a live app.
GC Tuning Flags and JVisualVM Profiling is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Tune GC?
Default GC settings work well for many apps, but high-throughput or low-latency workloads may need tuning. Measure first — never tune blindly.
Heap Size Flags
Set initial (-Xms) and maximum (-Xmx) heap. In production, set them equal to prevent dynamic resizing pauses. Start with 60-70% of available RAM.
// Example production flags:
java -Xms2g -Xmx2g -XX:+UseG1GC MyApp
// Avoid:
java -Xmx8g MyApp // initial = 256m, max = 8g → resizing pausesYoung Generation Size
Increase Young Gen for allocation-heavy apps to reduce Minor GC frequency. Use -Xmn or -XX:NewRatio (Old:Young ratio, default 2).
// Set Young Gen to 1 GB:
java -Xmx4g -Xmn1g MyApp
// Or via ratio:
java -Xmx4g -XX:NewRatio=3 MyApp // 3GB old, 1GB youngG1 Pause Target
Set -XX:MaxGCPauseMillis to tell G1 your desired max pause. G1 tunes its collection set to meet the target — but it's a goal, not a hard guarantee.
java -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -Xmx8g MyApp
// G1 will try to keep pauses under 100 msGC Thread Count
Control parallelism with -XX:ParallelGCThreads (stop-the-world threads) and -XX:ConcGCThreads (concurrent threads for G1/ZGC).
// On an 8-core machine:
java -XX:+UseG1GC -XX:ParallelGCThreads=8 -XX:ConcGCThreads=2 MyAppEnabling GC Logging (JVM 9+)
Use the Xlog framework to capture GC events. Analyze logs offline to spot long pauses, allocation storms, and promotion failures.
java -Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=20m MyAppHumongous Object Warning
Objects larger than 50% of a G1 region are "humongous" and bypass the Young Gen, going directly to Old Gen. This can cause fragmentation. Keep large objects small or increase region size.
// Increase G1 region size (range: 1 MB to 32 MB, must be power of 2):
java -XX:+UseG1GC -XX:G1HeapRegionSize=16m -Xmx16g MyAppJVisualVM Overview
JVisualVM (bundled with JDK 8, downloadable for newer JDKs) provides a GUI to monitor heap, GC, CPU, and threads of local and remote JVM processes.
Connecting to a Remote JVM
Enable JMX on the server to connect JVisualVM remotely. Add these flags to the target JVM startup.
// Remote JVM flags:
java -Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=9090 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
MyAppHeap Dump from JVisualVM
In JVisualVM, right-click the process → "Heap Dump". The dump shows live objects, retained sizes, and references. Use the "OQL Console" to query the dump with SQL-like syntax.
CPU Profiling with JVisualVM
Switch to the Profiler tab, click "CPU". JVisualVM instruments methods and shows hot call stacks. Stop the profiler to see which methods consumed the most time.
Java Flight Recorder vs JVisualVM
JFR has lower overhead (~1-2%) and is suitable for production. JVisualVM with profiling has higher overhead and is better for development. Use JFR in production, JVisualVM for dev debugging.
Quick Check
What flag sets the target maximum GC pause for G1?
Recap
Set -Xms == -Xmx in production. Tune -XX:MaxGCPauseMillis for G1. Enable GC logging with Xlog. Use JVisualVM for dev debugging and JFR for production profiling.
Frequently asked questions
Is the “GC Tuning Flags and JVisualVM Profiling” lesson free?
Yes — the full text of “GC Tuning Flags and JVisualVM Profiling” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “GC Tuning Flags and JVisualVM Profiling”?
Set heap size flags, enable GC logging, and use JVisualVM to profile heap usage in a live app. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “GC Tuning Flags and JVisualVM Profiling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- JVM Heap Regions and Object Lifecycle
- GC Algorithms: Serial, G1, ZGC, Shenandoah
- Detecting and Fixing Memory Leaks
- GC Tuning Flags and JVisualVM Profiling