SlideShare a Scribd company logo
1 of 39
[email_address] Basic Garbage Collection Techniques
Contents ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Tracing
1. Garbage Collection ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Explicit Storage Management Hazards ,[object Object],[object Object],[object Object],[object Object],[object Object]
Explicit Storage Management Hazards ,[object Object],[object Object],[object Object],[object Object]
GC Complexity ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Garbage Collection – The Two-Phase Abstraction ,[object Object],[object Object],[object Object],[object Object]
2. Reference Counting ,[object Object],[object Object],[object Object]
Reference Counting ,[object Object],[object Object],[object Object]
The Cycles Problem ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Efficiency Problem ,[object Object],[object Object],[object Object]
Deferred Reference Counting ,[object Object],[object Object],[object Object],[object Object]
Reference Counting - Recap ,[object Object],[object Object],[object Object],[object Object]
3. Mark-Sweep Collection  - [McCarthy 1960] ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example Mark phase Sweep phase Before Heap root root root Free_list
Example p p Live object Marked object Garbage Free list r1 Free list r1 Free list r1
Basic Algorithm New (A)= if free_list is empty mark_sweep() if free_list is empty return (“out-of-memory”) pointer = allocate(A) return (pointer) mark_sweep ()=  for Ptr in Roots mark(Ptr) sweep() mark (Obj)= if mark_bit(Obj) == unmarked mark_bit(Obj)=marked for C in Children(Obj) mark(C) Sweep ()= p = Heap_bottom while (p < Heap_top) if (mark_bit(p) == unmarked) then free(p) else mark_bit(p) = unmarked; p=p+size(p)
Properties of Mark & Sweep ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Standard Engineering Techniques ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
4. Mark-Compact Collection Compact after mark ,[object Object],[object Object],[object Object]
Mark-Compact Collection ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Object Ordering ,[object Object],[object Object],[object Object],1 2 3 4 Linearizing 4 1 2 3 Arbitrary Worst 1 3 4 2 Good 1 2 3 4 Sliding Best
The Two Finger Algorithm  [Edwards 1974] ,[object Object],[object Object],[object Object],[object Object]
Pass I: Compact ,[object Object],[object Object],[object Object],[object Object],[object Object]
Two finger, pass I - Example
Pass II: Fix Pointers ,[object Object],[object Object],[object Object],Compacted area free area
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Two finger –Properties
The Lisp2 Algorithm ,[object Object],[object Object],[object Object],[object Object],Free
The Lisp2 Algorithm ,[object Object],[object Object],[object Object],Free
Lisp 2 –Properties ,[object Object],[object Object],[object Object],Slower: 3 passes.  Extra space required –a pointer per object.
5. Copying Garbage Collection ,[object Object],[object Object],[object Object]
Stop-and-Copy Collector ,[object Object],[object Object],[object Object]
Cheney breath-first copying
The algorithm Init()= Tospace=Heap_bottom space_size=Heap_size/2 top_of_space=Tospace+space_size Fromspace=top_of_space+1 free=Tospace New(n)= If free+n>top_of_space Collect() if free+n>top_of_space abort“Memoryexhausted” new-object=free free=free+n return(new-object) collect()= from-space,to-space= to-space,from-space//swap scan=free=Tospace top_of_space=Tospace+space_size forRinRoots R=copy(R) whilescan<free forPinchildren(scan) *P=copy(P) scan=scan+size(scan) copy(P)= ifforwarded(P) returnforwarding_address(P) else addr=free mem-copy(P,free) free=free+size(P) forwarding_address(P)=addr return(addr) 1 2 4 3
Efficiency of Copying Collection ,[object Object],[object Object],[object Object],[object Object]
6. Non-Copying Implicit Collection ,[object Object],[object Object],[object Object],[object Object],[object Object]
7. Choosing Among Basic Tracing Techniques ,[object Object],[object Object],[object Object],[object Object],[object Object]
8. Problems with Simple Tracing Collectors
9. Conservatism in GC  ,[object Object],[object Object],[object Object]

More Related Content

What's hot

Virtual memory
Virtual memoryVirtual memory
Virtual memory
Anuj Modi
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local names
Varsha Kumar
 
Dynamic storage allocation techniques
Dynamic storage allocation techniquesDynamic storage allocation techniques
Dynamic storage allocation techniques
Shashwat Shriparv
 
Cache memory
Cache memoryCache memory
Cache memory
Anuj Modi
 
Free space managment46
Free space managment46Free space managment46
Free space managment46
myrajendra
 

What's hot (20)

Address mapping
Address mappingAddress mapping
Address mapping
 
8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems
 
Dynamic storage allocation techniques in Compiler design
Dynamic storage allocation techniques in Compiler designDynamic storage allocation techniques in Compiler design
Dynamic storage allocation techniques in Compiler design
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Distributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock DetectionDistributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock Detection
 
Network Layer design Issues.pptx
Network Layer design Issues.pptxNetwork Layer design Issues.pptx
Network Layer design Issues.pptx
 
Leaky Bucket & Tocken Bucket - Traffic shaping
Leaky Bucket & Tocken Bucket - Traffic shapingLeaky Bucket & Tocken Bucket - Traffic shaping
Leaky Bucket & Tocken Bucket - Traffic shaping
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local names
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Three Address code
Three Address code Three Address code
Three Address code
 
Dynamic storage allocation techniques
Dynamic storage allocation techniquesDynamic storage allocation techniques
Dynamic storage allocation techniques
 
Cache memory
Cache memoryCache memory
Cache memory
 
contiguous memory allocation.pptx
contiguous memory allocation.pptxcontiguous memory allocation.pptx
contiguous memory allocation.pptx
 
Page Replacement
Page ReplacementPage Replacement
Page Replacement
 
Free space managment46
Free space managment46Free space managment46
Free space managment46
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 

Similar to Basic Garbage Collection Techniques

Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
Carol McDonald
 
How to empower community by using GIS lecture 2
How to empower community by using GIS lecture 2How to empower community by using GIS lecture 2
How to empower community by using GIS lecture 2
wang yaohui
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
Wednesday Solutions
 

Similar to Basic Garbage Collection Techniques (20)

Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
I phone arc
I phone arcI phone arc
I phone arc
 
Gc in android
Gc in androidGc in android
Gc in android
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
 
Garbage collector complete information
Garbage collector complete informationGarbage collector complete information
Garbage collector complete information
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
 
Unit 5
Unit 5Unit 5
Unit 5
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
Chapter 7 Run Time Environment
Chapter 7   Run Time EnvironmentChapter 7   Run Time Environment
Chapter 7 Run Time Environment
 
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Stacks
StacksStacks
Stacks
 
Ruby memory tips and tricks
Ruby memory tips and tricksRuby memory tips and tricks
Ruby memory tips and tricks
 
Memory management
Memory managementMemory management
Memory management
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
How to empower community by using GIS lecture 2
How to empower community by using GIS lecture 2How to empower community by using GIS lecture 2
How to empower community by using GIS lecture 2
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Basic Garbage Collection Techniques

  • 1. [email_address] Basic Garbage Collection Techniques
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. Example Mark phase Sweep phase Before Heap root root root Free_list
  • 16. Example p p Live object Marked object Garbage Free list r1 Free list r1 Free list r1
  • 17. Basic Algorithm New (A)= if free_list is empty mark_sweep() if free_list is empty return (“out-of-memory”) pointer = allocate(A) return (pointer) mark_sweep ()= for Ptr in Roots mark(Ptr) sweep() mark (Obj)= if mark_bit(Obj) == unmarked mark_bit(Obj)=marked for C in Children(Obj) mark(C) Sweep ()= p = Heap_bottom while (p < Heap_top) if (mark_bit(p) == unmarked) then free(p) else mark_bit(p) = unmarked; p=p+size(p)
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. Two finger, pass I - Example
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 34. The algorithm Init()= Tospace=Heap_bottom space_size=Heap_size/2 top_of_space=Tospace+space_size Fromspace=top_of_space+1 free=Tospace New(n)= If free+n>top_of_space Collect() if free+n>top_of_space abort“Memoryexhausted” new-object=free free=free+n return(new-object) collect()= from-space,to-space= to-space,from-space//swap scan=free=Tospace top_of_space=Tospace+space_size forRinRoots R=copy(R) whilescan<free forPinchildren(scan) *P=copy(P) scan=scan+size(scan) copy(P)= ifforwarded(P) returnforwarding_address(P) else addr=free mem-copy(P,free) free=free+size(P) forwarding_address(P)=addr return(addr) 1 2 4 3
  • 35.
  • 36.
  • 37.
  • 38. 8. Problems with Simple Tracing Collectors
  • 39.

Editor's Notes

  1. Ưu điểm: Ko cần cấp thêm vùng nhớ để lưu trữ trường count field Các object ko cần di chuyển trong quá trình GC Loại bỏ được rác trong trường hợp có tham chiếu vòng (cyclic structures) Nhược điểm: Giới hạn giá trị lớn nhất có thể của ô nhớ (integer) Tăng khả năng làm phân mảnh vùng nhớ Chương trình phải tạm dừng khi GC thực thi Giai đoạn sweep tốn chi phí cao(thời gian)
  2. Ưu điểm: Giảm khả năng phân mảnh vùng nhớ heap. Thời gian thực hiện công việc thì tỉ lệ với các live object Có thể xử lý được rác chứa các tham chiếu vòng Không tốn bộ nhớ để lưu trường count field Nhược điểm: Vùng nhớ dùng cho chương trình bị chia đôi(fromspace – tospace) Các object phải di chuyển trong vùng nhớ trong suốt quá trình thu gom rác, do vậy các tham chiếu đến các đối tượng cũng phải cập nhật. Chương trình phải tạm dừng khi bộ thu gom rác thực thi.