SlideShare a Scribd company logo
GC Algorithms
Michał Warecki
Who am I?
●
Programming Geek interested in:
–
GC
–
JiT Compilers
–
Concurrency
–
Non-blocking algorithms
–
Programming languages runtime
Outline
●
Introduction
●
Detecting dead objects
●
Basic algorithms
●
Generational GC
●
Multi-threaded GC
●
Real-time GC
What I'm not covering
●
GC tuning
●
JVM GC Options
●
JVM Specific GC Implementation
-Xms8g -Xmx8g -XX:MaxPermSize=256m -XX:NewSize=3G
-XX:MaxNewSize=3g -XX:NewRatio=4 -XX:MaxTenuringThreshold=5
-XX:+UseConcMarkSweepGC -XX:+CMSScavengeBeforeRemark
-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
-Xloggc:gclogs.txt -XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCApplicationConcurrentTime -XX:ParallelGCThreads=7
-XX:+UseGCTaskAffinity -XX:+BindGCTaskThreadsToCPUs
-XX:+UnlockDiagnosticVMOptions -XX:ParGCCardsPerStrideChunk=32768
Why do I need to know about GC?
Because GC stops your application!
Gc algorithms
Why to collect garbage?
●
Limited storage
●
Programmers do not like to get dirty
●
Programmers make mistakes
–
Too little collected – memory leaks – error
–
Too much collected – broken programs – error
●
Programmers like good software design
–
Explicit memory management conflicts with the software
engineering principles of abstraction and modularity
What is garbage?
Garbage is an object which does not carry any
reference from other objects.
Detecting dead objects
Reference tracing vs Reference counting
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference tracing
Root references HEAP
Reference counting (naive)
New():
ref ← allocate()
if ref = null
error “Out of memory”
rc(ref) ← 0
return ref
atomic Write(src, i, ref):
addReference(ref)
deleteReference(src[i])
src[i] ← ref
addReference(ref):
if ref != null
rc(ref) ← rc(ref) + 1
deleteReference(ref):
if ref != null
rc(ref) ← rc(ref) – 1
if rc(ref) = 0
for each fld in Pointers(ref)
deleteReference(*fld)
free(ref)
The Garbage Collection Handbook – Jones, Hosking, Moss
Reference counting (naive)
Root references HEAP
Reference counting (naive)
Root references
1
1
1
2
3
0
1
1
1
1
HEAP
Reference counting (naive)
Root references
1
1
1
1
1
0
1
1
1
1
HEAP
Reference counting (naive)
Root references
1
1
1
1
1
0
1
1
1
1
HEAP
Memory leak!
Detecting dead objects
●
Reference tracing
✔ No mutator overhead
✔ Collect cycles
✔ High throughput
✗ Batch style
✗ Not real time
●
Reference counting
✔ Incremental
✔ Short pause
✔ Real time
✗ Reference cycles
✗ High mutator overhead
✗ Low throughput
Basic tracing algorithms
Not Moving Moving
Mark/Sweep Mark/Compact Copying
Mark/Sweep vs Mark/Compact
Before collection
After Mark/Sweep
After Mark/Compact
Live object
Free space
Dead object
Mark/Sweep vs Mark/Compact
After Mark/Sweep
After Mark/Compact
Free list allocation
Bump the pointer allocation
Mark/Sweep vs Mark/Compact
●
Mark/Sweep
✔ Fast
✗ Fragmentation
✗ Slower free list
allocation
●
Mark/Compact
✗ Slow
✔ Compacted heap
✔ Fast bump the pointer
allocation
Copying GC (Ping Pong)
ToFrom
Copying GC (Ping Pong)
ToFrom
Copying GC (Ping Pong)
To From
Copying GC (Survivor spaces)
Eden From To
Eden From To
Eden From To
Eden From To
Copying GC (Survivor spaces)
Eden From To
Eden To From
Eden To From
Eden From To
Copying GC
✔ Compacted heap
✔ The speed depends on
the number of live
objects
✔ Possible improvement of
locality during
evacuation
✗ Space overhead
Generational hypothesis
Weak generational hypothesis is the
observation that, in most cases, young objects
are much more likely to die than old objects.
Conversely any object that has survived several
GC cycles will probably survive a lot more.
Generational GC
Young space Old space
4 10 16 16
Objects headers
Object age
Max tenuring threashold = 15
Generational GC algorithms
Young space Old space
Copying GC
Mark/Sweep
Mark/Compact
Reference counting
Dynamic Generational GC
G1 GC
Eden
Survivor
Old
Humongous
Unused
Card Table and Remembered Set
Young space Old space
Card Table
Dirty
Dirty card – write to memory – possible reference to young generation.
Will be added to Remembered Set
Card Table
Ref picture: http://blog.ragozin.info/2011/06/understanding-gc-pauses-in-jvm-hotspots.html
Multi-threaded GC
Mutator
GC
Serial GC
Parallel GC
Concurrent GC
Incremental GC
Thread Local Allocation Buffer
●
Thread allocates within TLAB using bump the pointer
●
Improved objects locality
●
No contention single pointer
TLAB
Promotion Local Allocation Buffer
Each thread has two PLABs:
• One for survivor space,
• One for tenured space
PLAB1 PLAB2
GC Thread 1
GC Thread 2
Real-time GC
Plane: I'm landing.
GC: Pff, please wait
2 minutes, I'm collecting
Real-time GC
Real-time systems impose operational deadlines
on particular tasks within an application. These
real-time tasks must be able to response to
application inputs (events) within a fixed time
window.
The Garbage Collection Handbook – Jones, Hosking, Moss
Metronome GC
Traditional GC
Metronome GC
Thanks!!
Questions?

More Related Content

What's hot (19)

PDF
[Jbcn 2016] Garbage Collectors WTF!?
Alonso Torres
 
PPTX
Java. Есть ли свет в конце тоннеля
Alexey Demin
 
PDF
FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...
Rob Skillington
 
PDF
Using R in remote computer clusters
Burak Himmetoglu
 
PDF
Understanding JVM GC: advanced!
Jean-Philippe BEMPEL
 
PPT
Taming Java Garbage Collector
Daya Atapattu
 
ODP
Hotspot gc
Michał Warecki
 
ODP
Debugging and Profiling Rails Application
David Paluy
 
PDF
Understanding low latency jvm gcs V2
Jean-Philippe BEMPEL
 
PDF
Understanding jvm gc advanced
Jean-Philippe BEMPEL
 
PDF
Understanding low latency jvm gcs
Jean-Philippe BEMPEL
 
PDF
Solving Multi-tenancy and G1GC in Apache HBase
HBaseCon
 
PPTX
Real-time Fluid Simulation in Shadow of the Tomb Raider
Eidos-Montréal
 
PDF
Kubernetes Jobによるバッチシステムのリソース最適化 / AbemaTV DevCon 2018 TrackB Session B6
AbemaTV, Inc.
 
PDF
"Работа с утечками в V8", Роман Кривцов, MoscowJS 19
MoscowJS
 
PPTX
SPARQLstream and Morph-streams
Jean-Paul Calbimonte
 
PDF
Parallel Random Generator - GDC 2015
Manchor Ko
 
PDF
Lab 12 08_15
Hao Wu
 
[Jbcn 2016] Garbage Collectors WTF!?
Alonso Torres
 
Java. Есть ли свет в конце тоннеля
Alexey Demin
 
FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...
Rob Skillington
 
Using R in remote computer clusters
Burak Himmetoglu
 
Understanding JVM GC: advanced!
Jean-Philippe BEMPEL
 
Taming Java Garbage Collector
Daya Atapattu
 
Hotspot gc
Michał Warecki
 
Debugging and Profiling Rails Application
David Paluy
 
Understanding low latency jvm gcs V2
Jean-Philippe BEMPEL
 
Understanding jvm gc advanced
Jean-Philippe BEMPEL
 
Understanding low latency jvm gcs
Jean-Philippe BEMPEL
 
Solving Multi-tenancy and G1GC in Apache HBase
HBaseCon
 
Real-time Fluid Simulation in Shadow of the Tomb Raider
Eidos-Montréal
 
Kubernetes Jobによるバッチシステムのリソース最適化 / AbemaTV DevCon 2018 TrackB Session B6
AbemaTV, Inc.
 
"Работа с утечками в V8", Роман Кривцов, MoscowJS 19
MoscowJS
 
SPARQLstream and Morph-streams
Jean-Paul Calbimonte
 
Parallel Random Generator - GDC 2015
Manchor Ko
 
Lab 12 08_15
Hao Wu
 

Viewers also liked (7)

PDF
Hackathon - building and extending OpenJDK
Michał Warecki
 
PDF
About garbage collection
NAVER / MusicPlatform
 
ODP
Apache SolrCloud
Michał Warecki
 
ODP
Java memory model
Michał Warecki
 
PPTX
Java GC
Ray Cheng
 
PPT
Basic Garbage Collection Techniques
An Khuong
 
PDF
[D2]java 성능에 대한 오해와 편견
NAVER D2
 
Hackathon - building and extending OpenJDK
Michał Warecki
 
About garbage collection
NAVER / MusicPlatform
 
Apache SolrCloud
Michał Warecki
 
Java memory model
Michał Warecki
 
Java GC
Ray Cheng
 
Basic Garbage Collection Techniques
An Khuong
 
[D2]java 성능에 대한 오해와 편견
NAVER D2
 
Ad

Similar to Gc algorithms (20)

PPTX
JVM Magic
Baruch Sadogursky
 
PPTX
Java memory problem cases solutions
bluedavy lin
 
PPTX
Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...
Monica Beckwith
 
ODP
Java GC, Off-heap workshop
Valerii Moisieienko
 
PPTX
Progress_190130
Hyo jeong Lee
 
PDF
Understanding Garbage Collection
Doug Hawkins
 
PDF
Understanding Garbage Collection Using Automatic Memory Management
zuluJDK
 
PPTX
Java 어플리케이션 성능튜닝 Part1
상욱 송
 
PDF
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
PDF
淺談 Java GC 原理、調教和 新發展
Leon Chen
 
PPT
Performance tuning jvm
Prem Kuppumani
 
PPTX
G1 Garbage Collector - Big Heaps and Low Pauses?
C2B2 Consulting
 
PDF
Low pause GC in HotSpot
jClarity
 
PDF
ZGC-SnowOne.pdf
Monica Beckwith
 
PDF
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
Haim Yadid
 
PDF
Tuning the g1gc
Kirk Pepperdine
 
PDF
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
NodejsFoundation
 
PDF
Путь мониторинга 2.0 всё стало другим / Всеволод Поляков (Grammarly)
Ontico
 
PDF
Living With Garbage
Gregg Donovan
 
PPTX
Progress_190118
Hyo jeong Lee
 
Java memory problem cases solutions
bluedavy lin
 
Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...
Monica Beckwith
 
Java GC, Off-heap workshop
Valerii Moisieienko
 
Progress_190130
Hyo jeong Lee
 
Understanding Garbage Collection
Doug Hawkins
 
Understanding Garbage Collection Using Automatic Memory Management
zuluJDK
 
Java 어플리케이션 성능튜닝 Part1
상욱 송
 
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
淺談 Java GC 原理、調教和 新發展
Leon Chen
 
Performance tuning jvm
Prem Kuppumani
 
G1 Garbage Collector - Big Heaps and Low Pauses?
C2B2 Consulting
 
Low pause GC in HotSpot
jClarity
 
ZGC-SnowOne.pdf
Monica Beckwith
 
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
Haim Yadid
 
Tuning the g1gc
Kirk Pepperdine
 
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
NodejsFoundation
 
Путь мониторинга 2.0 всё стало другим / Всеволод Поляков (Grammarly)
Ontico
 
Living With Garbage
Gregg Donovan
 
Progress_190118
Hyo jeong Lee
 
Ad

Recently uploaded (20)

PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
PDF
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
PDF
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
PPTX
Talbott's brief History of Computers for CollabDays Hamburg 2025
Talbott Crowell
 
PDF
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Linux schedulers for fun and profit with SchedKit
Alessio Biancalana
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PPTX
Essential Content-centric Plugins for your Website
Laura Byrne
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
[GDGoC FPTU] Spring 2025 Summary Slidess
minhtrietgect
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
PPTX
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
Talbott's brief History of Computers for CollabDays Hamburg 2025
Talbott Crowell
 
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Linux schedulers for fun and profit with SchedKit
Alessio Biancalana
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Essential Content-centric Plugins for your Website
Laura Byrne
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
[GDGoC FPTU] Spring 2025 Summary Slidess
minhtrietgect
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 

Gc algorithms