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 = “codeBEAM”

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

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

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

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

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

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

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

REFERENCE COUNTING name name = “codeBEAM” other_name = name “codeBEAM” 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 THE BEAM? Erlang & Elixir garbage collection

📬 Processes

📬 🚫 Processes Immutability

MEMORY LAYOUT Shared

MEMORY LAYOUT Process A A Shared

MEMORY LAYOUT Process B Process A A B Shared

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

MEMORY LAYOUT Process B B Process C C Shared

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

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

PROCESS MEMORY Heap Stack

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

BINARY HEAP

BINARY HEAP ✉ Binaries > 64 bytes

BINARY HEAP ✉ 🔢 Binaries > 64 bytes Reference counted

đź”® đź’» 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 Erlang Garbage Collector - Lukas Larsson (Erlang Solutions)