SlideShare une entreprise Scribd logo
1  sur  58
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!
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?

Contenu connexe

Tendances

Tendances (19)

[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?
 
Java. Есть ли свет в конце тоннеля
Java. Есть ли свет в конце тоннеляJava. Есть ли свет в конце тоннеля
Java. Есть ли свет в конце тоннеля
 
FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...
FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...
FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...
 
Using R in remote computer clusters
Using R in remote computer clustersUsing R in remote computer clusters
Using R in remote computer clusters
 
Understanding JVM GC: advanced!
Understanding JVM GC: advanced!Understanding JVM GC: advanced!
Understanding JVM GC: advanced!
 
Taming Java Garbage Collector
Taming Java Garbage CollectorTaming Java Garbage Collector
Taming Java Garbage Collector
 
Hotspot gc
Hotspot gcHotspot gc
Hotspot gc
 
Debugging and Profiling Rails Application
Debugging and Profiling Rails ApplicationDebugging and Profiling Rails Application
Debugging and Profiling Rails Application
 
Understanding low latency jvm gcs V2
Understanding low latency jvm gcs V2Understanding low latency jvm gcs V2
Understanding low latency jvm gcs V2
 
Understanding jvm gc advanced
Understanding jvm gc advancedUnderstanding jvm gc advanced
Understanding jvm gc advanced
 
gRPC
gRPCgRPC
gRPC
 
Understanding low latency jvm gcs
Understanding low latency jvm gcsUnderstanding low latency jvm gcs
Understanding low latency jvm gcs
 
Solving Multi-tenancy and G1GC in Apache HBase
Solving Multi-tenancy and G1GC in Apache HBase Solving Multi-tenancy and G1GC in Apache HBase
Solving Multi-tenancy and G1GC in Apache HBase
 
Real-time Fluid Simulation in Shadow of the Tomb Raider
Real-time Fluid Simulation in Shadow of the Tomb RaiderReal-time Fluid Simulation in Shadow of the Tomb Raider
Real-time Fluid Simulation in Shadow of the Tomb Raider
 
Kubernetes Jobによるバッチシステムのリソース最適化 / AbemaTV DevCon 2018 TrackB Session B6
Kubernetes Jobによるバッチシステムのリソース最適化 / AbemaTV DevCon 2018 TrackB Session B6Kubernetes Jobによるバッチシステムのリソース最適化 / AbemaTV DevCon 2018 TrackB Session B6
Kubernetes Jobによるバッチシステムのリソース最適化 / AbemaTV DevCon 2018 TrackB Session B6
 
"Работа с утечками в V8", Роман Кривцов, MoscowJS 19
"Работа с утечками в V8", Роман Кривцов, MoscowJS 19"Работа с утечками в V8", Роман Кривцов, MoscowJS 19
"Работа с утечками в V8", Роман Кривцов, MoscowJS 19
 
SPARQLstream and Morph-streams
SPARQLstream and Morph-streamsSPARQLstream and Morph-streams
SPARQLstream and Morph-streams
 
Parallel Random Generator - GDC 2015
Parallel Random Generator - GDC 2015Parallel Random Generator - GDC 2015
Parallel Random Generator - GDC 2015
 
Lab 12 08_15
Lab 12 08_15Lab 12 08_15
Lab 12 08_15
 

En vedette (7)

Hackathon - building and extending OpenJDK
Hackathon - building and extending OpenJDKHackathon - building and extending OpenJDK
Hackathon - building and extending OpenJDK
 
About garbage collection
About garbage collectionAbout garbage collection
About garbage collection
 
Apache SolrCloud
Apache SolrCloudApache SolrCloud
Apache SolrCloud
 
Java memory model
Java memory modelJava memory model
Java memory model
 
Java GC
Java GCJava GC
Java GC
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
[D2]java 성능에 대한 오해와 편견
[D2]java 성능에 대한 오해와 편견[D2]java 성능에 대한 오해와 편견
[D2]java 성능에 대한 오해와 편견
 

Similaire à Gc algorithms

Similaire à Gc algorithms (20)

JVM Magic
JVM MagicJVM Magic
JVM Magic
 
Java memory problem cases solutions
Java memory problem cases solutionsJava memory problem cases solutions
Java memory problem cases solutions
 
Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...
Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...
Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...
 
Java GC, Off-heap workshop
Java GC, Off-heap workshopJava GC, Off-heap workshop
Java GC, Off-heap workshop
 
Progress_190130
Progress_190130Progress_190130
Progress_190130
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage Collection
 
Understanding Garbage Collection Using Automatic Memory Management
Understanding Garbage Collection Using Automatic Memory ManagementUnderstanding Garbage Collection Using Automatic Memory Management
Understanding Garbage Collection Using Automatic Memory Management
 
Java 어플리케이션 성능튜닝 Part1
Java 어플리케이션 성능튜닝 Part1Java 어플리케이션 성능튜닝 Part1
Java 어플리케이션 성능튜닝 Part1
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvm
 
G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?
 
Low pause GC in HotSpot
Low pause GC in HotSpotLow pause GC in HotSpot
Low pause GC in HotSpot
 
ZGC-SnowOne.pdf
ZGC-SnowOne.pdfZGC-SnowOne.pdf
ZGC-SnowOne.pdf
 
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
 
Tuning the g1gc
Tuning the g1gcTuning the g1gc
Tuning the g1gc
 
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
 
Путь мониторинга 2.0 всё стало другим / Всеволод Поляков (Grammarly)
Путь мониторинга 2.0 всё стало другим / Всеволод Поляков (Grammarly)Путь мониторинга 2.0 всё стало другим / Всеволод Поляков (Grammarly)
Путь мониторинга 2.0 всё стало другим / Всеволод Поляков (Grammarly)
 
Living With Garbage
Living With GarbageLiving With Garbage
Living With Garbage
 
Progress_190118
Progress_190118Progress_190118
Progress_190118
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Gc algorithms