Java 8 GC Tutorials - Herong's Tutorial Examples - v1.03, by Dr. Herong Yang
"-XX:TargetSurvivorRatio" - Second Tenuring Condition
This section describes the '-XX:TargetSurvivorRatio' JVM option, with sets the maximum survivor space usage percentage. When this limit is reached, all remaining live objects will be promoted to Tenured generation regardless of their age.
In previous tutorials, we learned the first condition of live object tenuring (promoted from survivor space into Tenured generation). When a live object has reached the tenuring threshold age, it will be promoted into Tenured generation.
However, there another condition that triggers object tenuring. When the survivor space usage has reached target ratio defined by another option called "-XX:TargetSurvivorRatio", remaining live objects, regardless of their age, will be promoted into Tenured generation. This case is also called premature tenuring (or premature promotion).
The Serial collector, uses "-XX:TargetSurvivorRatio=50" as the default, which says, I need to keep my survivor space usage below 50%. This seems to be a big waste of heap memory.
Let's run our first test with "-XX:TargetSurvivorRatio=50" on our test program GarbageCollection2.java.
herong> \progra~1\java\jdk1.8.0\bin\java -Xms360m -Xmx360m \ -XX:NewRatio=1 -XX:SurvivorRatio=1 \ -XX:TargetSurvivorRatio=50 \ -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC \ -XX:+PrintTenuringDistribution GarbageCollection2 ... {Heap before GC invocations=2 (full 0): def new generation total 122880K, used 92590K [... eden space 61440K, 98% used [... from space 61440K, 52% used [... to space 61440K, 0% used [... tenured generation total 184320K, used 0K [... the space 184320K, 0% used [... Metaspace used 1567K, capacity 2242K, committed 2368K, reserved 4480K [GC (Allocation Failure) [DefNew Desired survivor size 31457280 bytes, new threshold 15 (max 15) - age 1: 25166208 bytes, 25166208 total - age 2: 5242960 bytes, 30409168 total : 92590K->29696K(122880K), 0.0142930 secs] 92590K->31990K(307200K), 0.0143288 secs] Heap after GC invocations=3 (full 0): def new generation total 122880K, used 29696K [... eden space 61440K, 0% used [... from space 61440K, 48% used [... to space 61440K, 0% used [... tenured generation total 184320K, used 2294K [... the space 184320K, 1% used [... Metaspace used 1567K, capacity 2242K, committed 2368K, reserved 4480K }
As you can see from the output, the third Minor GC promoted 2294K objects to Tenured generation prematurely, because TargetSurvivorRatio of 50% was reached.
Now rerun the test with "-XX:TargetSurvivorRatio=65". And we can also lower the survivor space to 50MB.
herong> \progra~1\java\jdk1.8.0\bin\java -Xms300m -Xmx300m \ -XX:NewRatio=1 -XX:SurvivorRatio=1 \ -XX:TargetSurvivorRatio=65 \ -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC \ -XX:+PrintTenuringDistribution GarbageCollection2 ... {Heap before GC invocations=203 (full 0): def new generation total 102400K, used 82925K [... eden space 51200K, 99% used [... from space 51200K, 62% used [... to space 51200K, 0% used [... tenured generation total 153600K, used 246K [... the space 153600K, 0% used [... Metaspace used 1567K, capacity 2242K, committed 2368K, reserved 4480K [GC (Allocation Failure) [DefNew Desired survivor size 34078720 bytes, new threshold 15 (max 15) - age 1: 24117616 bytes, 24117616 total - age 2: 6291552 bytes, 30409168 total - age 4: 2097184 bytes, 32506352 total : 82925K->31744K(102400K), 0.0084702 secs] 83172K->31991K(256000K), 0.0085043 secs] Heap after GC invocations=204 (full 0): def new generation total 102400K, used 31744K [... eden space 51200K, 0% used [... from space 51200K, 62% used [... to space 51200K, 0% used [... tenured generation total 153600K, used 246K [... the space 153600K, 0% used [... Metaspace used 1567K, capacity 2242K, committed 2368K, reserved 4480K }
As you can see from the output, after 204 Minor GCs, no objects from our array promoted to Tenured generation, because 65% of 50M survivor space is enough to hold all live objected at different ages.
If we maximize the survivor space usage with "-XX:TargetSurvivorRatio=99", we could lower survivor space to 34MB to hold all liver objects.
herong> \progra~1\java\jdk1.8.0\bin\java -Xms105m -Xmx105m \ -XX:NewSize=102m -XX:SurvivorRatio=1 \ -XX:TargetSurvivorRatio=99 \ -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC \ -XX:+PrintTenuringDistribution GarbageCollection2 ... {Heap before GC invocations=302 (full 0): def new generation total 69632K, used 66219K [... eden space 34816K, 99% used [... from space 34816K, 91% used [... to space 34816K, 0% used [... tenured generation total 4096K, used 246K [... the space 4096K, 6% used [... Metaspace used 1567K, capacity 2242K, committed 2368K, reserved 4480K [GC (Allocation Failure) [DefNew Desired survivor size 35295068 bytes, new threshold 15 (max 15) - age 1: 25166208 bytes, 25166208 total - age 2: 4194368 bytes, 29360576 total - age 4: 2097184 bytes, 31457760 total - age 5: 1048592 bytes, 32506352 total : 66219K->31744K(69632K), 0.0077266 secs] 66466K->31991K(73728K), 0.0077804 secs] Heap after GC invocations=303 (full 0): def new generation total 69632K, used 31744K [... eden space 34816K, 0% used [... from space 34816K, 91% used [... to space 34816K, 0% used [... tenured generation total 4096K, used 246K [... the space 4096K, 6% used [... Metaspace used 1567K, capacity 2242K, committed 2368K, reserved 4480K }
Table of Contents
Heap Memory Area and Size Control
JVM Garbage Collection Logging
Introduction of Garbage Collectors
►Serial Collector - "+XX:+UseSerialGC"
GC Log Message Format for Serial Collector
GC Log Message Examples of Serial Collector
Log Message Types from Serial Collector
Serial Collector Stops Application for Minor/Major GC
Usage Report on Heap Memory Areas
Default NewRatio - Old vs. New Generation
"-XX:NewRatio" - Ratio of Tenured and "new" Generation
"-XX:SurvivorRatio" - Ratio of Eden and Survivor Space
Serial GC Tracing - Tight Heap
Serial GC Tracing - Plenty of Heap
Serial GC Tracing - Aged Live Objects
Serial GC Tracing - Tenuring Threshold
►"-XX:TargetSurvivorRatio" - Second Tenuring Condition
Serial GC Tracing - Tenuring Threshold Controlled
"-XX:+NeverTenure" and "-XX:+AlwaysTenure" not Working
Minor GC Triggering Condition of Serial Collector
Parallel Collector - "+XX:+UseParallelGC"
Concurrent Mark-Sweep (CMS) Collector - "+XX:+UseConcMarkSweepGC"
Garbage First (G1) Collector - "+XX:+UseG1GC"
Object References and Garbage Collection
Garbage Collection Performance Test Program
Performance Tests on Serial Collector
Performance Tests on Parallel collector
Performance Tests on Concurrent collector
Performance Tests on G1 collector