SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

Inter process communication
Inter process communicationInter process communication
Inter process communication
Mohd Tousif
 

La actualidad más candente (20)

Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
ELEMENTS OF TRANSPORT PROTOCOL
ELEMENTS OF TRANSPORT PROTOCOLELEMENTS OF TRANSPORT PROTOCOL
ELEMENTS OF TRANSPORT PROTOCOL
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Memory management
Memory managementMemory management
Memory management
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Segments in Graphics
Segments in GraphicsSegments in Graphics
Segments in Graphics
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Operating system Memory management
Operating system Memory management Operating system Memory management
Operating system Memory management
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Chess board problem(divide and conquer)
Chess board problem(divide and conquer)Chess board problem(divide and conquer)
Chess board problem(divide and conquer)
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
Corba concepts & corba architecture
Corba concepts & corba architectureCorba concepts & corba architecture
Corba concepts & corba architecture
 
Basic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler ConstructionBasic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler Construction
 
IPC
IPCIPC
IPC
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 

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

Similar a 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 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
 
Ruby memory tips and tricks
Ruby memory tips and tricksRuby memory tips and tricks
Ruby memory tips and tricks
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection
 

Último

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

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
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
 
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...
 
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 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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...
 
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...
 
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
 
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
 
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
 

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