Containers provide much better elasticity and density than VMs, but JVM-based applications are not fully container-ready. The first issue is that HotSpot JVM doesn’t release unused committed heap memory automatically. Second, it is not possible to increase the size of the JVM heap at runtime. To solve these two major issues and make JVM more container-friendly, a new patch is implemented for the Garbage-First collector in OpenJDK 9. This presentation shares details of what is done and how the added improvements enhance resource consumption efficiency.
From the session at Oracle Code One 2018 presented by Ruslan Synytsky, Jelastic CEO.
15. G1 Garbage Collector: Fast Memory Usage Growth
java -XX:+UseG1GC -Xmx2g -Xms32m -jar app.jar 0
Memory grew from 32 MB to 1 GB in 25 seconds
https://github.com/jelastic/java-vertical-scaling-test
16. G1: Medium Memory Usage Growth
java -XX:+UseG1GC -Xmx2g -Xms32m -jar app.jar 10
Memory grew from 32 MB to 1 GB in 90 seconds during 4 cycles
17. G1: Slow Memory Usage Growth
java -XX:+UseG1GC -Xmx2g -Xms32m -jar app.jar 100
Memory grew from 32 MB to 1 GB with delta time growth of about 300 seconds
18. G1: Aggressive Heap = No Vertical Scaling
java -XX:+UseG1GC -Xmx2g -Xms2g
or
java -XX:+UseG1GC -Xmx2g -XX:+AggressiveHeap
19. Parallel Garbage Collector
java -XX:+UseParallelGC -Xmx2g -Xms32m -jar app.jar 10
The unused but committed RAM is never released back to OS
20. Serial Garbage Collector
java -XX:+UseSerialGC -Xmx2g -Xms32m -jar app.jar 10
It requires 4 Full GC cycles to release all unused resources
21. ConcMarkSweep Garbage Collector
java -XX:+UseConcMarkSweepGC -Xmx2g -Xms32m -jar app.jar 10
It requires 4 Full GC cycles to release all unused resources
22. Calling Full GC On-Time
https://github.com/jelastic-jps/java-memory-agent
1. Full GC is not triggered automatically and must be executed explicitly
2. Can lead to significant performance degradation
Workaround:
inject an agent which monitors the memory usage and calls System.gc()
periodically:
-javaagent:jelastic-gc-agent.jar=period=300,debug=true
23. Shenandoah Garbage Collector
java -Xmx2g -Xms32m -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC
-XX:ShenandoahUncommitDelay=1000 -XX:ShenandoahGuaranteedGCInterval=10000 -jar app.jar 10
It releases unused RAM back to OS on the fly without Full GC calls
https://wiki.openjdk.java.net/display/shenandoah/Main
24. The Z Garbage Collector
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC
http://cr.openjdk.java.net/~pliden/slides/ZGC-FOSDEM-2018.pdf
25. OpenJ9
Command-line options to enable automatic vertical scaling:
● -XX:+IdleTuningGcOnIdle
● -XX:+IdleTuningCompactOnIdle
● -XX:IdleTuningMinIdleWaitTime=<secs>
● Low memory footprint
● Fast startup time
● High application throughput
● Smoother ramp-up in the cloud
https://www.eclipse.org/openj9/
27. Started in 2011
● Initiated investigation (at first, worked out only with SerialGC)
● Built cloud business model on vertical scaling
● Started to promote in the community
28. Credits To
● Ruslan Synytsky, Tetiana Fydorenchyk: Jelastic
● Rodrigo Bruno, Paulo Ferreira: INESC-ID / Instituto Superior Técnico,
University of Lisbon
● Jia Rao: The University of Texas at Arlington
● Hang Huang, Song Wu: Huazhong University of Science and Technology
● Thomas Schatzl: Oracle
29. Timely Reduce Unused Committed Memory
Make the G1 garbage collector automatically give back Java heap memory
to the operating system when idle
● G1PeriodicGCInterval
● G1PeriodicGCSystemLoadThreshold
● G1PeriodicGCInvokesConcurrent
http://openjdk.java.net/jeps/8204089
30. Dynamic Max Memory Limit
Allow a user to increase / decrease the amount of Java heap memory
available to the application at runtime http://openjdk.java.net/jeps/8204088
jinfo -flag CurrentMaxHeapSize=1g <java_pid>
33. Tracking Native Non-Heap Memory Usage
-XX:NativeMemoryTracking=summary
5-10% performance hit if this option is enabled
http://trustmeiamadeveloper.com/2016/03/18/where-is-my-memory-java/
34. Java SE Support for Docker CPU and RAM Limits
CPU (Java SE 8u131+)
-XX:ParalllelGCThreads or
-XX:CICompilerCount
if not specified → Docker CPU limit
RAM
Java heap via -Xmx
if not specified →
-XX:+UnlockExperimentalVMOptions
-XX:+UseCGroupMemoryLimitForHeap
https://jelastic.com/blog/java-and-memory-li
mits-in-containers-lxc-docker-and-openvz
https://blogs.oracle.com/java-platform-group
/java-se-support-for-docker-cpu-and-memor
y-limits