WHO TAKES OUT YOUR TRASH? @sannekalkman

🔮

🔮 💻

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

🧠 Allocate memory

🧠 🔍 Allocate memory Find the garbage

🧠 🔍 🗑 Allocate memory Find the garbage Free up memory

📚 Running out of memory

📚 🔥 Running out of memory Breaking things

DANGLING POINTERS

DANGLING POINTERS

DANGLING POINTERS 🔥

DANGLING POINTERS 🔥 Memory Leak

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

REFERENCE COUNTING

REFERENCE COUNTING name = “Ruby”

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

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

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

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

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

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

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

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

CYCLES

CYCLES a = { other: nil }

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

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

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

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

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

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

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

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

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

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

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

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

COPYING COLLECTION Keep your stuff together

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

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

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

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

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

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

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

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

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

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

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

GENERATIONAL COLLECTION New objects Old objects D

WHAT ABOUT RUBY? MRI garbage collection

FREE LIST

FREE LIST

FREE LIST

FREE LIST

FREE LIST

FREE LIST

FREE LIST

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

MARK & SWEEP

LAZY SWEEPING

LAZY SWEEPING

LAZY SWEEPING

LAZY SWEEPING

LAZY SWEEPING

LAZY SWEEPING

GENERATIONAL COLLECTION ) (

GENERATIONAL COLLECTION ) (

GENERATIONAL COLLECTION ) (

GENERATIONAL COLLECTION ) (

GENERATIONAL COLLECTION ) (

GENERATIONAL COLLECTION ) (

GENERATIONAL COLLECTION ) (

GENERATIONAL COLLECTION ) (

WRITE BARRIERS

WRITE BARRIERS

WRITE BARRIERS

WRITE BARRIERS

WRITE BARRIERS

WRITE BARRIERS *

☀ Sunny

☀ 🕶 Sunny Shady

🔮 💻 Garbage collection is no longer magic!

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