Video in progress

How do the -D, -X and -XX prefixes in JVM parameters differ from each other?

-D sets a system property (-Dspring.profiles.active=prod). -X are non-standard but widespread options (-Xmx, -Xss). -XX are HotSpot's internal settings (-XX:+UseG1GC), and they can change from version to version.

📖 Answer

java -Xms512m -Xmx512m \
     -XX:+UseG1GC -XX:MaxGCPauseMillis=100 \
     -Dfile.encoding=UTF-8 -Dspring.profiles.active=prod \
     -jar app.jar

-Dkey=value is a value you read with System.getProperty("key"). The JVM itself doesn't use it; the application and libraries do. The list of -X... options is shown by java -X. -XX:+Flag / -XX:-Flag turns a boolean on or off, and -XX:Name=value sets a value. Some work only after -XX:+UnlockExperimentalVMOptions or -XX:+UnlockDiagnosticVMOptions.

Order matters: the parameters must come before -jar app.jar. Those written after it are passed as arguments to the application's main(args); people often slip here in interviews.

To see which flags a running JVM is using:

jcmd <pid> VM.flags
jcmd <pid> VM.command_line
java -XX:+PrintFlagsFinal -version | grep MaxHeapSize

In a container the flags are often passed through the JAVA_TOOL_OPTIONS environment variable: the JVM reads it by itself.