Flags worth knowing

JVM · lesson 14 of 34 · 3 min read

A short list of JVM flags that genuinely earn their place in a production start command.

Open this lesson in the learning hub

Key points

  • -Xmx caps the heap and -Xms sets the starting size. Setting them equal avoids resize pauses.
  • In a container prefer -XX:MaxRAMPercentage=75 over a fixed -Xmx. It follows the limit when someone changes it.
  • Modern JVMs read cgroup limits, so a container already gets a sensible heap and cpu count with no flags at all.
  • -Xss sets the thread stack size. Raise it for deep recursion, lower it if you run thousands of platform threads.
  • GC logging is unified since Java 9: -Xlog:gc*:file=gc.log:time,uptime. The old PrintGC flags are gone, not deprecated.
  • Two to set on every service: -XX:+HeapDumpOnOutOfMemoryError and a writable -XX:HeapDumpPath.

Example

# a sane production start command
JAVA_OPTS="-XX:MaxRAMPercentage=75"
JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/app"
JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:file=/var/log/app/gc.log:time,uptime:filecount=5,filesize=10M"

java $JAVA_OPTS -jar app.jar

# what is this JVM actually running with?
jcmd <pid> VM.flags
jcmd <pid> VM.system_properties
java -XX:+PrintFlagsFinal -version | grep -i maxheapsize

Size the heap, log the GC, and always dump on out of memory.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the JVM course, and every lesson in it is listed on the JVM contents page.