Slide 1

Slide 1 text

Interactive Portion

Slide 2

Slide 2 text

Unlock your phones

Slide 3

Slide 3 text

Selfie Sticks

Slide 4

Slide 4 text

Two Purposes

Slide 5

Slide 5 text

Help you identify people who like fun stuff and to have a fun time

Slide 6

Slide 6 text

Let you know who doesn’t like to have fun or like fun things

Slide 7

Slide 7 text

Have you seen this?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

It’s me!

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Methods of Memory Management in MRI

Slide 17

Slide 17 text

mmmmRuby

Slide 18

Slide 18 text

Aaron Patterson @tenderlove PGP Fingerprint: 4CE9 1B75 A798 28E8 6B1A A8BB 9531 70BC B4FF AFC6

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

I am from Seattle

Slide 21

Slide 21 text

Which is not Ohio

Slide 22

Slide 22 text

!

Slide 23

Slide 23 text

"Ohio"

Slide 24

Slide 24 text

h GitHub

Slide 25

Slide 25 text

LeGit

Slide 26

Slide 26 text

GitHub Certified® Engineer

Slide 27

Slide 27 text

Bear Metal

Slide 28

Slide 28 text

x tenderlove

Slide 29

Slide 29 text

Please call me "Aaron"

Slide 30

Slide 30 text

("tenderlove" is fine too)

Slide 31

Slide 31 text

I love cats!

Slide 32

Slide 32 text

Cats are the best!

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

I have stickers of my cats

Slide 39

Slide 39 text

I also have GitHub stickers!

Slide 40

Slide 40 text

I was in the news recently

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Keyboards

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

New Ruby Features

Slide 46

Slide 46 text

Soft Typing

Slide 47

Slide 47 text

Dynamic Typing

Slide 48

Slide 48 text

Static Typing

Slide 49

Slide 49 text

GC

Slide 50

Slide 50 text

Garbage Collector

Slide 51

Slide 51 text

Memory Terms

Slide 52

Slide 52 text

Stack Heap

Slide 53

Slide 53 text

Heap Ruby Heap

Slide 54

Slide 54 text

GC in MRI

Slide 55

Slide 55 text

Apps in Production

Slide 56

Slide 56 text

Scaling Issues

Slide 57

Slide 57 text

Tuning Issues

Slide 58

Slide 58 text

What I want you to learn

Slide 59

Slide 59 text

If you don’t know much about GC

Slide 60

Slide 60 text

If you know about GC terminology

Slide 61

Slide 61 text

If you already know the algorithms

Slide 62

Slide 62 text

If you already know the new stuff

Slide 63

Slide 63 text

GC Algorithms (in MRI)

Slide 64

Slide 64 text

Two sides of a GC

Slide 65

Slide 65 text

Collection Algorithm

Slide 66

Slide 66 text

Allocation Algorithm

Slide 67

Slide 67 text

Introspection API

Slide 68

Slide 68 text

Tuning Variables

Slide 69

Slide 69 text

Collection Algorithm

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

What type is the MRI collector?

Slide 73

Slide 73 text

Generational Incremental Mark & Sweep

Slide 74

Slide 74 text

High level: What is a GC?

Slide 75

Slide 75 text

Tree of Objects Root A B C D a = [ { c: 'd' } ] Ruby Array Hash String Symbol

Slide 76

Slide 76 text

Tree of Objects Root A B C D a = [ { c: 'd' } ] a = nil Ruby Array Hash String Symbol

Slide 77

Slide 77 text

Important Words!

Slide 78

Slide 78 text

Root Set

Slide 79

Slide 79 text

Garbage

Slide 80

Slide 80 text

Live Data

Slide 81

Slide 81 text

GC’s job: Find unlinked nodes, then free them

Slide 82

Slide 82 text

How to find unlinked nodes

Slide 83

Slide 83 text

Mark & Sweep

Slide 84

Slide 84 text

2 distinct phases

Slide 85

Slide 85 text

Mark Phase Root D A B C F E H G

Slide 86

Slide 86 text

Mark Phase Root D A B C F E H G

Slide 87

Slide 87 text

Mark Phase Root D A B C F E H G

Slide 88

Slide 88 text

Mark Phase Root D A B C F E H G

Slide 89

Slide 89 text

Sweep Phase Root D A B C F E H G

Slide 90

Slide 90 text

Sweep Phase Root D A B C F

Slide 91

Slide 91 text

Mark & Sweep Very Easy! Too Slow

Slide 92

Slide 92 text

"Stop the world"

Slide 93

Slide 93 text

Visits every object, every time

Slide 94

Slide 94 text

Walk every object every time

Slide 95

Slide 95 text

Generational

Slide 96

Slide 96 text

Objects die young

Slide 97

Slide 97 text

Divide objects in to "old" and "new"

Slide 98

Slide 98 text

Generational Root A B D C Gen 0 Gen 1

Slide 99

Slide 99 text

Generational Root A B D C Gen 0 Gen 1

Slide 100

Slide 100 text

Generational Root A B D C Gen 0 Gen 1

Slide 101

Slide 101 text

Generational Root B D Gen 0 Gen 1

Slide 102

Slide 102 text

Generational Root B D Gen 0 Gen 1 E F G

Slide 103

Slide 103 text

Generational Root B D Gen 0 Gen 1 E F G

Slide 104

Slide 104 text

Generational Root B D Gen 0 Gen 1 E F G

Slide 105

Slide 105 text

Generational Root B D Gen 0 Gen 1 F G

Slide 106

Slide 106 text

Generational Root B D Gen 0 Gen 1 F G

Slide 107

Slide 107 text

We didn’t have to touch "B"

Slide 108

Slide 108 text

One Slight Problem

Slide 109

Slide 109 text

Generational Root B D Gen 0 Gen 1

Slide 110

Slide 110 text

Generational Root B D Gen 0 Gen 1 E U nused!

Slide 111

Slide 111 text

Remembered Set Root B D Gen 0 Gen 1 E

Slide 112

Slide 112 text

Remembered Set Root B D Gen 0 Gen 1 E Remember!

Slide 113

Slide 113 text

Remembered Set Root B D Gen 0 Gen 1 E

Slide 114

Slide 114 text

Remembered Set Root B D Gen 0 Gen 1 E

Slide 115

Slide 115 text

Remembered Set Root B D Gen 0 Gen 1 E

Slide 116

Slide 116 text

Important Words

Slide 117

Slide 117 text

Write Barrier

Slide 118

Slide 118 text

Remembered Set

Slide 119

Slide 119 text

Generational Fast(er)! Not so easy

Slide 120

Slide 120 text

"Stop the world"

Slide 121

Slide 121 text

Incremental GC

Slide 122

Slide 122 text

Tri-Color Marking

Slide 123

Slide 123 text

Object Colors • White: will be collected • Black: No reference to white objects, but reachable from the root • Gray: Reachable from root, but not yet scanned

Slide 124

Slide 124 text

Algorithm 1. Pick an object from the gray set and move it to the black set 2. For each object it references, move it to the gray set 3. Repeat 1 and 2 until the gray set is empty

Slide 125

Slide 125 text

Tri-Color Marking Root H G F C B D A E

Slide 126

Slide 126 text

Tri-Color Marking Root H G F C B D A E

Slide 127

Slide 127 text

Tri-Color Marking Root H G F C B D A E

Slide 128

Slide 128 text

Tri-Color Marking Root H G F C B D A E

Slide 129

Slide 129 text

Tri-Color Marking Root H G F C B D A E

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

What is the benefit?

Slide 132

Slide 132 text

We can interrupt steps!

Slide 133

Slide 133 text

Each step can be performed incrementally

Slide 134

Slide 134 text

Halting time is reduced

Slide 135

Slide 135 text

One Slight Problem

Slide 136

Slide 136 text

Tri-Color Marking Root F C B D A

Slide 137

Slide 137 text

Tri-Color Marking Root F C B D A G

Slide 138

Slide 138 text

Tri-Color Marking Root F C B D A G

Slide 139

Slide 139 text

Tri-Color Marking Root F C B D A G Write!

Slide 140

Slide 140 text

Important Words

Slide 141

Slide 141 text

Incremental

Slide 142

Slide 142 text

Write Barrier

Slide 143

Slide 143 text

Remembered Set

Slide 144

Slide 144 text

Minimize tracing

Slide 145

Slide 145 text

Decrease halting

Slide 146

Slide 146 text

Things our GC is not

Slide 147

Slide 147 text

Parallel

Slide 148

Slide 148 text

Real-Time

Slide 149

Slide 149 text

Compacting

Slide 150

Slide 150 text

Allocation Algorithm

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

Heap layout

Slide 154

Slide 154 text

malloc isn’t free GET IT?????

Slide 155

Slide 155 text

Large Chunk: Page (or Slab)

Slide 156

Slide 156 text

Page memory is contiguous

Slide 157

Slide 157 text

Each page holds a linked list

Slide 158

Slide 158 text

Nodes are called "slots"

Slide 159

Slide 159 text

Each slot is a Ruby object

Slide 160

Slide 160 text

Page Layout Page Ruby Object Ruby Object Ruby Object Ruby Object Ruby Object

Slide 161

Slide 161 text

"Free List"

Slide 162

Slide 162 text

Find the first open slot!

Slide 163

Slide 163 text

Move to the next space in the free list

Slide 164

Slide 164 text

"Bump Pointer" allocation

Slide 165

Slide 165 text

Full Pages Object Object Object Object Page Page

Slide 166

Slide 166 text

"Eden" pages are searched

Slide 167

Slide 167 text

GC Time Object Object Object Object ☠

Slide 168

Slide 168 text

Important Words

Slide 169

Slide 169 text

Slot

Slide 170

Slide 170 text

Page

Slide 171

Slide 171 text

Eden

Slide 172

Slide 172 text

Tomb

Slide 173

Slide 173 text

Interesting Allocation Hacks

Slide 174

Slide 174 text

Not every object requires allocation

Slide 175

Slide 175 text

1 Page: 16k

Slide 176

Slide 176 text

1 Object: 40 bytes

Slide 177

Slide 177 text

Pages are "aligned"

Slide 178

Slide 178 text

Not `malloc` but "aligned malloc" `posix_memalign`

Slide 179

Slide 179 text

Choose "40" as a multiple

Slide 180

Slide 180 text

Start = 40 >> start = 40 => 40 >> (start * 1).to_s(2).rjust(10, '0') => "0000101000" >> (start * 2).to_s(2).rjust(10, '0') => "0001010000" >> (start * 3).to_s(2).rjust(10, '0') => "0001111000" >> (start * 4).to_s(2).rjust(10, '0') => "0010100000" >> (start * 5).to_s(2).rjust(10, '0') => "0011001000" >> (start * 6).to_s(2).rjust(10, '0') => "0011110000" >> (start * 7).to_s(2).rjust(10, '0') => "0100011000"

Slide 181

Slide 181 text

"0000101000" "0001010000" "0001111000" "0010100000" "0011001000" "0011110000" "0100011000"

Slide 182

Slide 182 text

Use these bits to add meaning

Slide 183

Slide 183 text

Represent Integers Without Allocation

Slide 184

Slide 184 text

Encode Number 2 >> INT_FLAG = 0x1 => 1 >> 2.to_s(2) => "10" >> ((2 << 1) | INT_FLAG).to_s(2) => "101"

Slide 185

Slide 185 text

Decode Number 2 >> 0b101 => 5 >> 0b101 >> 1 => 2

Slide 186

Slide 186 text

Biggest Fixnum >> ((2 ** 64) - 1).to_s(2) => "11111111111111111111111111111111111111111111111 11111111111111111" >> ((2 ** 64) - 1).class => Bignum >> ((2 ** 63) - 1).class => Bignum >> ((2 ** 62) - 1).class => Fixnum >> ((2 ** 62)).class => Bignum

Slide 187

Slide 187 text

Biggest Before Heap Allocation >> ((2 ** 64) - 1).to_s(2) => "11111111111111111111111111111111111111111111111 11111111111111111" >> ((2 ** 64) - 1).class => Integer >> ((2 ** 63) - 1).class => Integer >> ((2 ** 62) - 1).class => Integer >> ((2 ** 62)).class => Integer R uby 2.4

Slide 188

Slide 188 text

Fixnums are singletons >> ((2 ** 62) - 1).object_id => 9223372036854775807 >> ((2 ** 62) - 1).object_id => 9223372036854775807 >> ((2 ** 62)).object_id => 70213216135940 >> ((2 ** 62)).object_id => 70213216117840

Slide 189

Slide 189 text

Tagged Pointer

Slide 190

Slide 190 text

Tagged Pointers • Fixnum • Floats • True / False / Nil • Symbols

Slide 191

Slide 191 text

Allocation Problems

Slide 192

Slide 192 text

Poor Reclamation Object Object Object Object Object Object Object Object Object Object Object Object

Slide 193

Slide 193 text

Poor Reclamation Object Object Object Object Object Object Object Object NOPE

Slide 194

Slide 194 text

Poor Reclamation Object Object Object Object Object Object Object Object

Slide 195

Slide 195 text

CoW problems

Slide 196

Slide 196 text

1 Ruby Memory Page != 1 OS Memory Page

Slide 197

Slide 197 text

1 Ruby Page: 16k 1 OS Page: 4k

Slide 198

Slide 198 text

Object Object Object Parent Process Child Process Object

Slide 199

Slide 199 text

OS Copies 1 OS Page

Slide 200

Slide 200 text

We wrote 40 bytes, but 4kb got copied.

Slide 201

Slide 201 text

Solution: Group Old Objects

Slide 202

Slide 202 text

Two Page Types Probably Old Page Page Object Object

Slide 203

Slide 203 text

What is going to be old?

Slide 204

Slide 204 text

Probably Old class Foo end module Bar CONSTANT = Object.new def foo "frozen string".freeze end end

Slide 205

Slide 205 text

Statistically Determined

Slide 206

Slide 206 text

Efficiently use space

Slide 207

Slide 207 text

Reduce GC time

Slide 208

Slide 208 text

CoW Friendly

Slide 209

Slide 209 text

GC Work G GitHub

Slide 210

Slide 210 text

No content

Slide 211

Slide 211 text

No content

Slide 212

Slide 212 text

Key Objects Class / Module Empty

Slide 213

Slide 213 text

No content

Slide 214

Slide 214 text

No content

Slide 215

Slide 215 text

No content

Slide 216

Slide 216 text

No content

Slide 217

Slide 217 text

No content

Slide 218

Slide 218 text

No content

Slide 219

Slide 219 text

~17% Smaller Heap

Slide 220

Slide 220 text

No content

Slide 221

Slide 221 text

github.com/ github/ruby

Slide 222

Slide 222 text

Future Work

Slide 223

Slide 223 text

Moving Objects

Slide 224

Slide 224 text

Poor Reclamation Object Object Object Object Object Object Object Object NOPE YEP

Slide 225

Slide 225 text

Stack Scanning, Forward Pointers

Slide 226

Slide 226 text

GC Introspection

Slide 227

Slide 227 text

Seeing GC info

Slide 228

Slide 228 text

GC.stat {:count=>21, :heap_allocated_pages=>87, :heap_sorted_length=>87, :heap_allocatable_pages=>0, :heap_available_slots=>35460, :heap_live_slots=>35342, :heap_free_slots=>118, :heap_final_slots=>0, :heap_marked_slots=>22207, :heap_eden_pages=>87, :heap_tomb_pages=>0, :total_allocated_pages=>87, :total_freed_pages=>0, :total_allocated_objects=>208577, :total_freed_objects=>173235, :malloc_increase_bytes=>5152, :malloc_increase_bytes_limit=>16777216, :minor_gc_count=>19, :major_gc_count=>2, :remembered_wb_unprotected_objects=>189, :remembered_wb_unprotected_objects_limit=>374, :old_objects=>21977, :old_objects_limit=>39876, :oldmalloc_increase_bytes=>485600, :oldmalloc_increase_bytes_limit=>16777216}

Slide 229

Slide 229 text

GC Performance

Slide 230

Slide 230 text

GC::Profiler >> GC::Profiler.enable => nil >> GC::Profiler.report => nil >> GC.start => nil >> GC::Profiler.report GC 22 invokes. Index Invoke Time(sec) Use Size(byte) Total Size(byte) Total Object GC Time(ms) 1 0.143 906920 1419840 35496 3.72500000000000586198 => nil >> GC.start => nil >> GC::Profiler.report GC 23 invokes. Index Invoke Time(sec) Use Size(byte) Total Size(byte) Total Object GC Time(ms) 1 0.143 906920 1419840 35496 3.72500000000000586198 2 0.148 906920 1419840 35496 3.47800000000000864020

Slide 231

Slide 231 text

GC::Profiler.enable

Slide 232

Slide 232 text

GC::Profiler.report

Slide 233

Slide 233 text

Heap Inspection

Slide 234

Slide 234 text

ObjectSpace.dump_all

Slide 235

Slide 235 text

ObjectSpace.dump >> require 'objspace' => false >> x = Object.new => # >> ObjectSpace.dump x => "{\"address\":\"0x007fbcd09334a8\", \"type\":\"OBJECT\", \"class\": \"0x007fbcd08dd878\", \"ivars\":0, \"memsize\":40, \"flags\": {\"wb_protected\":true}}\n"

Slide 236

Slide 236 text

ObjectSpace.dump >> x = Object.new => # >> JSON.parse(ObjectSpace.dump(x))['flags'] => {"wb_protected"=>true} >> GC.start => nil >> JSON.parse(ObjectSpace.dump(x))['flags'] => {"wb_protected"=>true} >> GC.start => nil >> JSON.parse(ObjectSpace.dump(x))['flags'] => {"wb_protected"=>true} >> GC.start => nil >> JSON.parse(ObjectSpace.dump(x))['flags'] => {"wb_protected"=>true, "old"=>true, "uncollectible"=>true, "marked"=>true}

Slide 237

Slide 237 text

Object have 3 generations

Slide 238

Slide 238 text

ObjectSpace

Slide 239

Slide 239 text

trace_object_allocations

Slide 240

Slide 240 text

GC Tuning

Slide 241

Slide 241 text

RUBY_GC_HEAP _FREE_SLOTS Number of free slots available after a GC

Slide 242

Slide 242 text

RUBY_GC_HEAP _INIT_SLOTS Number of free slots to initialize the GC with

Slide 243

Slide 243 text

RUBY_GC_HEAP_GR OWTH_MAX_SLOTS Never grow more than this many objects

Slide 244

Slide 244 text

THANKS!

Slide 245

Slide 245 text

No content