Mastering JVM Performance: 6 Essential Parameters and GC Tuning Tips
Learn how to optimize Java applications by configuring six critical JVM parameters—including heap size, Metaspace limits, GC algorithm selection, logging, heap dumps, thread stack size, and network timeouts—while understanding their impact on performance, memory usage, and troubleshooting common out‑of‑memory issues.
-Xmx and -XX:MaxMetaspaceSize
The -Xmx option sets the maximum Java heap size (e.g., -Xmx2g). Aligning the initial heap size -Xms with -Xmx can reduce heap resizing overhead and improve performance.
Metaspace stores class metadata. By default it can grow until the process exhausts physical RAM. Use -XX:MaxMetaspaceSize=256m (or another appropriate limit) to cap the memory used for metadata and prevent uncontrolled growth.
GC Algorithms
OpenJDK ships with seven garbage‑collection algorithms. The default algorithm depends on the Java version: Java 8 uses Parallel GC, while Java 9 and later use G1 GC. Selecting the appropriate collector is critical for latency and throughput.
Serial GC – -XX:+UseSerialGC Parallel GC – -XX:+UseParallelGC Concurrent Mark‑Sweep (CMS) – -XX:+UseConcMarkSweepGC G1 GC – -XX:+UseG1GC Shenandoah GC – -XX:+UseShenandoahGC Z GC – -XX:+UseZGC Epsilon GC – -XX:+UseEpsilonGC For Java 11+ the low‑pause Z GC ( -XX:+UseZGC) often yields the best throughput and latency characteristics.
Enabling GC Logging
GC logs provide details about collection events, reclaimed memory, and pause times. Add the following JVM options to enable logging:
JDK 1‑8:
-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/path/to/gc.logJDK 9 and later: -Xlog:gc*:file=/path/to/gc.log GC throughput is defined as application‑time / (application‑time + GC‑time) . A throughput of 98 % means only 2 % of total runtime is spent in garbage collection.
Heap Dump on OutOfMemoryError
When an OutOfMemoryError occurs, automatically generate a heap dump for post‑mortem analysis:
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/dump.hprofTools such as HeapHero or Eclipse MAT can inspect the .hprof file to locate memory leaks or oversized objects.
Thread Stack Size (-Xss)
Each Java thread has its own stack, whose size is controlled by -Xss. A larger stack (e.g., -Xss2m) increases per‑thread memory consumption, while a too‑small stack can cause StackOverflowError.
Example calculation: 500 threads with -Xss2m consume roughly 1000 MB of stack memory; the same number of threads with -Xss256k consume about 125 MB, saving ~875 MB.
Network Timeout Parameters
To avoid hanging threads when remote services are slow, set global timeout properties for all java.net.URLConnection based protocols:
-Dsun.net.client.defaultConnectTimeout=2000 -Dsun.net.client.defaultReadTimeout=2000Values are in milliseconds; the default -1 disables the timeout.
Illustrative Heap‑Usage Graphs
Healthy JVM heap usage shows periodic full GC events (red triangles) that temporarily reduce memory usage before it rises again. A problematic JVM exhibits repeated full GCs without a corresponding memory drop, indicating a possible memory leak.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
