JVM GC Tuning in Containerised
Environment
Inspiring Brilliance
Garbage Collection
● Manages allocation and deallocation of the objects
JVM Memory Layout
GC Operations
● Mark
● Sweep
● Scavenge
● Compact
Commonly Faced Issues In Container Environment
● Application in zombie state
● OOM / Container OOM
● Unpredictable workload
● Sudden increase in latency
Prerequisite To The Tuning
● Exiting on Out of Memory (-XX:+ExitOnOutOfMemoryError)
● Print flags (-XX:+PrintFlagsFinal)
● GC logs
(-Xlog:gc=debug:file=/tmp/gc.log:time,uptime,level,tags:filecoun
t=5,filesize=100m)
● Making sure container has tools to support debugging (jmap)
Monitoring The Metrics
● Prometheus metrics
○ jvm_gc_pause_seconds
○ jvm_gc_memory_allocated_bytes_total
○ jvm_gc_memory_promoted_bytes_total
Tune GC Settings If
● Long pause of GC
● Frequent Full GC
● Frequent OOM
GC Optimization Goals
● Application Throughput
● Latency
● Memory Footprint
JVM Ergonomics
● Depending on where JVM is running it auto-tunes itself
● Tries to maximize one of the two goals pause time and throughput
● Target environment could be a lot different from local environment
Collecting Heap Dump
● Collect while application is running
● Collect when application gets crashed due to OOM - -XX:
+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps
● Retrieving the dump
○ kubectl cp / docker cp
○ copy from the node which is running the container
Monitoring the application
● Enable remote connections
● Tools to connect to running VM
○ Jconsole
○ VisualVM
○ Java Flight Recorder
Choosing Garbage Collector
Serial Collector
● Single threaded garbage collector
● Suitable for applications which are single core and have memory 100MB -
300MB
Parallel Collector (Throughput)
● Behaviour Tuning
● Latency -XX:MaxGCPauseMillis - No default value
● Throughput -XX:GCTimeRatio 1 / (1 + GCTimeRatio) - Default 12 (8%)
● Footprint -Xmx
● Additional Tuning
○ -XX:MinHeapFreeRatio
○ -XX:MaxHeapFreeRatio
○ -XX:AdaptiveSizePolicyWeight
Garbage First (G1GC)
Garbage First (G1GC)
● Throughput - -XX:MaxGCPauseMillis (Higher value)
● Latency - -XX:MaxGCPauseMillis (Lower value)
● Incurs overhead in application threads and space reclamation
● Meant for heaps larger than 10G
Z GC
● Non-generational and Concurrent (-XX:ConcGCThreads)
● GC pause doesn’t increase with the size of the heap
● Intended for low latency applications
● Also returns unused memory back to OS (-XX:-ZUncommit)
Epsilon
● NO OP Garbage collector (-XX:+UseEpsilonGC)
● Handles memory allocation but doesn’t implement
reclamation
Heap Sizing
● Law of diminishing returns
References
● https://www.oreilly.com/library/view/java-performance-the/9781449363512/
● https://docs.oracle.com/en/java/javase/17/gctuning/introduction-garbage-collection-tuning.ht
ml#GUID-326EB4CF-8C8C-4267-8355-21AB04F0D304
Thanks!
Contact