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

La actualidad más candente (20)

Data structures using c
Data structures using cData structures using c
Data structures using c
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Client Side Technologies
Client Side TechnologiesClient Side Technologies
Client Side Technologies
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
Features of java
Features of javaFeatures of java
Features of java
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
C vs c++
C vs c++C vs c++
C vs c++
 
Distributed and clustered systems
Distributed and clustered systemsDistributed and clustered systems
Distributed and clustered systems
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Java package
Java packageJava package
Java package
 
Semaphores
SemaphoresSemaphores
Semaphores
 
STL in C++
STL in C++STL in C++
STL in C++
 
Run time storage
Run time storageRun time storage
Run time storage
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Multiprocessor Systems
Multiprocessor SystemsMultiprocessor Systems
Multiprocessor Systems
 

Similar a Garbage collection algorithms

Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
An Khuong
 
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)

Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
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
 
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

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
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
 

Último (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 
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)
 
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
 
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
 
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...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 

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