The document discusses garbage collection techniques in Java. It begins with an overview of manual memory management and the problems it poses, such as memory leaks and dangling pointers. It then covers automatic memory management techniques used in Java, including garbage collection algorithms like reference counting and tracing. Specific algorithms used in OpenJDK's HotSpot virtual machine are examined, such as the G1, Shenandoah, and Z garbage collectors. Key aspects of garbage collection like heap layout, generations, and concurrent versus stop-the-world collection are summarized.
1. Java Team
Introduction to Garbage
Collection
OpenJDK: Automatic Memory Management
Monica Beckwith
JVM Performance
java-performance@Microsoft
@mon_beck
2. 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
6. 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
7. 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
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?
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?
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 O O O O
O O O O O O O O
O O O O O O O
O O O 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 O O O O
O O O O O O O O
O O O O O O O
O O 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 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)
14. Reference Counting vs Tracing
“The Continuous Tracker” Active When Full Or When Threshold Crossed
15. 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
16. 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
20. 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
21. O O O O O O 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 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
24. 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
26. 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