Video in progress

Strong Middle ⚙️ JVM and memory

How do you find out the actual values of JVM flags, and what does the "ergonomic" mark in the PrintFlagsFinal output mean?

java -XX:+PrintFlagsFinal -version, or jcmd <pid> VM.flags -all on a running process, shows the final value of every flag. Next to each value is its source: default, command line or ergonomic; the last means the JVM chose the value itself based on the machine's resources.

📖 Answer

On prod you shouldn't answer "which GC is running?" or "how big is the heap really?" with guesses. The container, the JDK version and JAVA_TOOL_OPTIONS all have an effect.

$ java -XX:+PrintFlagsFinal -version | grep -E 'UseG1GC|MaxHeapSize|ParallelGCThreads|ReservedCodeCacheSize'
     bool UseG1GC                  = true            {product} {ergonomic}
   size_t MaxHeapSize              = 1610612736      {product} {ergonomic}
     uint ParallelGCThreads        = 2               {product} {default}
    uintx ReservedCodeCacheSize    = 251658240       {pd product} {ergonomic}

The columns: type, name, value, flag kind (product, diagnostic, experimental, pd is a platform-dependent default), source.

{ergonomic} means the JVM calculated it itself: for example, MaxHeapSize is 25% of the container limit (or MaxRAMPercentage), and the GC depends on CPU and memory. These values change on another node or with another limit. That is why on prod I write the important things (the GC, the heap percentage) explicitly, so they show up as {command line}.

For a running JVM:

jcmd <pid> VM.flags            # only the ones that were changed
jcmd <pid> VM.flags -all       # everything
jcmd <pid> VM.command_line     # what arguments it was started with
jinfo -flag MaxHeapSize <pid>

Some manageable flags can be changed in a running JVM: jcmd <pid> VM.set_flag HeapDumpOnOutOfMemoryError true.

Another useful thing: running diff on the PrintFlagsFinal output of two JDK versions shows right away where the defaults changed before a migration. If JAVA_TOOL_OPTIONS is used, the JVM writes "Picked up JAVA_TOOL_OPTIONS: ..." at startup, so look for that in the log too.