Java Team
Introduction to Garbage
Collection
OpenJDK: Automatic Memory Management
Monica Beckwith
JVM Performance
java-performance@Microsoft
@mon_beck
Agenda
What is Garbage Collection (GC)?
Allocation, Reclamation == Memory Management
Techniques of Automatic Memory Management
A Few GC Algorithms in OpenJDK HotSpot VM
July 1st , 2020
What We Will Try To Cover Today
Garbage Collection aka
Memory Management
==
Allocation
+
Reclamation
Allocation: The Manual Way …
Allocation Space
Allocations Happen …
O O O O O O O O O O
O O O O O O O
O O O O O O O O
Space Gets Filled Up
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
Its Marking Time!
Root Set
O
O
O
O
O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
Its Marking Time! – Tracing from the Root Set
Its Marking Time! – Creating Live Object Graph
Root Set
O
O
O
O
O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
Ready to Reclaim?
Root Set
O
O
O
O
O
O O O O O
O O O O O O O O
O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
Ready to Reclaim? – Are We Done?
Manual Reclamation – Problem!
Root Set
O
O
O
O
O
O O O O O
O O O O O O O O
O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
This can leadto a
memory leak!!
Manual Reclamation – Problem: Memory Leak
Root Set
O
O
O
O
O
O O O O O
O O O O O O O O
O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
Manual Reclamation – Problem!
Root Set
O
O
O
O
O
O O O O O
O O O O O O O O
O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O
Two dangling
pointers!!
Manual Reclamation – Problem: Dangling Pointers
So What Can We Do?
Stack Based Allocation:
- Push and pop will take care of the issues
- E.g. Java bytecode interpreter
Hybrid Allocation:
- These typically combine region-based allocation with reference
counting or tracing garbage collection
- E.g. OpenJDK GCs using Thread Local Allocation Buffers (TLABs)
Reference Counting vs Tracing
“The Continuous Tracker” Active When Full Or When Threshold Crossed
Basic Considerations of Automatic Memory
Management
Collection Efficiency
- It maybe more efficient to leave some garbage around
- E.g. G1 GC’s incremental compaction / mixed collection
Performance Trifecta
- GC designs are based around responsiveness, footprint considerations
and throughput efficiency
- E.g. OpenJDK’s Serial GC for low footprint, Parallel GC for throughput
efficiency
Further Considerations …
Generational Hypothesis
- Most objects die young
- E.g. OpenJDK GCs other than ZGC and Shenandoah GC
Age Threshold
- Promote only the long-lived objects (including transients)
- E.g. OpenJDK’s generational collectors with survivor space
Groundwork : Heap
layout - regions and
generations
Heap Layout
Heap
Z GC
Shenandoah GC
Young Generation
G1 GC
Old Generation
Commonalities :
Copying collector –
from and to spaces From To
HeapFrom Space To Space
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
GC ROOTS
THREAD
1 STACK
THREAD
N STACK
STATIC
VARIABLES
ANY JNI
REFERENCES
Copying Collector aka Compacting Collector aka
Evacuation
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
GC ROOTS
THREAD
1 STACK
THREAD
N STACK
O O
O O
O
STATIC
VARIABLES
ANY JNI
REFERENCES
O
OO
O
O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
Copying Collector aka Compacting Collector aka
Evacuation
Copying Collector aka Compacting Collector aka
Evacuation
O O O O O O O O
O O O O O O O
O O O O O O O
O O O
O O O
O O
Commonalities : Regions
– occupied and free
Occupied and Free Regions
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O
O O O O
O O O O
• List of free regions
• In case of generational heap (like G1), the occupied regions could be young, old or
humongous
Commonalities :
Collection set and
priority
Collection Priority and Collection Set
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O
O O O O
O O O O
OO
OO
OO
OO
OO
OO
OO
OO
OO
O
OO
OO
OO
OO
OO
OO
OO
OO
OO
O
OOOO
• Priority is to reclaim regions with most garbage
• The candidate regions for collection/reclamation/relocation are said to be in a collection set
• There are threshold based on how expensive a region can get and maximum regions to
collect
• Efficiency: Incremental collection aka incremental compaction or partial compaction
• Usually needs a threshold that triggers the compaction
• Stops after the desired reclamation threshold or free-ness threshold is reached
• Doesn’t need to be stop-the-world
Introduction : G1,
Shenandoah & Z -
Algorithms
Algorithm and Other Considerations
Garbage Collectors G1 GC Shenandoah GC Z GC
Regionalized? Yes Yes Yes
Generational? Yes No No
Compaction? Yes, STW, Forwarding
address in header
Yes, Concurrent,
Forwarding Pointer
Yes, Concurrent,
Colored Pointers
Target Pause Times? 200ms 10ms 10ms
Concurrent Marking
Algorithm?
SATB SATB Striped
Further Reading
https://www.youtube.com/watch?v=VCeHkcwfF9Q
https://www.usenix.org/legacy/events/vee05/full_papers/p46-click.pdf
http://mail.openjdk.java.net/pipermail/zgc-dev/2017-December/000047.html
http://hg.openjdk.java.net/zgc/zgc/file/ffab403eaf14/src/hotspot/share/gc/z/zB
arrier.cpp
https://wiki.openjdk.java.net/display/zgc/Main
https://www.azul.com/files/c4_paper_acm1.pdf
© Copyright Microsoft Corporation. All rights reserved.
Stop-the-world aka STW GC
Application Threads
GC Threads
Application Threads
Safepoint
Requested
GC
Completed
Application Threads GC Threads Application Threads
Safepoint
Requested
GC
Completed
Handshakes
Thread local handshakes vs Global
Time To Safepoint
(TTSP)
Concurrent GC
Application Threads
GC Threads
© Copyright Microsoft Corporation. All rights reserved.

Intro to Garbage Collection

  • 1.
    Java Team Introduction toGarbage Collection OpenJDK: Automatic Memory Management Monica Beckwith JVM Performance java-performance@Microsoft @mon_beck
  • 2.
    Agenda What is GarbageCollection (GC)? Allocation, Reclamation == Memory Management Techniques of Automatic Memory Management A Few GC Algorithms in OpenJDK HotSpot VM July 1st , 2020 What We Will Try To Cover Today
  • 3.
    Garbage Collection aka MemoryManagement == Allocation + Reclamation
  • 4.
    Allocation: The ManualWay … Allocation Space
  • 5.
    Allocations Happen … OO O O O O O O O O O O O O O O O O O O O O O O O
  • 6.
    Space Gets FilledUp O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O
  • 7.
    Its Marking Time! RootSet O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O Its Marking Time! – Tracing from the Root Set
  • 8.
    Its Marking Time!– Creating Live Object Graph Root Set O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O
  • 9.
    Ready to Reclaim? RootSet O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O Ready to Reclaim? – Are We Done?
  • 10.
    Manual Reclamation –Problem! Root Set O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O This can leadto a memory leak!! Manual Reclamation – Problem: Memory Leak
  • 11.
    Root Set O O O O O O OO O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O Manual Reclamation – Problem!
  • 12.
    Root Set O O O O O O OO O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O Two dangling pointers!! Manual Reclamation – Problem: Dangling Pointers
  • 13.
    So What CanWe Do? Stack Based Allocation: - Push and pop will take care of the issues - E.g. Java bytecode interpreter Hybrid Allocation: - These typically combine region-based allocation with reference counting or tracing garbage collection - E.g. OpenJDK GCs using Thread Local Allocation Buffers (TLABs)
  • 14.
    Reference Counting vsTracing “The Continuous Tracker” Active When Full Or When Threshold Crossed
  • 15.
    Basic Considerations ofAutomatic Memory Management Collection Efficiency - It maybe more efficient to leave some garbage around - E.g. G1 GC’s incremental compaction / mixed collection Performance Trifecta - GC designs are based around responsiveness, footprint considerations and throughput efficiency - E.g. OpenJDK’s Serial GC for low footprint, Parallel GC for throughput efficiency
  • 16.
    Further Considerations … GenerationalHypothesis - Most objects die young - E.g. OpenJDK GCs other than ZGC and Shenandoah GC Age Threshold - Promote only the long-lived objects (including transients) - E.g. OpenJDK’s generational collectors with survivor space
  • 17.
    Groundwork : Heap layout- regions and generations
  • 18.
    Heap Layout Heap Z GC ShenandoahGC Young Generation G1 GC Old Generation
  • 19.
    Commonalities : Copying collector– from and to spaces From To
  • 20.
    HeapFrom Space ToSpace O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O GC ROOTS THREAD 1 STACK THREAD N STACK STATIC VARIABLES ANY JNI REFERENCES Copying Collector aka Compacting Collector aka Evacuation
  • 21.
    O O OO O O O O O O O O O O O O O O O O O O O O O O O O O O GC ROOTS THREAD 1 STACK THREAD N STACK O O O O O STATIC VARIABLES ANY JNI REFERENCES O OO O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O Copying Collector aka Compacting Collector aka Evacuation
  • 22.
    Copying Collector akaCompacting Collector aka Evacuation O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O
  • 23.
    Commonalities : Regions –occupied and free
  • 24.
    Occupied and FreeRegions O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O • List of free regions • In case of generational heap (like G1), the occupied regions could be young, old or humongous
  • 25.
  • 26.
    Collection Priority andCollection Set O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O OO OO OO OO OO OO OO OO OO O OO OO OO OO OO OO OO OO OO O OOOO • Priority is to reclaim regions with most garbage • The candidate regions for collection/reclamation/relocation are said to be in a collection set • There are threshold based on how expensive a region can get and maximum regions to collect • Efficiency: Incremental collection aka incremental compaction or partial compaction • Usually needs a threshold that triggers the compaction • Stops after the desired reclamation threshold or free-ness threshold is reached • Doesn’t need to be stop-the-world
  • 27.
  • 28.
    Algorithm and OtherConsiderations Garbage Collectors G1 GC Shenandoah GC Z GC Regionalized? Yes Yes Yes Generational? Yes No No Compaction? Yes, STW, Forwarding address in header Yes, Concurrent, Forwarding Pointer Yes, Concurrent, Colored Pointers Target Pause Times? 200ms 10ms 10ms Concurrent Marking Algorithm? SATB SATB Striped
  • 29.
  • 30.
    © Copyright MicrosoftCorporation. All rights reserved.
  • 31.
    Stop-the-world aka STWGC Application Threads GC Threads Application Threads Safepoint Requested GC Completed Application Threads GC Threads Application Threads Safepoint Requested GC Completed Handshakes Thread local handshakes vs Global Time To Safepoint (TTSP)
  • 32.
  • 33.
    © Copyright MicrosoftCorporation. All rights reserved.