SlideShare une entreprise Scribd logo
1  sur  14
Garbage Collection Algorithms By Achinth Anand Gurkhi
What is Garbage Collection? Garbage Collection is the process by which unused objects are deleted to reclaim memory space Invented by John McCarthy around 1959 to solve problems in Lisp It is used in Lisp, Smalltalk, Eiffel, Haskell, ML, Schema, Modula-3, Java and .NET
GC Algorithms Reference Counting Mark-Sweep Collector Copying Collector Mark-Compact Collector Generational Collector
Reference Counting Needs help from compiler and the program to maintain a reference count Compiler adds code to increment/decrement reference count when the object is referenced/dereferenced If reference count is zero it is garbage collected and reference count of all objects it references is decremented by one Used by ANSI C++ library classes like string
Reference Counting Advantages:  Garbage collection can be immediate Disadvantages: Cannot handle cyclic references    Need extra memory for the reference counter Incrementing and decrementing reference counts every time a reference is created or destroyed can significantly impede performance. Example: array processing
Mark-Sweep Collector All application threads are stopped Mark: from the roots (objects referenced directly) every referenced object is visited and marked Sweep: entire heap is scanned and all unmarked objects are collected. Next all marked objects are reset
Mark-Sweep Collector Advantages: Can handle cyclic references No burden on the compiler or the application Disadvantages As entire heap is scanned, pauses would be longer If heap is paged can have performance issues Causes heap fragmentation which could lead of out of memory errors
Copying Collector The heap is divided into two equal spaces One contains active data and the other is inactive When the active half gets full, the world is stopped, live objects are moved to the inactive half and then the active half is cleared For the next cycle, roles are reversed, the inactive half becomes the active half
Copying Collector Advantages: Only live objects are visited, garbage objects are not visited Data compaction is achieved which reduces cost of object allocation and out of memory errors Disadvantages: Requires twice the heap size than other collectors Overhead of copying objects from one space to another Overhead of adjusting all references to the new copy Long lived objects are copied back and forth on every collection
Mark-Compact Collector All application threads are stopped Mark: from the roots (objects referenced directly) every referenced object is visited and marked Compact: entire heap is scanned and all unmarked objects are collected. Next all marked objects compacted at the bottom of the heap and then the flags are reset
Mark-Compact Collector Advantages: Compaction is achieved  Without the hassle of long lived objects being copied back and forth Without the need for double the heap size  Disadvantages Overhead of copying objects (for compaction) Overhead of adjusting all references to the new copy
Generational Collector 98% of the objects die young  Copying Collectors perform well with short-lived objects Mark-Compact Collectors perform well with long-lived objects Can we use different GC algorithms based on object’s age?
Generational Collector The heap is divided into generations (usually 2 or 3) Objects are created in the young generation (gen 0) When memory is needed, a GC of gen 0 is performed & live objects are moved to the next generation (gen 1) & dead objects are collected (Copying Collector). If enough memory is now available GC is stopped Else the next older generation (gen 1) is collected. This goes on till enough memory is released or till the last (oldest) generation is reached.  The last generation (old objects) uses Mark-Compact Collector algorithm
Generational Collector Advantages: GC cycles are small as all objects are not collected Only old objects are copied from one generation to another Entire heap is not scanned Disadvantages Overhead of copying objects (for compaction) Overhead of adjusting all references to the new copy

Contenu connexe

Tendances

Multithreading computer architecture
 Multithreading computer architecture  Multithreading computer architecture
Multithreading computer architecture
Haris456
 
Mach Kernel
Mach KernelMach Kernel
Mach Kernel
Arif A.
 
Group Communication (Distributed computing)
Group Communication (Distributed computing)Group Communication (Distributed computing)
Group Communication (Distributed computing)
Sri Prasanna
 

Tendances (20)

Multithreading computer architecture
 Multithreading computer architecture  Multithreading computer architecture
Multithreading computer architecture
 
Mobile Network Layer
Mobile Network LayerMobile Network Layer
Mobile Network Layer
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
Distributed file system
Distributed file systemDistributed file system
Distributed file system
 
Three Address code
Three Address code Three Address code
Three Address code
 
Small computing & Mobile Computing
Small computing & Mobile ComputingSmall computing & Mobile Computing
Small computing & Mobile Computing
 
jstl ( jsp standard tag library )
jstl ( jsp standard tag library )jstl ( jsp standard tag library )
jstl ( jsp standard tag library )
 
Mach Kernel
Mach KernelMach Kernel
Mach Kernel
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
Group Communication (Distributed computing)
Group Communication (Distributed computing)Group Communication (Distributed computing)
Group Communication (Distributed computing)
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
SNMP
SNMPSNMP
SNMP
 
Android Memory Management
Android Memory ManagementAndroid Memory Management
Android Memory Management
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Remote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemRemote Procedure Call in Distributed System
Remote Procedure Call in Distributed System
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Swap-space Management
Swap-space ManagementSwap-space Management
Swap-space Management
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 

Similaire à Garbage collection algorithms

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
 
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
 

Similaire à Garbage collection algorithms (20)

Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
.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
 
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)
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Garbage collector complete information
Garbage collector complete informationGarbage collector complete information
Garbage collector complete information
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
 
Gc in android
Gc in androidGc in android
Gc in android
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
An introduction to G1 collector for busy developers
An introduction to G1 collector for busy developersAn introduction to G1 collector for busy developers
An introduction to G1 collector for busy developers
 
Learn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionLearn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage Collection
 
Garbage First & You
Garbage First & YouGarbage First & You
Garbage First & You
 
Garbage First and you
Garbage First and youGarbage First and you
Garbage First and you
 
Garbage First and You!
Garbage First and You!Garbage First and You!
Garbage First and You!
 
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
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Ruby memory tips and tricks
Ruby memory tips and tricksRuby memory tips and tricks
Ruby memory tips and tricks
 

Dernier

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
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

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
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 

Garbage collection algorithms

  • 1. Garbage Collection Algorithms By Achinth Anand Gurkhi
  • 2. What is Garbage Collection? Garbage Collection is the process by which unused objects are deleted to reclaim memory space Invented by John McCarthy around 1959 to solve problems in Lisp It is used in Lisp, Smalltalk, Eiffel, Haskell, ML, Schema, Modula-3, Java and .NET
  • 3. GC Algorithms Reference Counting Mark-Sweep Collector Copying Collector Mark-Compact Collector Generational Collector
  • 4. Reference Counting Needs help from compiler and the program to maintain a reference count Compiler adds code to increment/decrement reference count when the object is referenced/dereferenced If reference count is zero it is garbage collected and reference count of all objects it references is decremented by one Used by ANSI C++ library classes like string
  • 5. Reference Counting Advantages: Garbage collection can be immediate Disadvantages: Cannot handle cyclic references Need extra memory for the reference counter Incrementing and decrementing reference counts every time a reference is created or destroyed can significantly impede performance. Example: array processing
  • 6. Mark-Sweep Collector All application threads are stopped Mark: from the roots (objects referenced directly) every referenced object is visited and marked Sweep: entire heap is scanned and all unmarked objects are collected. Next all marked objects are reset
  • 7. Mark-Sweep Collector Advantages: Can handle cyclic references No burden on the compiler or the application Disadvantages As entire heap is scanned, pauses would be longer If heap is paged can have performance issues Causes heap fragmentation which could lead of out of memory errors
  • 8. Copying Collector The heap is divided into two equal spaces One contains active data and the other is inactive When the active half gets full, the world is stopped, live objects are moved to the inactive half and then the active half is cleared For the next cycle, roles are reversed, the inactive half becomes the active half
  • 9. Copying Collector Advantages: Only live objects are visited, garbage objects are not visited Data compaction is achieved which reduces cost of object allocation and out of memory errors Disadvantages: Requires twice the heap size than other collectors Overhead of copying objects from one space to another Overhead of adjusting all references to the new copy Long lived objects are copied back and forth on every collection
  • 10. Mark-Compact Collector All application threads are stopped Mark: from the roots (objects referenced directly) every referenced object is visited and marked Compact: entire heap is scanned and all unmarked objects are collected. Next all marked objects compacted at the bottom of the heap and then the flags are reset
  • 11. Mark-Compact Collector Advantages: Compaction is achieved Without the hassle of long lived objects being copied back and forth Without the need for double the heap size Disadvantages Overhead of copying objects (for compaction) Overhead of adjusting all references to the new copy
  • 12. Generational Collector 98% of the objects die young Copying Collectors perform well with short-lived objects Mark-Compact Collectors perform well with long-lived objects Can we use different GC algorithms based on object’s age?
  • 13. Generational Collector The heap is divided into generations (usually 2 or 3) Objects are created in the young generation (gen 0) When memory is needed, a GC of gen 0 is performed & live objects are moved to the next generation (gen 1) & dead objects are collected (Copying Collector). If enough memory is now available GC is stopped Else the next older generation (gen 1) is collected. This goes on till enough memory is released or till the last (oldest) generation is reached. The last generation (old objects) uses Mark-Compact Collector algorithm
  • 14. Generational Collector Advantages: GC cycles are small as all objects are not collected Only old objects are copied from one generation to another Entire heap is not scanned Disadvantages Overhead of copying objects (for compaction) Overhead of adjusting all references to the new copy