Skip to content

Instantly share code, notes, and snippets.

@Raboo
Last active August 6, 2025 16:11
Show Gist options
  • Save Raboo/39e3edf0ce9b5a1efbdfc7459951f950 to your computer and use it in GitHub Desktop.
Save Raboo/39e3edf0ce9b5a1efbdfc7459951f950 to your computer and use it in GitHub Desktop.
JVM Container optimization notes

JVM 21 introduces G1GC Generational, it can reduce heap and cpu consumption.
ZGenerational ZGC mode organises objects into young and old generations, allowing tailored garbage collection strategies. Fast incremental sweeps in the young generation ensure responsive performance, while comprehensive, less frequent clean-ups in the old generation maintain stability over time, exclusively supported in recent JDK versions like JDK 21

-XX:+UseZGC -XX:+ZGenerational

ZGC got production ready in JDK 15, focuses more on lower latency, but can also increase throughput in many cases.

-XX:+UseZGC

G1GC is default since JDK 9 and has quite decent balance between throughput and latency.

-XX:+UseG1GC

JVM 18 introduces the JVM option -XX:+UseStringDeduplication. This feature (disabled by default) tells ZGC to find and deduplicate identical character arrays backing String objects to reduce overall heap memory usage.

Percentage based heap settings, great for containers and non-shared servers

-XX:InitialRAMPercentage=20 -XX:MaxRAMPercentage=75 -XX:MinRAMPercentage=80

Starting from Java 10, this parameter (which is enabled by default) is used to make the JVM take the container memory limits into account when allocating the heap size, not the host machine configuration.
This was backported to Java 8, but not enabled by default.

-XX:+UseContainerSupport

Table for memory rules when using -XX:+UseContainerSupport.

Container memory limit <size> Maximum Java heap size
Less than 1 GB 50% <size>
1 GB - 2 GB <size> - 512 MB
Greater than 2 GB 75% <size>

Rules

  • The application is running in a container environment.
  • The memory limit for the container is set.
  • The -XX:+UseContainerSupport option is set, which is the default behavior since Java 10.

JVM 13 introduced -XX:SoftMaxHeapSize=2G -Xmx5G for ZGC.
Using -XX:SoftMaxHeapSize=2G -Xmx5G will tell ZGC to keep the max heap usage at 2G, but it’s allowed to grow to 5G if it otherwise would have resulted in an allocation stall or an OutOfMemoryError. This is useful when you want to keep the memory footprint down, while maintaining the capability to handle a temporary increase in heap space requirement.

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