Skip to content

Instantly share code, notes, and snippets.

@thikade
Last active March 8, 2023 12:02
Show Gist options
  • Save thikade/d51c96907c044cef81dabbf5371a713e to your computer and use it in GitHub Desktop.
Save thikade/d51c96907c044cef81dabbf5371a713e to your computer and use it in GitHub Desktop.
Java GC tuning

determine JVM defaults

java -XshowSettings:vm -XX:+PrintFlagsFinal -version

Default Java 1.8.0_275: (-XX:+UseParallelGC)

$ java -XshowSettings:vm -XX:+PrintFlagsFinal  -version 2>&1 | \
  grep -E 'GCTimeRatio|HeapFreeRatio|MaxRAM|MaxHeapSize|UnlockExperimentalVMOptions|UseCGroupMemoryLimitForHeap'
    uintx DefaultMaxRAMFraction                     = 4                                   {product}
    uintx GCTimeRatio                               = 99                                  {product}
    uintx MaxHeapFreeRatio                          = 100                                 {manageable}
    uintx MaxHeapSize                              := 4152360960                          {product}
 uint64_t MaxRAM                                    = 137438953472                        {pd product}
    uintx MaxRAMFraction                            = 4                                   {product}
   double MaxRAMPercentage                          = 25.000000                           {product}
    uintx MinHeapFreeRatio                          = 0                                   {manageable}
VM settings:
    Max. Heap Size (Estimated): 3.44G
    Ergonomics Machine Class: server
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)

UseG1GC Java 1.8.0_275: (-XX:+UseG1GC)

$ java   -XX:+UseG1GC  -XX:+PrintFlagsFinal  -version 2>&1 | \
  grep -E 'GCTimeRatio|HeapFreeRatio|MaxRAM|MaxHeapSize|UnlockExperimentalVMOptions|UseCGroupMemoryLimitForHeap|Threads'
     bool BindGCTaskThreadsToCPUs                   = false                               {product}
    uintx ConcGCThreads                            := 2                                   {product}
    uintx DefaultMaxRAMFraction                     = 4                                   {product}
    uintx G1ConcRefinementThreads                   = 8                                   {product}
    uintx GCTimeRatio                               = 9                                   {product}
    uintx MaxHeapFreeRatio                          = 70                                  {manageable}
    uintx MaxHeapSize                              := 4152360960                          {product}
 uint64_t MaxRAM                                    = 137438953472                        {pd product}
    uintx MaxRAMFraction                            = 4                                   {product}
   double MaxRAMPercentage                          = 25.000000                           {product}
    uintx MinHeapFreeRatio                          = 40                                  {manageable}
    uintx ParallelGCThreads                         = 8                                   {product}
     bool TraceDynamicGCThreads                     = false                               {product}
     bool UseBoundThreads                           = true                                {product}
     bool UseDynamicNumberOfGCThreads               = false                               {product}

Containerized JVMs

If the container memory limit is set and the experimental options are supported by the JVM,
set -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap.

This sets -XX:MaxRAM to the container memory limit, and the maximum heap size (-XX:MaxHeapSize / -Xmx) to 1/-XX:MaxRAMFraction (1/4 by default).

I have a 2gb heap, and my jvm instance is using 4gb, where are the other 2gb going?

You can track native memory by running Java with the following command line arguments:

java -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=”summary” -XX:+PrintNMTStatistics

GC Logging

Garbage Collection Logging Settings for Java 8 (non-modular JVM)

-verbose:gc -Xloggc:/opt/eap/standalone/log/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps 
-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=3M -XX:-TraceClassUnloading

Garbage Collection Logging Settings for Java 11 (modular JVM)

-Xlog:gc*:file=/opt/eap/standalone/log/gc.log:time,uptimemillis:filecount=5,filesize=3M
@thikade
Copy link
Author

thikade commented Mar 16, 2021

Excellent series explaining Metaspace in Java 11 in detail: https://stuefe.de/posts/metaspace/what-is-metaspace/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment