Garbage Collection Akim Demaille, Etienne Renault, Roland Levillain - - PowerPoint PPT Presentation

garbage collection
SMART_READER_LITE
LIVE PREVIEW

Garbage Collection Akim Demaille, Etienne Renault, Roland Levillain - - PowerPoint PPT Presentation

Garbage Collection Akim Demaille, Etienne Renault, Roland Levillain June 4, 2019 TYLA Garbage Collection June 4, 2019 1 / 35 Table of contents Motivations and Definitions 1 Reference Counting Garbage Collection 2 Mark and Sweep Garbage


slide-1
SLIDE 1

Garbage Collection

Akim Demaille, Etienne Renault, Roland Levillain June 4, 2019

TYLA Garbage Collection June 4, 2019 1 / 35

slide-2
SLIDE 2

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

TYLA Garbage Collection June 4, 2019 2 / 35

slide-3
SLIDE 3

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

TYLA Garbage Collection June 4, 2019 3 / 35

slide-4
SLIDE 4

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.

TYLA Garbage Collection June 4, 2019 4 / 35

slide-5
SLIDE 5

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 */ }

TYLA Garbage Collection June 4, 2019 5 / 35

slide-6
SLIDE 6

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

TYLA Garbage Collection June 4, 2019 6 / 35

slide-7
SLIDE 7

Basics of a Garbage Collector

1 Distinguishing the live objects from the garbage ones 2 Reclaiming the garbage object’ storage TYLA Garbage Collection June 4, 2019 7 / 35

slide-8
SLIDE 8

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 TYLA Garbage Collection June 4, 2019 7 / 35

slide-9
SLIDE 9

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

  • ften

◮ avoid overhead due to long time objects ◮ rely on pause to collect data TYLA Garbage Collection June 4, 2019 8 / 35

slide-10
SLIDE 10

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

TYLA Garbage Collection June 4, 2019 9 / 35

slide-11
SLIDE 11

Reference Counting

Intuition

TYLA Garbage Collection June 4, 2019 10 / 35

slide-12
SLIDE 12

Reference Counting

Intuition

Maintain for each object a counter to the references to this object

TYLA Garbage Collection June 4, 2019 10 / 35

slide-13
SLIDE 13

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

  • bject’s counter

TYLA Garbage Collection June 4, 2019 10 / 35

slide-14
SLIDE 14

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

  • bject’s counter

Each time an existing reference to an object is eliminated, the counter is decremented

TYLA Garbage Collection June 4, 2019 10 / 35

slide-15
SLIDE 15

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

  • bject’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

TYLA Garbage Collection June 4, 2019 10 / 35

slide-16
SLIDE 16

Deallocation

Caution

When an object is destructed: Transitive reclamation can be deferred by maintaining a list of freed objects

TYLA Garbage Collection June 4, 2019 11 / 35

slide-17
SLIDE 17

Deallocation

Caution

When an object is destructed: examines pointer fields Transitive reclamation can be deferred by maintaining a list of freed objects

TYLA Garbage Collection June 4, 2019 11 / 35

slide-18
SLIDE 18

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

TYLA Garbage Collection June 4, 2019 11 / 35

slide-19
SLIDE 19

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

TYLA Garbage Collection June 4, 2019 11 / 35

slide-20
SLIDE 20

Exemple

class LinkedList { LinkedList next = null; } int main () { }

TYLA Garbage Collection June 4, 2019 12 / 35

slide-21
SLIDE 21

Exemple

class LinkedList { LinkedList next = null; } int main () { LinkedList head = new LinkedList; }

head 1

TYLA Garbage Collection June 4, 2019 12 / 35

slide-22
SLIDE 22

Exemple

class LinkedList { LinkedList next = null; } int main () { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; }

head 1 mid 1

TYLA Garbage Collection June 4, 2019 12 / 35

slide-23
SLIDE 23

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

TYLA Garbage Collection June 4, 2019 12 / 35

slide-24
SLIDE 24

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 1 tail 1 2

TYLA Garbage Collection June 4, 2019 12 / 35

slide-25
SLIDE 25

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 1 tail 1 2 2

TYLA Garbage Collection June 4, 2019 12 / 35

slide-26
SLIDE 26

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 1 tail 1 2 2 1

TYLA Garbage Collection June 4, 2019 12 / 35

slide-27
SLIDE 27

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 1 tail 1 2 2 1 1

TYLA Garbage Collection June 4, 2019 12 / 35

slide-28
SLIDE 28

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 1 tail 1 2 2 1 1

TYLA Garbage Collection June 4, 2019 12 / 35

slide-29
SLIDE 29

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 1 tail 1 2 2 1 1 reclaimed

TYLA Garbage Collection June 4, 2019 12 / 35

slide-30
SLIDE 30

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 1 mid 1 tail 2 1

TYLA Garbage Collection June 4, 2019 12 / 35

slide-31
SLIDE 31

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 1 mid 1 tail 2 1 reclaimed

TYLA Garbage Collection June 4, 2019 12 / 35

slide-32
SLIDE 32

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 1 tail 2 1

TYLA Garbage Collection June 4, 2019 12 / 35

slide-33
SLIDE 33

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 1 tail 2 1 reclaimed

TYLA Garbage Collection June 4, 2019 12 / 35

slide-34
SLIDE 34

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 () { }

TYLA Garbage Collection June 4, 2019 13 / 35

slide-35
SLIDE 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

TYLA Garbage Collection June 4, 2019 13 / 35

slide-36
SLIDE 36

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

TYLA Garbage Collection June 4, 2019 13 / 35

slide-37
SLIDE 37

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

TYLA Garbage Collection June 4, 2019 13 / 35

slide-38
SLIDE 38

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 1 tail 1 2

TYLA Garbage Collection June 4, 2019 13 / 35

slide-39
SLIDE 39

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 1 tail 1 2 2

TYLA Garbage Collection June 4, 2019 13 / 35

slide-40
SLIDE 40

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 1 mid 1 tail 1 2 2 2

TYLA Garbage Collection June 4, 2019 13 / 35

slide-41
SLIDE 41

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 1 mid 1 tail 1 2 2 2 1

TYLA Garbage Collection June 4, 2019 13 / 35

slide-42
SLIDE 42

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 1 mid 1 tail 1 2 2 2 1 1

TYLA Garbage Collection June 4, 2019 13 / 35

slide-43
SLIDE 43

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 1 mid 1 tail 1 2 2 2 1 1 1

TYLA Garbage Collection June 4, 2019 13 / 35

slide-44
SLIDE 44

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

  • bjects

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

TYLA Garbage Collection June 4, 2019 14 / 35

slide-45
SLIDE 45

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

TYLA Garbage Collection June 4, 2019 15 / 35

slide-46
SLIDE 46

Analysis

Reference counting tries to find unreachable objects by finding objects without incoming references These references have been forgotten !

TYLA Garbage Collection June 4, 2019 16 / 35

slide-47
SLIDE 47

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

TYLA Garbage Collection June 4, 2019 16 / 35

slide-48
SLIDE 48

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

TYLA Garbage Collection June 4, 2019 17 / 35

slide-49
SLIDE 49

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. TYLA Garbage Collection June 4, 2019 18 / 35

slide-50
SLIDE 50

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 TYLA Garbage Collection June 4, 2019 19 / 35

slide-51
SLIDE 51

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

TYLA Garbage Collection June 4, 2019 20 / 35

slide-52
SLIDE 52

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08

TYLA Garbage Collection June 4, 2019 20 / 35

slide-53
SLIDE 53

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08

Working Set

  • bject-01
  • bject-04
  • bject-08

TYLA Garbage Collection June 4, 2019 20 / 35

slide-54
SLIDE 54

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08

Working Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01

TYLA Garbage Collection June 4, 2019 20 / 35

slide-55
SLIDE 55

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08

Working Set

  • bject-05
  • bject-02
  • bject-04
  • bject-08
  • bject-01

TYLA Garbage Collection June 4, 2019 20 / 35

slide-56
SLIDE 56

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01

Working Set

  • bject-05
  • bject-02
  • bject-04
  • bject-08
  • bject-05

TYLA Garbage Collection June 4, 2019 20 / 35

slide-57
SLIDE 57

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05

Working Set

  • bject-02
  • bject-04
  • bject-08

TYLA Garbage Collection June 4, 2019 20 / 35

slide-58
SLIDE 58

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05

Working Set

  • bject-02
  • bject-04
  • bject-08
  • bject-02
  • bject-02

TYLA Garbage Collection June 4, 2019 20 / 35

slide-59
SLIDE 59

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05
  • bject-02

Working Set

  • bject-04
  • bject-08
  • bject-02

Working Set

  • bject-04
  • bject-08
  • bject-04

TYLA Garbage Collection June 4, 2019 20 / 35

slide-60
SLIDE 60

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05
  • bject-02
  • bject-02
  • bject-04

Working Set

  • bject-08

TYLA Garbage Collection June 4, 2019 20 / 35

slide-61
SLIDE 61

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05
  • bject-02
  • bject-02
  • bject-04

Working Set

  • bject-08
  • bject-08

TYLA Garbage Collection June 4, 2019 20 / 35

slide-62
SLIDE 62

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05
  • bject-02
  • bject-02
  • bject-04
  • bject-08

Working Set

  • bject-06

TYLA Garbage Collection June 4, 2019 20 / 35

slide-63
SLIDE 63

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05
  • bject-02
  • bject-02
  • bject-04
  • bject-08

Working Set

  • bject-06
  • bject-06

Working Set

TYLA Garbage Collection June 4, 2019 20 / 35

slide-64
SLIDE 64

Example

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Root Set

  • bject-01
  • bject-04
  • bject-08
  • bject-01
  • bject-05
  • bject-02
  • bject-02
  • bject-04
  • bject-08
  • bject-06

Working Set

reclaimed reclaimed

TYLA Garbage Collection June 4, 2019 20 / 35

slide-65
SLIDE 65

How to sweep?

Sweeping requires to know where are unreacheable objets ! Heap :

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

TYLA Garbage Collection June 4, 2019 21 / 35

slide-66
SLIDE 66

How to sweep?

Sweeping requires to know where are unreacheable objets ! Heap :

  • bject-01
  • bject-02
  • bject-03
  • bject-04
  • bject-05
  • bject-06
  • bject-07
  • bject-08

Just remove from the heap all non-marked objects

TYLA Garbage Collection June 4, 2019 21 / 35

slide-67
SLIDE 67

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 TYLA Garbage Collection June 4, 2019 22 / 35

slide-68
SLIDE 68

Pros and Cons

Pros: Can free cyclic references 1 bits per state Runtime can be proportional to the number of reachable objects (Baker’s algorihtm) Cons: Stop the world algorithm with possibly huge pauses times Memory Fragmentation Need to walk the whole heap

TYLA Garbage Collection June 4, 2019 23 / 35

slide-69
SLIDE 69

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

TYLA Garbage Collection June 4, 2019 24 / 35

slide-70
SLIDE 70

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

TYLA Garbage Collection June 4, 2019 25 / 35

slide-71
SLIDE 71

Exemple

Zone 1 Zone 2

Split memory in two pieces

TYLA Garbage Collection June 4, 2019 26 / 35

slide-72
SLIDE 72

Exemple

Zone 1 Zone 2

Split memory in two pieces Allocate memory in the first zone

TYLA Garbage Collection June 4, 2019 26 / 35

slide-73
SLIDE 73

Exemple

Zone 1 Zone 2

Split memory in two pieces Allocate memory in the first zone

TYLA Garbage Collection June 4, 2019 26 / 35

slide-74
SLIDE 74

Exemple

Zone 1 Zone 2

Split memory in two pieces Allocate memory in the first zone

TYLA Garbage Collection June 4, 2019 26 / 35

slide-75
SLIDE 75

Exemple

Zone 1 Zone 2

Split memory in two pieces Allocate memory in the first zone

TYLA Garbage Collection June 4, 2019 26 / 35

slide-76
SLIDE 76

Exemple

Zone 1 Zone 2

Split memory in two pieces Allocate memory in the first zone

TYLA Garbage Collection June 4, 2019 26 / 35

slide-77
SLIDE 77

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!

TYLA Garbage Collection June 4, 2019 26 / 35

slide-78
SLIDE 78

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

  • bject)

TYLA Garbage Collection June 4, 2019 26 / 35

slide-79
SLIDE 79

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

  • bject)

Copy objects

TYLA Garbage Collection June 4, 2019 26 / 35

slide-80
SLIDE 80

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

  • bject)

Copy objects

TYLA Garbage Collection June 4, 2019 26 / 35

slide-81
SLIDE 81

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

  • bject)

Copy objects

TYLA Garbage Collection June 4, 2019 26 / 35

slide-82
SLIDE 82

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

  • bject)

Copy objects Update References & Root set

TYLA Garbage Collection June 4, 2019 26 / 35

slide-83
SLIDE 83

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

  • bject)

Copy objects Update References & Root set Clean zone 1 (Constant time)

TYLA Garbage Collection June 4, 2019 26 / 35

slide-84
SLIDE 84

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

  • bject)

Copy objects Update References & Root set Clean zone 1 (Constant time) Swap zone 1 and 2 (Now allocation will happen in zone 2)

TYLA Garbage Collection June 4, 2019 26 / 35

slide-85
SLIDE 85

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

  • bject)

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

TYLA Garbage Collection June 4, 2019 26 / 35

slide-86
SLIDE 86

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.

TYLA Garbage Collection June 4, 2019 27 / 35

slide-87
SLIDE 87

Problems

How to adjust pointers in the copied objects correctly?

TYLA Garbage Collection June 4, 2019 28 / 35

slide-88
SLIDE 88

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 TYLA Garbage Collection June 4, 2019 28 / 35

slide-89
SLIDE 89

Pros and Cons

Pros: Compact the Heap Allocation only increments a pointer No sweep Cons: Smaller Heap Copy Reference adjusting

TYLA Garbage Collection June 4, 2019 29 / 35

slide-90
SLIDE 90

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

TYLA Garbage Collection June 4, 2019 30 / 35

slide-91
SLIDE 91

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

TYLA Garbage Collection June 4, 2019 31 / 35

slide-92
SLIDE 92

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.

TYLA Garbage Collection June 4, 2019 32 / 35

slide-93
SLIDE 93

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

TYLA Garbage Collection June 4, 2019 33 / 35

slide-94
SLIDE 94

Garbage Collection in C

Boehm GC Mark and Sweep Conservative Consider all program variables as root set Easy to combine with C

TYLA Garbage Collection June 4, 2019 34 / 35

slide-95
SLIDE 95

Bibliography

Uniprocessor Garbage Collection , Paul R. Wilson

TYLA Garbage Collection June 4, 2019 35 / 35