Who Takes Out Your Trash?

A presentation at CodeBEAM Lite Amsterdam in November 2018 in Amsterdam, Netherlands by Sanne Kalkman

Slide 1

Slide 1

WHO TAKES OUT YOUR TRASH? @sannekalkman

Slide 2

Slide 2

Slide 3

Slide 3

đź”®

Slide 4

Slide 4

đź”® đź’»

Slide 5

Slide 5

🔮 💻 Magic is just (computer) science you don’t understand yet

Slide 6

Slide 6

Slide 7

Slide 7

Slide 8

Slide 8

Slide 9

Slide 9

🧠 Allocate memory

Slide 10

Slide 10

🧠 🔍 Allocate memory Find the garbage

Slide 11

Slide 11

🧠 🔍 🗑 Allocate memory Find the garbage Free up memory

Slide 12

Slide 12

Slide 13

Slide 13

đź“š Running out of memory

Slide 14

Slide 14

📚 🔥 Running out of memory Breaking things

Slide 15

Slide 15

DANGLING POINTERS

Slide 16

Slide 16

DANGLING POINTERS

Slide 17

Slide 17

DANGLING POINTERS 🔥

Slide 18

Slide 18

DANGLING POINTERS 🔥 Memory Leak

Slide 19

Slide 19

REFERENCE COUNTING If nothing points to it, nothing’s using it

Slide 20

Slide 20

REFERENCE COUNTING

Slide 21

Slide 21

REFERENCE COUNTING name = “codeBEAM”

Slide 22

Slide 22

REFERENCE COUNTING name name = “codeBEAM” “codeBEAM” 1

Slide 23

Slide 23

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” 1

Slide 24

Slide 24

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” other_name 2

Slide 25

Slide 25

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” name = nil other_name 2

Slide 26

Slide 26

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” name = nil other_name 1

Slide 27

Slide 27

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” name = nil other_name = nil other_name 1

Slide 28

Slide 28

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” name = nil other_name = nil other_name 0

Slide 29

Slide 29

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” name = nil other_name = nil other_name 0

Slide 30

Slide 30

CYCLES

Slide 31

Slide 31

CYCLES a = { other: nil }

Slide 32

Slide 32

CYCLES a = { other: nil } a { other: nil } 1

Slide 33

Slide 33

CYCLES a = { other: nil } b = { other: a } a { other: nil } 1

Slide 34

Slide 34

CYCLES a = { other: nil } a { other: nil } 1 b { other: a } 1 b = { other: a }

Slide 35

Slide 35

CYCLES a = { other: nil } a { other: nil } 2 b { other: a } 1 b = { other: a }

Slide 36

Slide 36

CYCLES a = { other: nil } a { other: nil } 2 b { other: a } 1 b = { other: a } a[:other] = b

Slide 37

Slide 37

CYCLES a = { other: nil } a { other: b } 2 b { other: a } 1 b = { other: a } a[:other] = b

Slide 38

Slide 38

CYCLES a = { other: nil } a { other: b } 2 b { other: a } 2 b = { other: a } a[:other] = b

Slide 39

Slide 39

CYCLES a = { other: nil } a { other: b } 2 b { other: a } 2 b = { other: a } a[:other] = b a = nil b = nil

Slide 40

Slide 40

CYCLES a = { other: nil } a { other: b } 1 b { other: a } 2 b = { other: a } a[:other] = b a = nil b = nil

Slide 41

Slide 41

CYCLES a = { other: nil } a { other: b } 1 b { other: a } 1 b = { other: a } a[:other] = b a = nil b = nil

Slide 42

Slide 42

MARK & SWEEP If you can’t get to it, you can’t be using it

Slide 43

Slide 43

Slide 44

Slide 44

MARK & SWEEP

Slide 45

Slide 45

MARK & SWEEP

Slide 46

Slide 46

MARK & SWEEP

Slide 47

Slide 47

MARK & SWEEP

Slide 48

Slide 48

MARK & SWEEP

Slide 49

Slide 49

MARK & SWEEP

Slide 50

Slide 50

MARK & SWEEP

Slide 51

Slide 51

MARK & SWEEP

Slide 52

Slide 52

MARK & SWEEP

Slide 53

Slide 53

MARK & SWEEP

Slide 54

Slide 54

MARK & SWEEP

Slide 55

Slide 55

MARK & SWEEP

Slide 56

Slide 56

COPYING COLLECTION Keep your stuff together

Slide 57

Slide 57

COPYING COLLECTION FROM Space TO Space 0 1 2 3 4 5 6 7 8

Slide 58

Slide 58

COPYING COLLECTION FROM Space 0 TO Space 0 1 2 3 4 5 6 7 8

Slide 59

Slide 59

COPYING COLLECTION FROM Space 0 1 TO Space 0 2 2 3 4 5 6 7 8

Slide 60

Slide 60

COPYING COLLECTION FROM Space 0 1 2 TO Space 0 2 3 3 4 5 6 7 8

Slide 61

Slide 61

COPYING COLLECTION FROM Space 0 1 2 3 TO Space 0 2 3 4 4 5 6 7 8

Slide 62

Slide 62

COPYING COLLECTION FROM Space 0 1 2 3 4 TO Space 0 2 3 4 5 5 6 7 8

Slide 63

Slide 63

COPYING COLLECTION FROM Space 0 1 2 3 4 5 TO Space 0 2 3 4 5 7 6 7 8

Slide 64

Slide 64

COPYING COLLECTION FROM Space 0 TO Space 0 2 2 3 4 5 3 4 5 7 7

Slide 65

Slide 65

COPYING COLLECTION FROM Space 0 TO Space 0 2 2 3 4 5 3 4 5 7 7

Slide 66

Slide 66

GENERATIONAL COLLECTION If you’ve been using it for a while, it’s probably important

Slide 67

Slide 67

GENERATIONAL COLLECTION New objects Old objects D

Slide 68

Slide 68

GENERATIONAL COLLECTION New objects Old objects D

Slide 69

Slide 69

GENERATIONAL COLLECTION New objects Old objects D

Slide 70

Slide 70

GENERATIONAL COLLECTION New objects Old objects D

Slide 71

Slide 71

GENERATIONAL COLLECTION New objects Old objects D

Slide 72

Slide 72

GENERATIONAL COLLECTION New objects Old objects D

Slide 73

Slide 73

GENERATIONAL COLLECTION New objects Old objects D

Slide 74

Slide 74

GENERATIONAL COLLECTION New objects Old objects D

Slide 75

Slide 75

GENERATIONAL COLLECTION New objects Old objects D

Slide 76

Slide 76

GENERATIONAL COLLECTION New objects Old objects D

Slide 77

Slide 77

GENERATIONAL COLLECTION New objects Old objects D

Slide 78

Slide 78

GENERATIONAL COLLECTION New objects Old objects D

Slide 79

Slide 79

GENERATIONAL COLLECTION New objects Old objects D

Slide 80

Slide 80

WHAT ABOUT THE BEAM? Erlang & Elixir garbage collection

Slide 81

Slide 81

Slide 82

Slide 82

📬 Processes

Slide 83

Slide 83

📬 🚫 Processes Immutability

Slide 84

Slide 84

MEMORY LAYOUT Shared

Slide 85

Slide 85

MEMORY LAYOUT Process A A Shared

Slide 86

Slide 86

MEMORY LAYOUT Process B Process A A B Shared

Slide 87

Slide 87

MEMORY LAYOUT Process B Process A A B Process C C Shared

Slide 88

Slide 88

MEMORY LAYOUT Process B B Process C C Shared

Slide 89

Slide 89

MEMORY LAYOUT Process B Process D D B Process C C Shared

Slide 90

Slide 90

PROCESS MEMORY Heap Stack

Slide 91

Slide 91

PROCESS MEMORY Heap Stack

Slide 92

Slide 92

PROCESS MEMORY Heap Stack

Slide 93

Slide 93

PROCESS MEMORY Heap Stack

Slide 94

Slide 94

PROCESS MEMORY Heap Stack

Slide 95

Slide 95

PROCESS MEMORY Heap Stack

Slide 96

Slide 96

PROCESS MEMORY Heap Stack

Slide 97

Slide 97

PROCESS MEMORY Heap Stack

Slide 98

Slide 98

PROCESS MEMORY Heap Stack

Slide 99

Slide 99

PROCESS MEMORY Heap Stack

Slide 100

Slide 100

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space 1 2 3 4 5 6 7 8 9

Slide 101

Slide 101

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space 1 6 2 3 4 5 6 7 8 9

Slide 102

Slide 102

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space 1 6 2 3 4 5 M 6 7 8 9

Slide 103

Slide 103

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space 1 2 6 1 3 4 5 M 6 7 8 9

Slide 104

Slide 104

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 6 1 3 4 5 M 6 7 8 9

Slide 105

Slide 105

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 6 1 3 4 5 M 6 7 8 9

Slide 106

Slide 106

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 3 6 1 5 4 5 M 6 7 8 9

Slide 107

Slide 107

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 3 6 1 5 4 M 5 M 6 7 8 9

Slide 108

Slide 108

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 3 6 1 5 4 M 5 M 6 7 8 9

Slide 109

Slide 109

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 3 4 6 1 5 3 M 5 M 6 7 8 9

Slide 110

Slide 110

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 M 3 4 6 1 5 3 M 5 M 6 7 8 9

Slide 111

Slide 111

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 M 3 4 6 1 5 3 M 5 M 6 7 8 9

Slide 112

Slide 112

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 M 3 4 6 1 5 3 M 5 M 6 7 8 9

Slide 113

Slide 113

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space M 1 2 M 3 4 6 1 5 3 M 5 M 6 7 8 9 7 8 9

Slide 114

Slide 114

COPYING COLLECTION ROOTS 4 1 3 6 2 5 FROM Space TO Space 6 1 5 3 7 8 9

Slide 115

Slide 115

GENERATIONAL COLLECTION FROM Space TO Space OLD Space 6 3 5 1 8 9 8 9

Slide 116

Slide 116

GENERATIONAL COLLECTION High-water mark FROM Space TO Space OLD Space 6 3 5 1 8 9 8 9

Slide 117

Slide 117

GENERATIONAL COLLECTION * High-water mark FROM Space TO Space OLD Space 6 3 5 1 8 9 8 9

Slide 118

Slide 118

GENERATIONAL COLLECTION * High-water mark FROM Space TO Space OLD Space 6 3 5 1 A 8 9 8 9

Slide 119

Slide 119

GENERATIONAL COLLECTION * High-water mark FROM Space TO Space OLD Space 6 3 5 1 A B 8 9 8 9

Slide 120

Slide 120

GENERATIONAL COLLECTION * High-water mark FROM Space TO Space OLD Space 6 3 5 1 A B C 8 9 8 9

Slide 121

Slide 121

GENERATIONAL COLLECTION * High-water mark FROM Space 6 TO Space OLD Space 5 3 M 5 1 A B C 8 9 8 9

Slide 122

Slide 122

GENERATIONAL COLLECTION * High-water mark FROM Space 6 TO Space A OLD Space 5 3 M 5 1 M A B C 8 9 8 9

Slide 123

Slide 123

GENERATIONAL COLLECTION * High-water mark FROM Space 6 3 TO Space A C OLD Space 5 M 5 1 M A B M C 8 9 8 9

Slide 124

Slide 124

GENERATIONAL COLLECTION * High-water mark FROM Space 6 M 3 TO Space A C OLD Space 5 3 M 5 1 M A B M C 8 9 8 9

Slide 125

Slide 125

GENERATIONAL COLLECTION * High-water mark FROM Space 6 M 3 TO Space A C OLD Space 5 3 M 5 1 M A B M C 8 9 8 9

Slide 126

Slide 126

GENERATIONAL COLLECTION * High-water mark FROM Space 6 M 3 TO Space A C OLD Space 5 3 M 5 1 M A B M C 8 9 8 9

Slide 127

Slide 127

BINARY HEAP

Slide 128

Slide 128

BINARY HEAP ✉ Binaries > 64 bytes

Slide 129

Slide 129

BINARY HEAP ✉ 🔢 Binaries > 64 bytes Reference counted

Slide 130

Slide 130

đź”® đź’» Garbage collection is no longer magic!

Slide 131

Slide 131

LET ME KNOW WHAT YOU THINK! Tweet me @sannekalkman Find me in person later! The Garbage Collection Handbook - Jones, Hoskin & Moss Erlang Garbage Collector - Lukas Larsson (Erlang Solutions)