Slide 1

Slide 1 text

GC You Later, Allocator github.com/swankjesse/memory

Slide 2

Slide 2 text

Let’s Make Breakfast

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Kitchen Counters You allocate space You do stuff in that space You clean it up and start again

Slide 6

Slide 6 text

Counter Space is Precious More counter space lets you do more things! When counter space is tight, you thrash

Slide 7

Slide 7 text

Locality Items that are used together should be close together

Slide 8

Slide 8 text

Let’s Make an App

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

RUN!

Slide 13

Slide 13 text

9:57 9:57

Slide 14

Slide 14 text

9:57 9:57

Slide 15

Slide 15 text

Compose UI ANDROID iOS SwiftUI SHARED NearbyGator GatorsService Code

Slide 16

Slide 16 text

data class NearbyGator( val name: String, val distance: Double, )

Slide 17

Slide 17 text

/** * Detects green toothy gators using Bluetooth GATT ERS. */ interface GatorsService : AutoCloseable { fun addListener(listener: Listener) fun removeListener(listener: Listener) interface Listener { fun onGators(nearbyGators: List) } }

Slide 18

Slide 18 text

class NearbyGatorsContent( gatorsService: GatorsService, ) : GatorsService.Listener { var list by mutableStateOf>(listOf()) init { gatorsService.addListener(this) } override fun onGators(nearbyGators: List) { list = nearbyGators } }

Slide 19

Slide 19 text

class NearbyGatorsContent: ObservableObject, GatorsServiceListener { @Published var array: [NearbyGator] = [] init(gatorsService: GatorsService) { gatorsService.addListener(listener: self) } func onGators(nearbyGators: [NearbyGator]) { self.array = nearbyGators } }

Slide 20

Slide 20 text

@Composable fun Content() { if (showNearbyGators) { NearbyGatorsScreen( content = NearbyGatorsContent(service), onDismiss = { showNearbyGators = false }, ).Show() } ... }

Slide 21

Slide 21 text

struct ContentView: View { ... var body: some View { Button( ... ) .sheet(isPresented: $showNearbyGators) { NearbyGatorsView( NearbyGatorsContent(service), $showNearbyGators ) } } }

Slide 22

Slide 22 text

Logical Memory

Slide 23

Slide 23 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) NearbyGator name "Chompy" distance 5.5

Slide 24

Slide 24 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) String data "Chompy" NearbyGator name "Chompy" distance 5.5

Slide 25

Slide 25 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) String data "Chompy" NearbyGator name "Chompy" distance 5.5

Slide 26

Slide 26 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" NearbyGator name "Chompy" distance 5.5

Slide 27

Slide 27 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" NearbyGator name "Chompy" distance 5.5

Slide 28

Slide 28 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5

Slide 29

Slide 29 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 Class name "NearbyGator" superclass Any ...

Slide 30

Slide 30 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 Class name "NearbyGator" superclass Any ...

Slide 31

Slide 31 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 Class name "NearbyGator" superclass Any ... Class name "Any" superclass null ...

Slide 32

Slide 32 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 Class name "NearbyGator" superclass Any ... Class name "Any" superclass null ...

Slide 33

Slide 33 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 class Class name "NearbyGator" superclass Any ... class Class name "Any" superclass null ...

Slide 34

Slide 34 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 class Class name "NearbyGator" superclass Any ... class Class name "Any" superclass null ... class Class name "Class" superclass Any ...

Slide 35

Slide 35 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 class Class name "NearbyGator" superclass Any ... class Class name "Any" superclass null ... class Class name "Class" superclass Any ...

Slide 36

Slide 36 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 class Class name "NearbyGator" superclass Any ... class Class name "Any" superclass null ... class Class name "Class" superclass Any ...

Slide 37

Slide 37 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y String data "Chompy" class NearbyGator name "Chompy" distance 5.5 class Class name "NearbyGator" superclass Any ... class Class name "Any" superclass null ... class Class name "Class" superclass Any ...

Slide 38

Slide 38 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) class CharArray length 6 0 C 1 h 2 o 3 m 4 p 5 y class String data "Chompy" class NearbyGator name "Chompy" distance 5.5 class Class name "NearbyGator" superclass Any ... class Class name "Any" superclass null ... class Class name "Class" superclass Any ...

Slide 39

Slide 39 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) CharArray 6 C h o m p y String "Chompy" NearbyGator "Chompy" 5.5 Class "NearbyGator" Any ... Class "Any" null ... Class "Class" Any ...

Slide 40

Slide 40 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) 6 C h o m p y 5.5 ... null ... ...

Slide 41

Slide 41 text

val chompy = NearbyGator( name = "Chompy", distance = 5.5, ) 0x06 0x43 0x68 0x6f 0x6d 0x70 0x79 0x40160000000000 ... 0x00 ... ...

Slide 42

Slide 42 text

Logical Memory is a Big Graph! Each object is just a combination of: • Values like Doubles and Chars • References to other objects Your program works by manipulating this graph

Slide 43

Slide 43 text

Physical Memory

Slide 44

Slide 44 text

iFixit, https://www.ifixit.com/Teardown/Google+Pixel+XL+Teardown/71237

Slide 45

Slide 45 text

iFixit, https://www.ifixit.com/Teardown/Google+Pixel+XL+Teardown/71237

Slide 46

Slide 46 text

iFixit, https://www.ifixit.com/Teardown/Google+Pixel+XL+Teardown/71237

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

RAM: Random Access Memory Like a ByteArray(4294967296) Keeps state for the OS and all running apps Physical Memory is a Big ByteArray!

Slide 49

Slide 49 text

Allocating Memory

Slide 50

Slide 50 text

The allocator’s job is to bridge the gap between ‘Big Graph’ and ‘Big ByteArray’

Slide 51

Slide 51 text

The Simplest Allocator Allocate memory in sequence, as requested When you run out of memory, crash! targets.withType().all { compilations.configureEach { compileTaskProvider.configure { compilerOptions.freeCompilerArgs.add("-Xgc=noop") } } }

Slide 52

Slide 52 text

0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x

Slide 53

Slide 53 text

var nextOffset = 160000 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x

Slide 54

Slide 54 text

var nextOffset = 160000 val referenceSize = 4 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x

Slide 55

Slide 55 text

var nextOffset = 160000 val referenceSize = 4 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000

Slide 56

Slide 56 text

var nextOffset = 160000 val referenceSize = 4 10 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000

Slide 57

Slide 57 text

var nextOffset = 160000 val referenceSize = 4 10 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160010

Slide 58

Slide 58 text

var nextOffset = 160000 val referenceSize = 4 10 21 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160010

Slide 59

Slide 59 text

var nextOffset = 160000 val referenceSize = 4 10 21 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160010

Slide 60

Slide 60 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160010

Slide 61

Slide 61 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160034 160010

Slide 62

Slide 62 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 39 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160034 160010

Slide 63

Slide 63 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 39 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160034 160039 160010

Slide 64

Slide 64 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 39 45 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160034 160039 160010

Slide 65

Slide 65 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 39 45 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160034 160039 160045 160010

Slide 66

Slide 66 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 39 45 56 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 160000 160021 160034 160039 160045 160010

Slide 67

Slide 67 text

var nextOffset = 160000 val referenceSize = 4 10 21 34 39 45 56 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 0x00027100 0x00027115 0x00027122 0x00027127 0x0002712d 0x000271a0

Slide 68

Slide 68 text

var nextOffset = 160056 val referenceSize = 4 0x00027122 0x0002712d 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x 0x000271a0 0x00027115 0x00027115 0x0002712d 0x00027127 0x00027127 0x00027100 0x00027100 0x00027100 0x00027100 0x00027100 0x000271a0 0x000271a0

Slide 69

Slide 69 text

0x000271a0 0x00027115 0x00027122 0x00027127 0x0002712d 0x00027100 0x00027100 0x00027100 0x000271a0 0x000271a0 0x00027115 0x0002712d 0x00027127 0x99 0xfe 0xb0b15c00 0x0465 0x00 0x09 0xcafebabe 0x08675309 0x43 0x06 0x68 0x6d 0x6f 0x79 0x70 0xc0 4016000000000000 0x var nextOffset = 160056 val referenceSize = 4 0x00027100 0x00027100

Slide 70

Slide 70 text

0x00027100 0x000271a0 0x00027115 0x00027122 0x00027127 0x0002712d 00027100 00027100 00027100 000271a0 000271a0 00027115 0002712d 00027127 99 fe b0b15c00 0465 00 09 cafebabe 4016000000000000 08675309 43 06 68 6d 6f 79 70 c0 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x 0x var nextOffset = 160056 val referenceSize = 4

Slide 71

Slide 71 text

00027100 00027100 00027100 000271a0 000271a0 00027115 0002712d 00027127 99 fe b0b15c00 0465 00 09 cafebabe 4016000000000000 08675309 43 06 68 6d 6f 79 70 c0 var nextOffset = 160056 val referenceSize = 4

Slide 72

Slide 72 text

Real Allocators They don’t keep everything in the big graph! • Some objects in the graph have no incoming references • Nobody will know if we stop tracking them! You can create temporary objects without running out of memory

Slide 73

Slide 73 text

Garbage Collection A batch process to find & free unnecessary objects 1. Mark every object you can reach 2. Free the rest Used by the JVM, ART, Kotlin/Native, JavaScript, and Golang

Slide 74

Slide 74 text

class NearbyGatorsContent { var list = listOf() override fun onGators(nearbyGators: List) { list = nearbyGators } }

Slide 75

Slide 75 text

List size 1 0 NearbyGator name "Lyle" distance 4.57 NearbyGators Content list HomeActivity scope ... model

Slide 76

Slide 76 text

List size 1 0 NearbyGator name "Lyle" distance 4.57 NearbyGators Content list NearbyGator name "Smiley" distance 7.64 NearbyGator name "Chompy" distance 9.58 List size 2 0 1 HomeActivity scope ... model

Slide 77

Slide 77 text

List size 1 0 NearbyGator name "Lyle" distance 4.57 NearbyGators Content list NearbyGator name "Smiley" distance 7.64 NearbyGator name "Chompy" distance 9.58 List size 2 0 1 HomeActivity scope ... model

Slide 78

Slide 78 text

List size 1 0 NearbyGator name "Lyle" distance 4.57 NearbyGators Content list NearbyGator name "Smiley" distance 7.64 NearbyGator name "Chompy" distance 9.58 List size 2 0 1 HomeActivity scope ... model

Slide 79

Slide 79 text

List size 1 0 NearbyGator name "Lyle" distance 4.57 NearbyGators Content list NearbyGator name "Smiley" distance 7.64 NearbyGator name "Chompy" distance 9.58 List size 2 0 1 HomeActivity scope ... model ✔ ✔ ✔ ✔ ✔

Slide 80

Slide 80 text

NearbyGators Content list NearbyGator name "Smiley" distance 7.64 NearbyGator name "Chompy" distance 9.58 List size 2 0 1 HomeActivity scope ... model ✔ ✔ ✔ ✔ ✔

Slide 81

Slide 81 text

Reference Counting Each object tracks how many other objects reference it When the count reaches zero, it self-destructs Used by Swift, Python, C++’s std::shared_ptr, and Rust’s Rc

Slide 82

Slide 82 text

class NearbyGatorsContent { var array: [NearbyGator] = [] func onGators(array: [NearbyGator]) { self.array = array } }

Slide 83

Slide 83 text

NearbyGators Content ref count 1 array NearbyGator ref count 1 name "Lyle" distance 4.57 HomeView ref count 1 model Array ref count 1 size 1 0

Slide 84

Slide 84 text

NearbyGators Content ref count 1 array NearbyGator ref count 1 name "Chompy" distance 9.58 NearbyGator ref count 1 name "Smiley" distance 7.64 NearbyGator ref count 1 name "Lyle" distance 4.57 HomeView ref count 1 model Array ref count 1 size 1 0 Array ref count 1 size 1 0 1

Slide 85

Slide 85 text

NearbyGators Content ref count 1 array NearbyGator ref count 1 name "Chompy" distance 9.58 NearbyGator ref count 1 name "Smiley" distance 7.64 NearbyGator ref count 1 name "Lyle" distance 4.57 HomeView ref count 1 model Array ref count 1 size 1 0 Array ref count 1 size 1 0 1 0

Slide 86

Slide 86 text

NearbyGators Content ref count 1 array NearbyGator ref count 1 name "Chompy" distance 9.58 NearbyGator ref count 1 name "Smiley" distance 7.64 NearbyGator ref count 1 name "Lyle" distance 4.57 HomeView ref count 1 model Array ref count 1 size 1 0 1 0

Slide 87

Slide 87 text

NearbyGators Content ref count 1 array NearbyGator ref count 1 name "Chompy" distance 9.58 NearbyGator ref count 1 name "Smiley" distance 7.64 HomeView ref count 1 model Array ref count 1 size 1 0 1

Slide 88

Slide 88 text

NearbyGators Content ref count 1 array NearbyGator ref count 1 name "Chompy" distance 9.58 NearbyGator ref count 1 name "Smiley" distance 7.64 HomeView ref count 1 model Array ref count 1 size 1 0 1 2

Slide 89

Slide 89 text

Leaking Memory

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Leaks Wasted counter space wrecks your cooking performance Wasted memory can wreck your app’s performance Impact is most severe for long-running processes: • Servers • Point-of-sale apps

Slide 93

Slide 93 text

class NearbyGatorsContent( gatorsService: GatorsService, ) : GatorsService.Listener { var list by mutableStateOf>(listOf()) init { gatorsService.addListener(this) } override fun onGators(nearbyGators: List) { list = nearbyGators } }

Slide 94

Slide 94 text

class NearbyGatorsContent( gatorsService: GatorsService, ) : GatorsService.Listener { var list by mutableStateOf>(listOf()) init { gatorsService.addListener(this) } override fun onGators(nearbyGators: List) { list = nearbyGators } }

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

class NearbyGatorsContent: ObservableObject, GatorsServiceListener { @Published var array: [NearbyGator] = [] init(gatorsService: GatorsService) { gatorsService.addListener(listener: self) } func onGators(array: [NearbyGator]) { self.array = array } }

Slide 101

Slide 101 text

class NearbyGatorsContent: ObservableObject, GatorsServiceListener { @Published var array: [NearbyGator] = [] init(gatorsService: GatorsService) { gatorsService.addListener(listener: self) } func onGators(array: [NearbyGator]) { self.array = array } }

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

XCODE TIPS 5

Slide 107

Slide 107 text

FLATTEN KOTLIN/NATIVE ALLOCATIONS #1 MaNY OBJECTS SHARE A NODE

Slide 108

Slide 108 text

FLATTEN KOTLIN/NATIVE ALLOCATIONS #1 kotlin { targets.withType().all { compilations.configureEach { compileTaskProvider.configure { // This makes the Xcode memory analyzer graphs easier to // understand. Be careful when using this in production; // it's not as stable as the default allocator. // (See KT-68769) compilerOptions.freeCompilerArgs.add("-Xallocator=std") // Work around an issue with the above option! compilerOptions.freeCompilerArgs.add("-opt") } } } }

Slide 109

Slide 109 text

FLATTEN KOTLIN/NATIVE ALLOCATIONS #1

Slide 110

Slide 110 text

FLATTEN KOTLIN/NATIVE ALLOCATIONS #1 Task :shared:linkDebugFrameworkIosSimulatorArm64 FAILED /Users/jwilson/.gradle/.../kotlinx-coroutines-core is cached (in ~/.konan/kotlin-native-prebuilt-macos-aarch64-2.0.0/...), but its dependency isn't: /Users/jwilson/.konan/.../stdlib $ rm -rf ~/.konan/kotlin-native-prebuilt-macos-aarch64-2.0.0

Slide 111

Slide 111 text

FLATTEN KOTLIN/NATIVE ALLOCATIONS #1

Slide 112

Slide 112 text

FLATTEN KOTLIN/NATIVE ALLOCATIONS #1 MORE NODES!

Slide 113

Slide 113 text

PAY ATTENTION TO ZOOM #2

Slide 114

Slide 114 text

PAY ATTENTION TO ZOOM #2 Labeled Nodes

Slide 115

Slide 115 text

PAY ATTENTION TO ZOOM #2 Labeled Edges Labeled Nodes

Slide 116

Slide 116 text

PRINT DESCRIPTION #3

Slide 117

Slide 117 text

PRINT DESCRIPTION #3 THAT’S TOSTRING()

Slide 118

Slide 118 text

STACK LOGGING #4

Slide 119

Slide 119 text

STACK LOGGING #4

Slide 120

Slide 120 text

STACK LOGGING #4

Slide 121

Slide 121 text

STACK LOGGING #4

Slide 122

Slide 122 text

STACK LOGGING #4

Slide 123

Slide 123 text

STACK LOGGING #4

Slide 124

Slide 124 text

STACK LOGGING #4 USELESS LABELS

Slide 125

Slide 125 text

STACK LOGGING #4 USELESS LABELS

Slide 126

Slide 126 text

STACK LOGGING #4

Slide 127

Slide 127 text

STACK LOGGING #4

Slide 128

Slide 128 text

STACK LOGGING #4 CALL STACK WHEN IT WAS ALLOCATED!

Slide 129

Slide 129 text

FORCE GC #5 132 INSTANCEs }

Slide 130

Slide 130 text

/** * Call this in debug builds to collect Kotlin objects and chains * of Kotlin + Swift objects. This isn't necessary for correct * application behavior, but it is helpful when doing memory * analysis because it removes unreachable objects. * * This runs a Kotlin GC which may have the side effect of * queueing some Swift objects to be eligible for `deinit()`. * And running that `deinit()` on the main thread will potentially * make some Kotlin objects eligible for collection! */ @OptIn(NativeRuntimeApi::class) object KotlinMemory { fun blockingCollect() { GC.collect() } } FORCE GC #5

Slide 131

Slide 131 text

FORCE GC #5 9 INSTANCEs }

Slide 132

Slide 132 text

XCODE TIPS 5 FLATTEN KOTLIN/NATIVE ALLOCATIONS PAY ATTENTION TO ZOOM PRINT DESCRIPTION 1. 2. 3. 4. 5. STACK LOGGING FORCE GC

Slide 133

Slide 133 text

Memory Leaks on Android

Slide 134

Slide 134 text

Just Use LeakCanary It detects leaks automatically: • It knows the lifecycles for common Android objects • If an object is still reachable in the Big Graph after its lifecycle is done, it probably leaked The leak trace shows you what to fix

Slide 135

Slide 135 text

YourKit Too Export an .hprof file from Android Studio Open that in YourKit Much more capable than Android Studio’s heap viewer

Slide 136

Slide 136 text

class NearbyGatorsContent( private val gatorsService: GatorsService, ) : GatorsService.Listener { val list = mutableStateListOf() init { gatorsService.addListener(this) } override fun onGators(nearbyGators: List) { list.clear() list.addAll(nearbyGators) } } }

Slide 137

Slide 137 text

class NearbyGatorsContent( private val gatorsService: GatorsService, ) : GatorsService.Listener { val list = mutableStateListOf() init { gatorsService.addListener(this) } override fun onGators(nearbyGators: List) { list.clear() list.addAll(nearbyGators) } }

Slide 138

Slide 138 text

class NearbyGatorsContent( private val gatorsService: GatorsService, ) : GatorsService.Listener { val list = mutableStateListOf() init { gatorsService.addListener(this) } override fun onGators(nearbyGators: List) { list.clear() list.addAll(nearbyGators) } fun close() { gatorsService.removeListener(this) } }

Slide 139

Slide 139 text

class NearbyGatorsContent( private val gatorsService: GatorsService, ) : GatorsService.Listener { val list = mutableStateListOf() init { gatorsService.addListener(this) } override fun onGators(nearbyGators: List) { list.clear() list.addAll(nearbyGators) } fun close() { gatorsService.removeListener(this) } }

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

Memory Leaks on iOS using Kotlin/Native

Slide 147

Slide 147 text

Just Use LeakSwift Just joking! iOS doesn’t have a P-Y There’s a couple of libraries but nothing as easy or capable

Slide 148

Slide 148 text

class NearbyGatorsContent: ObservableObject, GatorsServiceListener { private let service: GatorsService @Published var array: [NearbyGator] = [] init(service: GatorsService) { self.service = service service.addListener(listener: self) } func onGators(array: [NearbyGator]) { self.array = array } } }

Slide 149

Slide 149 text

class NearbyGatorsContent: ObservableObject, GatorsServiceListener { private let service: GatorsService @Published var array: [NearbyGator] = [] init(service: GatorsService) { self.service = service service.addListener(listener: self) } func onGators(array: [NearbyGator]) { self.array = array } }

Slide 150

Slide 150 text

deinit { service.removeListener(listener: self) } } class NearbyGatorsContent: ObservableObject, GatorsServiceListener { private let service: GatorsService @Published var array: [NearbyGator] = [] init(service: GatorsService) { self.service = service service.addListener(listener: self) } func onGators(array: [NearbyGator]) { self.array = array }

Slide 151

Slide 151 text

class NearbyGatorsContent: ObservableObject, GatorsServiceListener { private let service: GatorsService @Published var array: [NearbyGator] = [] init(service: GatorsService) { self.service = service service.addListener(listener: self) } func onGators(array: [NearbyGator]) { self.array = array } deinit { service.removeListener(listener: self) } }

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

List size 0 0 GatorsService listeners ... NearbyGators Content ref count 1 service ... HomeView ref count 1 model

Slide 156

Slide 156 text

List size 0 0 GatorsService listeners ... NearbyGators Content ref count 1 service ... HomeView ref count 1 model 1

Slide 157

Slide 157 text

List size 0 0 GatorsService listeners ... NearbyGators Content ref count 1 service ... 2 HomeView ref count 1 model 1

Slide 158

Slide 158 text

List size 0 0 GatorsService listeners ... NearbyGators Content ref count 1 service ... 2 HomeView ref count 1 model 1 1

Slide 159

Slide 159 text

List size 0 0 GatorsService listeners ... NearbyGators Content ref count 1 service ... 2 HomeView ref count 1 model 1 1 ✔ ✔

Slide 160

Slide 160 text

func onGators(array: [NearbyGator]) { } .array = array private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject init(service: GatorsService) { self.service = service service.addListener( listener: )def deinit { service.removeListener( } listener: )ghi GatorsServiceListener }nop self. self self }xxz ,

Slide 161

Slide 161 text

private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject init(service: GatorsService) { self.service = service service.addListener( )def deinit { service.removeListener( } )ghi }nop }xxz , func onGators(array: [NearbyGator]) { } .array = array self. GatorsServiceListener listener: listener: self self

Slide 162

Slide 162 text

func onGators(array: [NearbyGator]) { } .array = array content? private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject init(service: GatorsService) { self.service = service service.addListener( )def }xxz )ghi GatorsServiceListener {hij }klm }nop private class Listener : var content: NearbyGatorsContent? = nil deinit { service.removeListener( } listener: listener: self self

Slide 163

Slide 163 text

func onGators(array: [NearbyGator]) { } .array = array content? private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject init(service: GatorsService) { self.service = service service.addListener( )def }xxz )ghi GatorsServiceListener {hij }klm }nop private class Listener : var content: NearbyGatorsContent? = nil deinit { service.removeListener( } listener: listener listener: listener

Slide 164

Slide 164 text

deinit { service.removeListener( } func onGators(array: [NearbyGator]) { } .array = array content? private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject private let listener: Listener init(service: GatorsService) { self.service = service service.addListener(listener: listener )def self.listener = Listener() self.listener.content = self }xxz listener: listener )ghi GatorsServiceListener {hij }klm }nop private class Listener : var content: NearbyGatorsContent? = nil

Slide 165

Slide 165 text

deinit { service.removeListener( } func onGators(array: [NearbyGator]) { } .array = array content? private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject private let listener: Listener init(service: GatorsService) { self.service = service service.addListener(listener: listener )def self.listener = Listener() self.listener.content = self }xxz listener: listener )ghi GatorsServiceListener {hij }klm }nop private class Listener : var content: NearbyGatorsContent? = nil

Slide 166

Slide 166 text

deinit { service.removeListener( } func onGators(array: [NearbyGator]) { } .array = array content? private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject private let listener: Listener init(service: GatorsService) { self.service = service service.addListener(listener: listener )def self.listener = Listener() self.listener.content = self }xxz listener: listener )ghi GatorsServiceListener {hij }klm }nop private class Listener : var content: NearbyGatorsContent? = nil

Slide 167

Slide 167 text

deinit { service.removeListener( } func onGators(array: [NearbyGator]) { } .array = array content? private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject private let listener: Listener init(service: GatorsService) { self.service = service service.addListener(listener: listener )def self.listener = Listener() self.listener.content = self }xxz listener: listener )ghi weak GatorsServiceListener {hij }klm }nop private class Listener : var content: NearbyGatorsContent? = nil

Slide 168

Slide 168 text

deinit { service.removeListener( } func onGators(array: [NearbyGator]) { } .array = array content? private let service: GatorsService @Published var array: [NearbyGator] = [] {abc class NearbyGatorsContent: ObservableObject private let listener: Listener init(service: GatorsService) { self.service = service service.addListener(listener: listener )def self.listener = Listener() self.listener.content = self }xxz listener: listener )ghi weak GatorsServiceListener {hij }klm }nop private class Listener : var content: NearbyGatorsContent? = nil

Slide 169

Slide 169 text

NearbyGators Content ref count 1 service listener ... List size 0 0 GatorsService listeners ... HomeView ref count 1 model

Slide 170

Slide 170 text

NearbyGators Content ref count 1 service listener ... List size 0 0 GatorsService listeners ... HomeView ref count 1 model Listener ref count 1 content

Slide 171

Slide 171 text

NearbyGators Content ref count 1 service listener ... List size 0 0 GatorsService listeners ... HomeView ref count 1 model Listener ref count 1 content + 1 weak

Slide 172

Slide 172 text

NearbyGators Content ref count 1 service listener ... List size 0 0 GatorsService listeners ... HomeView ref count 1 model 1 Listener ref count 1 content + 1 weak

Slide 173

Slide 173 text

NearbyGators Content ref count 1 service listener ... List size 0 0 GatorsService listeners ... HomeView ref count 1 model 1 Listener ref count 1 content 2 + 1 weak

Slide 174

Slide 174 text

NearbyGators Content ref count 1 service listener ... List size 0 0 GatorsService listeners ... HomeView ref count 1 model 1 Listener ref count 1 content 2 + 1 weak

Slide 175

Slide 175 text

NearbyGators Content ref count 1 service listener ... List size 0 0 GatorsService listeners ... HomeView ref count 1 model 1 Listener ref count 1 content 2 0 + 1 weak

Slide 176

Slide 176 text

List size 0 0 GatorsService listeners ... HomeView ref count 1 model 1 Listener ref count 1 content 2 1

Slide 177

Slide 177 text

HomeView ref count 1 model Listener ref count 1 content 2 1 0

Slide 178

Slide 178 text

HomeView ref count 1 model

Slide 179

Slide 179 text

No content

Slide 180

Slide 180 text

No content

Slide 181

Slide 181 text

No content

Slide 182

Slide 182 text

It’s Difficult One big graph of objects with different rules for different objects: • Swift does reference counting • Kotlin does garbage collection

Slide 183

Slide 183 text

class Holder { var target: Any? = null } fun createSmallCycle() { val holder = Holder() val array = NSMutableArray() array.addObject(holder) holder.target = array } Retain Cycles without any Swift NSMutableArray ref count 1 size 1 0 Holder target

Slide 184

Slide 184 text

It’s not a problem while the objects are still in use The garbage collector can collect cycles as long as every property is declared in Kotlin A cycle* in the Big Graph with at least one NSObject** * ** Anatomy of a Leak

Slide 185

Slide 185 text

Avoiding Leaks, Systematically

Slide 186

Slide 186 text

How Swift Does It Every property is either an owned reference (default) or one of two other kinds, weak and unowned iOS developers are always thinking about this! It’s as bad as Java developers worrying about null DispatchQueue.main.async { [weak self] in ... }

Slide 187

Slide 187 text

No content

Slide 188

Slide 188 text

How I Avoid Leaks in Kotlin/Native Never reference a Swift object from a Kotlin object If I must break Rule #1: A. Give the referencing Kotlin class a close() function B. That function sets properties to null C. Make sure it gets called RULE #1 RULE #2

Slide 189

Slide 189 text

class DisplayLinkTarget( private var displayLink: CADisplayLink?, ) { ... fun close() { displayLink?.invalidate() displayLink = null // Break a reference cycle. } }

Slide 190

Slide 190 text

Why Break Cycles From Kotlin?

Slide 191

Slide 191 text

Why Break Cycles From Kotlin? Did ya get the latest Kotlin alligators library?

Slide 192

Slide 192 text

Did ya get the latest Kotlin alligators library? Yep! But Xcode is mad ’cause it’s leaking memory

Slide 193

Slide 193 text

Yep! But Xcode is mad ’cause it’s leaking memory Oh shit

Slide 194

Slide 194 text

Oh shit Oh shit

Slide 195

Slide 195 text

Oh shit It’s equally valid to break cycles from Swift, or a mix of both! Kotlin that doesn’t leak helps to earn iOS engineers' trust

Slide 196

Slide 196 text

Detecting Leaks in Tests We built a LeakWatcher test helper in Redwood Currently JVM-only It uses PhantomReference to check whether an object is referenced val leakWatcher = LeakWatcher { content.listener } content.close() leakWatcher.assertNotLeaked()

Slide 197

Slide 197 text

Detecting Leaks in Production We also built a LeakDetector API in Redwood Supports the JVM, Kotlin/Native, and Kotlin/JS It uses WeakReference to check whether an object is referenced leakDetector.watchReference( reference = listener, note = "content listener removed", )

Slide 198

Slide 198 text

Take Aways

Slide 199

Slide 199 text

Life Comes At You Swiftly Garbage Collection is great LeakCanary is great iOS has neither Every Swift object is a resource you might need to release Xcode can be coerced into helping you

Slide 200

Slide 200 text

Valhalla BONUS TOPIC

Slide 201

Slide 201 text

Java’s Epic Refactor Kotlin already has a limited version with @JvmInline value classes GOAL: HOW: PLUS: Increase the information density in the JVM’s memory By unifying primitives and references in the type system Better null handling!

Slide 202

Slide 202 text

record NearbyGator(String name, double distance) { public }

Slide 203

Slide 203 text

record NearbyGator(String name, double distance) { public }

Slide 204

Slide 204 text

value public } record NearbyGator(String name, double distance) {

Slide 205

Slide 205 text

record NearbyGator(String name, double distance) { public } value

Slide 206

Slide 206 text

0 1 2 3 size Array distance name NearbyGator 7.64 String data "Lyle" distance name NearbyGator 9.58 String data "Smiley" 5.31 distance name NearbyGator String data "Chompy" 0 1 2

Slide 207

Slide 207 text

0 1 2 3 size Array distance name NearbyGator 7.64 String data "Lyle" distance name 9.58 String data "Smiley" 5.31 distance name String data "Chompy" 0 1 2

Slide 208

Slide 208 text

Valhalla Started in 2014 as a research project You can download a build of JDK 23 that has value classes today

Slide 209

Slide 209 text

Thanks github.com/swankjesse/memory