Who Takes Out Your Trash?

A presentation at AmsterdamRB - Talks & Drinks in January 2019 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 = “Ruby”

Slide 22

Slide 22

REFERENCE COUNTING name name = “Ruby” “Ruby” 1

Slide 23

Slide 23

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

Slide 24

Slide 24

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

Slide 25

Slide 25

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

Slide 26

Slide 26

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

Slide 27

Slide 27

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

Slide 28

Slide 28

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

Slide 29

Slide 29

REFERENCE COUNTING name name = “Ruby” other_name = name “Ruby” 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 RUBY? MRI garbage collection

Slide 81

Slide 81

FREE LIST

Slide 82

Slide 82

FREE LIST

Slide 83

Slide 83

FREE LIST

Slide 84

Slide 84

FREE LIST

Slide 85

Slide 85

FREE LIST

Slide 86

Slide 86

FREE LIST

Slide 87

Slide 87

FREE LIST

Slide 88

Slide 88

MARK & SWEEP

Slide 89

Slide 89

MARK & SWEEP

Slide 90

Slide 90

MARK & SWEEP

Slide 91

Slide 91

MARK & SWEEP

Slide 92

Slide 92

MARK & SWEEP

Slide 93

Slide 93

MARK & SWEEP

Slide 94

Slide 94

MARK & SWEEP

Slide 95

Slide 95

MARK & SWEEP

Slide 96

Slide 96

MARK & SWEEP

Slide 97

Slide 97

LAZY SWEEPING

Slide 98

Slide 98

LAZY SWEEPING

Slide 99

Slide 99

LAZY SWEEPING

Slide 100

Slide 100

LAZY SWEEPING

Slide 101

Slide 101

LAZY SWEEPING

Slide 102

Slide 102

LAZY SWEEPING

Slide 103

Slide 103

GENERATIONAL COLLECTION ) (

Slide 104

Slide 104

GENERATIONAL COLLECTION ) (

Slide 105

Slide 105

GENERATIONAL COLLECTION ) (

Slide 106

Slide 106

GENERATIONAL COLLECTION ) (

Slide 107

Slide 107

GENERATIONAL COLLECTION ) (

Slide 108

Slide 108

GENERATIONAL COLLECTION ) (

Slide 109

Slide 109

GENERATIONAL COLLECTION ) (

Slide 110

Slide 110

GENERATIONAL COLLECTION ) (

Slide 111

Slide 111

WRITE BARRIERS

Slide 112

Slide 112

WRITE BARRIERS

Slide 113

Slide 113

WRITE BARRIERS

Slide 114

Slide 114

WRITE BARRIERS

Slide 115

Slide 115

WRITE BARRIERS

Slide 116

Slide 116

WRITE BARRIERS *

Slide 117

Slide 117

Slide 118

Slide 118

☀ Sunny

Slide 119

Slide 119

☀ 🕶 Sunny Shady

Slide 120

Slide 120

🔮 💻 Garbage collection is no longer magic!

Slide 121

Slide 121

LET ME KNOW WHAT YOU THINK! Tweet me @sannekalkman Find me in person later! The Garbage Collection Handbook - Jones, Hoskin & Moss Ruby Under A Microscope - Pat Shaughnessy