Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Similar documents
Garbage Collection. Hwansoo Han

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

CMSC 330: Organization of Programming Languages

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

CMSC 330: Organization of Programming Languages

Garbage Collection. Steven R. Bagley

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Garbage Collection (1)

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Garbage Collection Algorithms. Ganesh Bikshandi

Exploiting the Behavior of Generational Garbage Collector

Lecture 13: Garbage Collection

Run-Time Environments/Garbage Collection

Compiler Construction

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Compiler Construction

Programming Language Implementation

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Automatic Memory Management

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Garbage Collection Techniques

Lecture 15 Garbage Collection

CS 241 Honors Memory

Compiler Construction D7011E

Heap Management. Heap Allocation

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Garbage Collection. Weiyuan Li

Lecture Notes on Garbage Collection

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Robust Memory Management Schemes

Lecture Notes on Garbage Collection

Managed runtimes & garbage collection

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

Run-time Environments -Part 3

Reference Counting. Reference counting: a way to know whether a record has other users

CS 4120 Lecture 37 Memory Management 28 November 2011 Lecturer: Andrew Myers

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Habanero Extreme Scale Software Research Project

Reference Counting. Reference counting: a way to know whether a record has other users

Automatic Garbage Collection

Lecture 13: Complex Types and Garbage Collection

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Name, Scope, and Binding. Outline [1]

CS Computer Systems. Lecture 8: Free Memory Management

Dynamic Storage Allocation

CS61C : Machine Structures

CA341 - Comparative Programming Languages

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

CS61C : Machine Structures

CS61C : Machine Structures

Garbage Collection (2) Advanced Operating Systems Lecture 9

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Implementation Garbage Collection

G Programming Languages - Fall 2012

CS61C : Machine Structures

Advanced Programming & C++ Language

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Java Performance Tuning

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Lecture 15 Advanced Garbage Collection

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Garbage Collection. CS 351: Systems Programming Michael Saelee

Running class Timing on Java HotSpot VM, 1

The Generational Hypothesis

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Cycle Tracing. Presented by: Siddharth Tiwary

Garbage Collection. Lecture Compilers SS Dr.-Ing. Ina Schaefer. Software Technology Group TU Kaiserslautern. Ina Schaefer Garbage Collection 1

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Lecture Notes on Advanced Garbage Collection

High-Level Language VMs

Parallel GC. (Chapter 14) Eleanor Ainy December 16 th 2014

Opera&ng Systems CMPSCI 377 Garbage Collec&on. Emery Berger and Mark Corner University of Massachuse9s Amherst

Review. Partitioning: Divide heap, use different strategies per heap Generational GC: Partition by age Most objects die young

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

JVM Memory Model and GC

Compiler construction 2009

Concurrent Garbage Collection

Run-time Environments - 3

Pointers II. Class 31

Shenandoah An ultra-low pause time Garbage Collector for OpenJDK. Christine H. Flood Roman Kennke

Lecture 23: Object Lifetime and Garbage Collection

Quantifying the Performance of Garbage Collection vs. Explicit Memory Management

Algorithms for Dynamic Memory Management (236780) Lecture 1

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Analyzing Real-Time Systems

Dynamic Memory Allocation II October 22, 2008

Mark-Sweep and Mark-Compact GC

Dynamic Memory Management

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps

Garbage collection. The Old Way. Manual labor. JVM and other platforms. By: Timo Jantunen

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Dynamic Memory Management! Goals of this Lecture!

Transcription:

Garbage Collection Akim Demaille, Etienne Renault, Roland Levillain May 15, 2017 CCMP2 Garbage Collection May 15, 2017 1 / 35

Table of contents 1 Motivations and Definitions 2 Reference Counting Garbage Collection 3 Mark and Sweep Garbage Collection 4 Stop and Copy Garbage Collection 5 Hybrid Approaches CCMP2 Garbage Collection May 15, 2017 2 / 35

Garbage Collection 1/2 Fisrt apparition in LISP, 1959, McCarthy Garbage collection is the automatic reclamation of computer storage (heap) at runtime Automatic memory management New/malloc doesn t need delete/free anymore Necessary for fully modular programming. Otherwise some modules are responsible for allocation while others are responsible for deallocation. No more memory leaks Avoid dangling-pointers/references. Reclaiming memory too soon is no more possible CCMP2 Garbage Collection May 15, 2017 3 / 35

Garbage Collection 2/2 Quite expensive relative to explicit heap management Slow running programs down by (very roughly) 10 percent...... But sometime cheaper or competitive Fair comparison is difficult since explicit deallocation affects the structure of programs in ways that may themselves be expensive Possible reduction of heap fragmentation Functional and logic programming languages generally incorporate garbage collection because their unpredictable execution patterns D, Python, Caml, Effeil, Swift, C#, Go, Java, Haskell, LISP, Dylan, Prolog, etc. CCMP2 Garbage Collection May 15, 2017 4 / 35

What is Garbage? An object is called garbage at some point during execution if it will never be used again. What is garbage at the indicated points? int main () { Object x, y; x = new Object (); y = new Object (); /* Point A */ x. dosomething (); y. dosomething (); /* Point B */ y = new Object (); /* Point C */ CCMP2 Garbage Collection May 15, 2017 5 / 35

Approximating Garbage In general, it is undecidable whether an object is garbage An object is reachable if it can still be referenced by the program. Goals Detect and reclaim unreachable objects CCMP2 Garbage Collection May 15, 2017 6 / 35

Basics of a Garbage Collector 1 Distinguishing the live objects from the garbage ones 2 Reclaiming the garbage object storage CCMP2 Garbage Collection May 15, 2017 7 / 35

Basics of a Garbage Collector 1 Distinguishing the live objects from the garbage ones 2 Reclaiming the garbage object storage We focus on built-in garbage collectors so that: allocation routines performs special actions reclaim memory emit specific code to recognize object format etc. explicit calls to the deallocator are unnecessary the allocator will call it on-time the objects will be automatically destroyed CCMP2 Garbage Collection May 15, 2017 7 / 35

Different kind of GC Incremental techniques: allow garbage collection to proceed piecemeal while application is running my provide real-time garantees can be generalized into concurrent collections Generationnal Schemes improve efficiency/locality by garbage collecting a smaller area more often avoid overhead due to long time objects rely on pause to collect data CCMP2 Garbage Collection May 15, 2017 8 / 35

Table of contents 1 Motivations and Definitions 2 Reference Counting Garbage Collection 3 Mark and Sweep Garbage Collection 4 Stop and Copy Garbage Collection 5 Hybrid Approaches CCMP2 Garbage Collection May 15, 2017 9 / 35

Reference Counting Intuition CCMP2 Garbage Collection May 15, 2017 10 / 35

Reference Counting Intuition Maintain for each object a counter to the references to this object CCMP2 Garbage Collection May 15, 2017 10 / 35

Reference Counting Intuition Maintain for each object a counter to the references to this object Each time a reference to the object is created, increase the pointed-to object s counter CCMP2 Garbage Collection May 15, 2017 10 / 35

Reference Counting Intuition Maintain for each object a counter to the references to this object Each time a reference to the object is created, increase the pointed-to object s counter Each time an existing reference to an object is eliminated, the counter is decremented CCMP2 Garbage Collection May 15, 2017 10 / 35

Reference Counting Intuition Maintain for each object a counter to the references to this object Each time a reference to the object is created, increase the pointed-to object s counter Each time an existing reference to an object is eliminated, the counter is decremented When the object counter equals zero, the memory can be reclaimed CCMP2 Garbage Collection May 15, 2017 10 / 35

Deallocation Caution When an object is destructed: Transitive reclamation can be deferred by maintaining a list of freed objects CCMP2 Garbage Collection May 15, 2017 11 / 35

Deallocation Caution When an object is destructed: examines pointer fields Transitive reclamation can be deferred by maintaining a list of freed objects CCMP2 Garbage Collection May 15, 2017 11 / 35

Deallocation Caution When an object is destructed: examines pointer fields for any references R contained by this object, decrement reference counter of R Transitive reclamation can be deferred by maintaining a list of freed objects CCMP2 Garbage Collection May 15, 2017 11 / 35

Deallocation Caution When an object is destructed: examines pointer fields for any references R contained by this object, decrement reference counter of R If the reference counter of R becomes 0, reclaim memory Transitive reclamation can be deferred by maintaining a list of freed objects CCMP2 Garbage Collection May 15, 2017 11 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; head 1 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; head 1 mid 1 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head 1 mid 1 tail 1 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; head 1 mid 12 tail 1 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; head 1 mid 12 tail 12 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head 1 mid 12 tail 21 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head 1 mid 21 tail 21 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head 1 mid 21 tail 21 0 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head 1 mid 21 tail reclaimed 21 0 CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; head 10 mid 21 tail CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; head reclaimed 1 mid 21 tail CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; head mid 210 tail CCMP2 Garbage Collection May 15, 2017 12 / 35

Exemple class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; head mid reclaimed 21 tail CCMP2 Garbage Collection May 15, 2017 12 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; head 1 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; head 1 mid 1 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head 1 mid 1 tail 1 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; head 1 mid 12 tail 1 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; head 1 mid 12 tail 12 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; tail.next = head; head 12 mid 12 tail 12 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; tail.next = head; tail = null; head 12 mid 12 tail 21 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; tail.next = head; tail = null; mid = null; head 12 mid 21 tail 21 CCMP2 Garbage Collection May 15, 2017 13 / 35

What about cyclic references 1 If the objects create a directed cycle, the objects references counters will never reduced to zero. class LinkedList { LinkedList next = null ; int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList; head.next = mid; mid.next = tail; tail.next = head; tail = null; mid = null; head = null; head 21 mid 21 tail 21 CCMP2 Garbage Collection May 15, 2017 13 / 35

Pros and Cons Pros: Easy to implement: perl, Firefox Can be implemented on top of explicit memory management librairies (shared ptr) Interleaved with running time Small overage per unit of program execution Transitive reclamation can be deferred by maintaining a list of freed objects Real-time requierements: no halt of the system. Necessary for application where response-time is critical Cons: A whole machine word per object When the number of references to an object overflows, the counter is set to the maximum and the memory will never be reclaimed Problem with cycles Efficiency: cost relative to the running program CCMP2 Garbage Collection May 15, 2017 14 / 35

Table of contents 1 Motivations and Definitions 2 Reference Counting Garbage Collection 3 Mark and Sweep Garbage Collection 4 Stop and Copy Garbage Collection 5 Hybrid Approaches CCMP2 Garbage Collection May 15, 2017 15 / 35

Analysis Reference counting tries to find unreachable objects by finding objects without incoming references These references have been forgotten! CCMP2 Garbage Collection May 15, 2017 16 / 35

Analysis Reference counting tries to find unreachable objects by finding objects without incoming references These references have been forgotten! We have to trace the lifetime of objects CCMP2 Garbage Collection May 15, 2017 16 / 35

Intuition Given knowledge of what s immediately accessible, find everything reachable in the program The root set is the set of memory locations in the program that are known to be reachable Graph Problem Simply do a graph search starting at the root set: Any objects reachable from the root set are reachable Any objects not reachable from the root set are not reachable CCMP2 Garbage Collection May 15, 2017 17 / 35

How to obtain the root set? static reference variables references registered through librairies (JNI, for instance) For each threads: local variables current method(s) arguments stack etc. CCMP2 Garbage Collection May 15, 2017 18 / 35

Mark-and-Sweep: the Algorithm 1 Marking phase: Find reachable objects Add the root set to a worklist While the worklist isn t empty Remove an object from the worklist If it is not marked, mark it and add to the worklist all objects reachable from that object 2 Sweeping phase: Reclaim free memory If that object isn t marked, reclaim its memory If the object is marked, unmark it CCMP2 Garbage Collection May 15, 2017 19 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-01 object-04 object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-01 object-04 object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-05 object-02 object-04 object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-05 object-02 object-04 object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-02 object-04 object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-02 object-04 object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-04 object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-08 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-06 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 object-01 object-04 object-08 Root Set object-06 Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

Example object-01 object-02 object-03 object-04 reclaimed object-05 object-06 object-07 reclaimed object-08 object-01 object-04 object-08 Root Set Working Set CCMP2 Garbage Collection May 15, 2017 20 / 35

How to sweep? Sweeping requires to know where are unreacheable objets! Heap : object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 CCMP2 Garbage Collection May 15, 2017 21 / 35

How to sweep? Sweeping requires to know where are unreacheable objets! Heap : object-01 object-02 object-03 object-04 object-05 object-06 object-07 object-08 Just remove from the heap all non-marked objects CCMP2 Garbage Collection May 15, 2017 21 / 35

Problems Runtime proportional to number of allocated objects Sweep phase visits all objects to free them or clear marks Work list requires lots of memory Amount of space required could potentially be as large as all of memory Can t preallocate this space CCMP2 Garbage Collection May 15, 2017 22 / 35

Pros and Cons Pros: Cons: Can free cyclic references 1 bits per state Runtime can be proportional to the number of reachable objects (Baker s algorihtm) Stop the world algorithm with possibly huge pauses times Memory Fragmentation Need to walk the whole heap CCMP2 Garbage Collection May 15, 2017 23 / 35

Table of contents 1 Motivations and Definitions 2 Reference Counting Garbage Collection 3 Mark and Sweep Garbage Collection 4 Stop and Copy Garbage Collection 5 Hybrid Approaches CCMP2 Garbage Collection May 15, 2017 24 / 35

Analysis Locality can be improved After garbage collection, objects are no longer closed in memory Allocation speed can be improved After garbage collection, the free list of the allocator must be walked. The Sweep Phase can be improved CCMP2 Garbage Collection May 15, 2017 25 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) Copy objects CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) Copy objects CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) Copy objects CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) Copy objects Update References & Root set CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) Copy objects Update References & Root set Clean zone 1 (Constant time) CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) Copy objects Update References & Root set Clean zone 1 (Constant time) Swap zone 1 and 2 (Now allocation will happen in zone 2) CCMP2 Garbage Collection May 15, 2017 26 / 35

Exemple Zone 1 Zone 2 Split memory in two pieces Allocate memory in the first zone When running out-of-space in the first zone: Garbage Collect! Explore only reachable references from the root set (here only green object) Copy objects Update References & Root set Clean zone 1 (Constant time) Swap zone 1 and 2 (Now allocation will happen in zone 2) Allocate the object that have provoqued the GC CCMP2 Garbage Collection May 15, 2017 26 / 35

Implementation Partition memory into two regions: the old space and the new space. Keep track of the next free address in the new space. To allocate n bytes of memory: If n bytes space exist at the free space pointer, use those bytes and advance the pointer. Otherwise, do a copy step. To execute a copy step: For each object in the root set: Copy that object over to the start of the old space. Recursively copy over all objects reachable from that object. Adjust the pointers in the old space and root set to point to new locations. Exchange the roles of the old and new spaces. CCMP2 Garbage Collection May 15, 2017 27 / 35

Problems How to adjust pointers in the copied objects correctly? CCMP2 Garbage Collection May 15, 2017 28 / 35

Problems How to adjust pointers in the copied objects correctly? 1 Have each object contain a extra space for a forwarding pointer 2 First, do a complete bitwise copy of the object 3 Next, set the forwarding pointer of the original object to point to the new object Follow the pointer to the object it references Replace the pointer with the pointee s forwarding pointer CCMP2 Garbage Collection May 15, 2017 28 / 35

Pros and Cons Pros: Compact the Heap Allocation only increments a pointer No sweep Cons: Smaller Heap Copy Reference adjusting CCMP2 Garbage Collection May 15, 2017 29 / 35

Table of contents 1 Motivations and Definitions 2 Reference Counting Garbage Collection 3 Mark and Sweep Garbage Collection 4 Stop and Copy Garbage Collection 5 Hybrid Approaches CCMP2 Garbage Collection May 15, 2017 30 / 35

Analysis The best garbage collectors in use today are based on a combination of smaller garbage collectors Objects Die Young Most objects have extremely short lifetimes Optimize garbage collection to reclaim young objects rapidly while spending less time on older objects CCMP2 Garbage Collection May 15, 2017 31 / 35

Generational Garbage Collector Partition memory into several generations Objects are always allocated in the first generation. When the first generation fills up, garbage collect it. Runs quickly; collects only a small region of memory. Move objects that survive in the first generation long enough into the next generation. When no space can be found, run a full (slower) garbage collection on all of memory. CCMP2 Garbage Collection May 15, 2017 32 / 35

Garbage Collection in Java 1 Split the Heap in 3 zones: eden, survivors and tenured 2 New objects are allocated using a modified stop-and-copy collector in the Eden space. 3 When Eden runs out of space, the stop-and-copy collector moves its elements to the survivor space. 4 Objects that survive long enough in the survivor space become tenured and are moved to the tenured space. 5 When memory fills up, a full garbage collection (perhaps mark-and-sweep) is used to garbage-collect the tenured objects CCMP2 Garbage Collection May 15, 2017 33 / 35

Garbage Collection in C Boehm GC Mark and Sweep Conservative Consider all program variables as root set Easy to combine with C CCMP2 Garbage Collection May 15, 2017 34 / 35

Bibliography Uniprocessor Garbage Collection, Paul R. Wilson CCMP2 Garbage Collection May 15, 2017 35 / 35