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

Garbage Collection

  • 2.
    The Purpose ofGarbage 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. UniprocessorGarbage 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