SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler offline
The Purpose of Garbage Collection




     Manual memory management is too tiresome and error-prone.
            Memory leaks.
            Dangling pointers.
     GC frees programmer to focus on more important issues.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   2/9
A Few Definitions




     Aim: to free objects that will not be used anymore.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   3/9
A Few Definitions




     Aim: to free objects that will not be used anymore.
     That is computationally impossible.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   3/9
A Few Definitions




     Aim: to free objects that will not be used anymore.
     That is computationally impossible.
     Reachability: transitive closure of pointers starting with the root set
     (all global and local variables).




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   3/9
Reference Counting




     Each object has a counter.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   4/9
Reference Counting




     Each object has a counter.
     Reference assignment → decrease the counter of the old object and
     increase the counter of the new one (if they exist).




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   4/9
Reference Counting




     Each object has a counter.
     Reference assignment → decrease the counter of the old object and
     increase the counter of the new one (if they exist).
     When the counter reaches zero, free the object.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   4/9
Reference Counting




     Each object has a counter.
     Reference assignment → decrease the counter of the old object and
     increase the counter of the new one (if they exist).
     When the counter reaches zero, free the object.
     Disadvantages: fairly big overhead, can not deal with cycles.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   4/9
Mark and Sweep




     Each object has a mark.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   5/9
Mark and Sweep




     Each object has a mark.
     Once in a while, go through transitive closure of the root set and
     mark all objects along the way.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   5/9
Mark and Sweep




     Each object has a mark.
     Once in a while, go through transitive closure of the root set and
     mark all objects along the way.
     Then free all unmarked objects.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   5/9
Mark and Sweep




     Each object has a mark.
     Once in a while, go through transitive closure of the root set and
     mark all objects along the way.
     Then free all unmarked objects.
     Disadvantages: fragmentation, locality of reference, cost is
     proportional to the size of available memory.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   5/9
Mark and Compact, Mark and Copy




     The same as mark and sweep except objects are moved (copied) so
     that they are next to each other.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   6/9
Mark and Compact, Mark and Copy




     The same as mark and sweep except objects are moved (copied) so
     that they are next to each other.
     Disadvantages: costly when there is a large number of survivors.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   6/9
Incremental Collectors




      Stop the world algorithms disrupt the execution of the program for
      too long.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   7/9
Incremental Collectors




      Stop the world algorithms disrupt the execution of the program for
      too long.
      It is better to collect garbage by increments.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   7/9
Incremental Collectors




      Stop the world algorithms disrupt the execution of the program for
      too long.
      It is better to collect garbage by increments.
      However, incremental algorithms must accommodate for possible
      changes in the object graph.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   7/9
Incremental Collectors




      Stop the world algorithms disrupt the execution of the program for
      too long.
      It is better to collect garbage by increments.
      However, incremental algorithms must accommodate for possible
      changes in the object graph.
      Two basic ways: read barrier and write barrier.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   7/9
Generational Collectors




      Most objects live a very short time.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   8/9
Generational Collectors




      Most objects live a very short time.
      GC among younglings has much higher efficiency—i. e. it lasts
      shorter and yields more free space.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   8/9
Generational Collectors




      Most objects live a very short time.
      GC among younglings has much higher efficiency—i. e. it lasts
      shorter and yields more free space.
      Therefore, the whole memory is divided into regions, objects are
      propagated through regions based on their age.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   8/9
Generational Collectors




      Most objects live a very short time.
      GC among younglings has much higher efficiency—i. e. it lasts
      shorter and yields more free space.
      Therefore, the whole memory is divided into regions, objects are
      propagated through regions based on their age.
      The root set for a given region has to include references from regions
      with more mature objects.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   8/9
See




Paul Wilson. Uniprocessor Garbage Collection Techniques. Memory
Management. http://dx.doi.org/10.1007/BFb0017182




 Michal P´se (CTU in Prague)
         ıˇ                    Object Prgrmmng L. 11: Garbage Collection   December 7, 2010   9/9

Mais conteúdo relacionado

Semelhante a Garbage Collection

Semelhante a Garbage Collection (6)

Prototype Languages
Prototype LanguagesPrototype Languages
Prototype Languages
 
Functional Concepts
Functional ConceptsFunctional Concepts
Functional Concepts
 
Multiple Dispatch
Multiple DispatchMultiple Dispatch
Multiple Dispatch
 
Reclassification
ReclassificationReclassification
Reclassification
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Reflection and Metadata
Reflection and MetadataReflection and Metadata
Reflection and Metadata
 

Último

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Último (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Garbage Collection

  • 1.
  • 2. The Purpose of Garbage Collection Manual memory management is too tiresome and error-prone. Memory leaks. Dangling pointers. GC frees programmer to focus on more important issues. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 2/9
  • 3. A Few Definitions Aim: to free objects that will not be used anymore. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3/9
  • 4. A Few Definitions Aim: to free objects that will not be used anymore. That is computationally impossible. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3/9
  • 5. A Few Definitions Aim: to free objects that will not be used anymore. That is computationally impossible. Reachability: transitive closure of pointers starting with the root set (all global and local variables). Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3/9
  • 6. Reference Counting Each object has a counter. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4/9
  • 7. Reference Counting Each object has a counter. Reference assignment → decrease the counter of the old object and increase the counter of the new one (if they exist). Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4/9
  • 8. Reference Counting Each object has a counter. Reference assignment → decrease the counter of the old object and increase the counter of the new one (if they exist). When the counter reaches zero, free the object. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4/9
  • 9. Reference Counting Each object has a counter. Reference assignment → decrease the counter of the old object and increase the counter of the new one (if they exist). When the counter reaches zero, free the object. Disadvantages: fairly big overhead, can not deal with cycles. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4/9
  • 10. Mark and Sweep Each object has a mark. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5/9
  • 11. Mark and Sweep Each object has a mark. Once in a while, go through transitive closure of the root set and mark all objects along the way. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5/9
  • 12. Mark and Sweep Each object has a mark. Once in a while, go through transitive closure of the root set and mark all objects along the way. Then free all unmarked objects. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5/9
  • 13. Mark and Sweep Each object has a mark. Once in a while, go through transitive closure of the root set and mark all objects along the way. Then free all unmarked objects. Disadvantages: fragmentation, locality of reference, cost is proportional to the size of available memory. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5/9
  • 14. Mark and Compact, Mark and Copy The same as mark and sweep except objects are moved (copied) so that they are next to each other. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 6/9
  • 15. Mark and Compact, Mark and Copy The same as mark and sweep except objects are moved (copied) so that they are next to each other. Disadvantages: costly when there is a large number of survivors. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 6/9
  • 16. Incremental Collectors Stop the world algorithms disrupt the execution of the program for too long. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7/9
  • 17. Incremental Collectors Stop the world algorithms disrupt the execution of the program for too long. It is better to collect garbage by increments. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7/9
  • 18. Incremental Collectors Stop the world algorithms disrupt the execution of the program for too long. It is better to collect garbage by increments. However, incremental algorithms must accommodate for possible changes in the object graph. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7/9
  • 19. Incremental Collectors Stop the world algorithms disrupt the execution of the program for too long. It is better to collect garbage by increments. However, incremental algorithms must accommodate for possible changes in the object graph. Two basic ways: read barrier and write barrier. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7/9
  • 20. Generational Collectors Most objects live a very short time. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8/9
  • 21. Generational Collectors Most objects live a very short time. GC among younglings has much higher efficiency—i. e. it lasts shorter and yields more free space. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8/9
  • 22. Generational Collectors Most objects live a very short time. GC among younglings has much higher efficiency—i. e. it lasts shorter and yields more free space. Therefore, the whole memory is divided into regions, objects are propagated through regions based on their age. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8/9
  • 23. Generational Collectors Most objects live a very short time. GC among younglings has much higher efficiency—i. e. it lasts shorter and yields more free space. Therefore, the whole memory is divided into regions, objects are propagated through regions based on their age. The root set for a given region has to include references from regions with more mature objects. Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8/9
  • 24. See Paul Wilson. Uniprocessor Garbage Collection Techniques. Memory Management. http://dx.doi.org/10.1007/BFb0017182 Michal P´se (CTU in Prague) ıˇ Object Prgrmmng L. 11: Garbage Collection December 7, 2010 9/9