Operations 7 min read

Master JVM Tuning: Practical Guide to Setting Memory and GC Parameters

This article walks through essential JVM memory and garbage‑collection settings, explaining each flag—such as -Xms, -Xmx, -XX:NewSize, and GC options—through a concrete example on an 8 GB server, and provides a complete command line configuration for optimal performance and OOM diagnostics.

Java Captain
Java Captain
Java Captain
Master JVM Tuning: Practical Guide to Setting Memory and GC Parameters

JVM Parameter Settings Introduction

JVM has many memory options, but in practice the most important are the heap size and the sizes of the young and old generations. Below is a simple example of JVM startup parameters.

java -server
-Xms3g -Xmx3g
-XX:NewSize=1g
-XX:MetaspaceSize=128m
-XX:NewRatio=3
-XX:SurvivorRatio=8
-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump.log -jar start.jar

-Xms and -Xmx set the initial and maximum heap size. Setting them to the same value avoids heap size oscillation after garbage collection.

-XX:NewSize=1g sets the young generation to 1 GB, typically about one‑third of the total heap.

-XX:MetaspaceSize=128m defines the metaspace size; increase it if many classes are loaded.

-XX:NewRatio=3 makes the young‑to‑old generation ratio 1:3, so the young generation occupies ¼ of the heap.

-XX:SurvivorRatio=8 sets the Eden to Survivor ratio to 8:1, resulting in Eden:SurvivorTo:SurvivorFrom = 8:1:1.

-XX:+UseParNewGC and -XX:+UseConcMarkSweepGC select ParNew for the young generation and CMS for the old generation.

-XX:HeapDumpOnOutOfMemoryError and -XX:HeapDumpPath dump the heap when an OOM occurs, aiding post‑mortem analysis.

JVM Parameter Settings Practice

When tuning JVM parameters, focus on garbage collector settings and memory allocation. The following example shows how to configure a Netty service (start.jar) on an 8 GB server.

Reserve OS memory: keep 2 GB for the operating system, leaving 6 GB for the application.

Determine direct memory: Netty uses off‑heap memory for performance. Reserve 2 GB (≈1/3 of available memory) for direct memory. -XX:MaxDirectMemorySize=2g Set Java heap size: allocate 3 GB for the heap (leaving 4 GB total for the JVM). -Xms3g -Xmx3g Configure young and old generations: assign 1 GB to the young generation (-XX:NewSize=1g) and the remaining 2 GB to the old generation. Use default Eden/Survivor ratios.

Set metaspace: with remaining memory, set metaspace to 128 MB. -XX:MetaspaceSize=128m Configure GC and OOM diagnostics: -XX:+UseParNewGC -XX:+UseConcMarkSweepGC for ParNew (young) and CMS (old) collectors, -XX:+UseG1GC if you prefer the G1 collector, -XX:+HeapDumpOnOutOfMemoryError and -XX:HeapDumpPath=dump.log for heap dumps,

-XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimestamps -XX:+PrintHeapAtGC

for detailed GC logs, -Xloggc:../gc/gc.log to specify the GC log file.

Complete command line example:

java -server
-XX:MaxDirectMemorySize=2g # direct memory 2GB
-Xms3g -Xmx3g # heap 3GB
-XX:NewSize=1g # young generation 1GB
-XX:MetaspaceSize=128m # metaspace 128MB
-XX:+UseParNewGC -XX:+UseConcMarkSweepGC # ParNew for young, CMS for old
-XX:+HeapDumpOnOutOfMemoryError # dump heap on OOM
-XX:HeapDumpPath=dump.log # dump file location
-XX:+PrintGC # enable GC logging
-XX:+PrintGCDetails # detailed GC logs
-XX:+PrintGCTimestamps # timestamps in GC logs
-XX:+PrintHeapAtGC # print heap before/after GC
-Xloggc:../gc/gc.log # GC log file path
-jar start.jar

Note that JVM options differ between versions; for example, -XX:PermSize and -XX:MaxPermSize control the permanent generation in pre‑Java 8 JVMs, but these options are obsolete in Java 8 and later.

JavaJVMGarbage CollectionMemory Tuning
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.