SlideShare a Scribd company logo
Managing Memory in Swift
(Yes, that's a thing)
Swift Cloud Workshop 2 Austin, TX

30 September 2017
Illustration
Renders
by
https://pixabay.com/en/users/3dman_eu-1553824/
@CarlBrwn
Managing Memory in Swift
(Yes, that's a thing)
Swift Cloud Workshop 2 Austin, TX

30 September 2017
Illustration
Renders
by
https://pixabay.com/en/users/3dman_eu-1553824/
@CarlBrwn
Obligatory Bio
• Swift on the Server Developer at IBM

• First iOS App in 2008, many projects since

• Author, App Accomplished

• Meetup Organizer 

• SwiftAustin & CocoaCoders

• Parent
@CarlBrwn
Is Swift on the Server READY?
• In my (personal) Opinion, [NOT speaking
on behalf of IBM], this answer depends
on two things:

1. Do you need the ecosystem to have
more features that you can build
yourself?
in the Cloud
[For your app?]
@CarlBrwn
Tale of Two Ecosystems
• NPM Claims 475,000 available node.js
packages.

• IBM’s Swift Package Catalog has 4000 entries
(last I looked), and that number includes at
least some iOS-only packages.

• So if you want to do Server-Side Swift, be
prepared to roll your own. 

• Personally, I’m okay with writing my own
left-pad, but YMMV.
@CarlBrwn
Is Swift on the Server READY?
• In my (personal) Opinion, [NOT speaking
on behalf of IBM], this answer depends
on two things:

1. Do you need the ecosystem to have
more features that you can build
yourself?

2. Can you manage your own memory?
in the Cloud
[For your app?]
@CarlBrwn
Previously on
“Conferences
with Carl”:
try!Swift NYC 2017
• This talk expands on that talk

• It should be up on video at
Realm sometime soon

• Not necessary to have seen it
@CarlBrwn
0
35
70
105
140
PR Fixes
CodeOrdering Counting Encapsulation Logic(App) Memory
Naming Optionals Performance Threading Typing
Unclear
PRs Meeting Criteria (502 total)
?
Today’s Talk
@CarlBrwn
Memory (6.4%)
• Most people I talk to don’t think of Swift
Memory Management as a problem

• Some of the Apple folks I talked to
about it at WWDC this year were
surprised, too (at first)

• It is a real problem, and it’s serious

• And it’s worse on Linux…

• For 3 primary reasons:
@CarlBrwn
Reason 1: Duration
• Cloud apps run longer (in general)

• Cloud apps can’t restart themselves in
the background while you’re checking
Facebook

• Memory leaks are cumulative

• Cloud costs scale with dedicated RAM
@CarlBrwn
Reason 2: Tools
• We don’t have Xcode

• We don’t have Instruments

• We don’t have cycle detectors

• In fact, we don’t have any Swift-aware
memory diagnostics on Linux at all
@CarlBrwn
Reason 3: Structure
• iOS (and Mac) Apps have a Structure:

• Application Delegate

• View Controllers

• Views

• Well-tested clean-up code

• (e.g. Views reclaimed when removed from
the screen)

• By contrast, Linux has: main

• Biggest problem w/Swift on Linux (IMNSHO)
@CarlBrwn
BRIEF Intro to ARC
• Automatic Reference Counting

• In the Old Days (2009), we would call
retain, release or autorelease on each
object by hand

• Apple published rules about when you were
supposed to use each

• Apple wrote an Analyzer tool that would tell
you when you broke the rules

• ARC does for you what those rules said to do
1/6
@CarlBrwn
BRIEF Intro to ARC
• Each object has a counter

• (Originally this was part of the NSObject implementation, but now
it applies to more things, but I’m probably still going to say “object”
today)

• When you take a reference to something (like
putting it in an ivar), you increment its counter

• When you’re done with it (or the system is done
with you), its counter is decremented

• The object only knows its count. It has no idea
who is pointing at it.
2/6
@CarlBrwn
BRIEF Intro to ARC
• ARC keeps things around as long as their
reference counts are greater than zero

• When a counter drops to zero, the object is
available to be reclaimed

• Reclamation happens periodically and
deterministically, but not necessarily
instantly
3/6
@CarlBrwn
BRIEF Intro to ARC
• When an object is reclaimed, all the things
it’s referencing have their counts reduced
by one

• That often causes those things to get
reclaimed in turn, and so on

• Unlike GC, no marking or sweeping is
needed during reclamation

• The system doesn’t need to pause the
whole system to free up memory
RAM
ARC
Object
4/6
@CarlBrwn
ARC-Optimized Architecture
This is the world ARC wants (and expects)
5/6
BRIEF Intro to ARC
@CarlBrwn
BRIEF Intro to ARC
• When two things refer to each other, ARC is
powerless

• Neither count will ever be set to zero

• The objects can never be reclaimed

• Some Garbage Collectors in Other Languages
can find and reclaim these kinds of cycles at
runtime (with a performance penalty), but in
Swift it’s not happening

• If no other objects can reach either of these, you
get a classic leak
6/6
@CarlBrwn
Good Structures
• Clear lines of ownership

• Clear relationships & hierarchies

• Tend to use ivars/properties

• Tend not to remember things passed in
from outside (especially from caller)

• Tend to reinforce existing relationships
& events

• e.g. Memory gets naturally
reclaimed when views leave the
screen or network connections drop
@CarlBrwn
Bad Structures
• Tangled or unclear relationships

• Strong references to things passed in
from outside

• Things captured in closures

• Hierarchies not rooted in natural
events

• e.g. Needs clean up
functions instead of it
“just happening”

• “Missed Dominos”
@CarlBrwn
You have to Measure
• This is a necessary step - you can’t reliably quantify
memory issues from reading code (if you can, you
should be mining bitcoin in your head)

• This has to happen at runtime, static analysis won’t
help

• You need either a production-like synthetic load, or
to measure in actual production (or both)

• Honestly, I don’t think many people do this (even for
iOS)
@CarlBrwn
My Measuring Scripts are Available
https://github.com/carlbrown/SwiftServerComparison
May or may not work for your use-case, but you’re welcome to them
@CarlBrwn
Wish there was a better way
Run long test,
grab `ps` logs,
graph
Rollback,
Next or
Fixed?
Test with `heaptrack`
to find next
Area of interest
Change code
@CarlBrwn
Graph of RSS from `ps aux`
Moving the slope, One little fix at a time
@CarlBrwn
Variability can be a Problem
Hard to see the leak (signal) when leak is small compared to the spread (noise)

This was happening because of clean-up functions

As you run the test longer, it becomes less of an issue@CarlBrwn
Duration
This is a half hour run
@CarlBrwn
Duration (cont)
Same run, full duration (~5 hours)
@CarlBrwn
Wish there was a better way
Run long test,
grab `ps` logs,
graph
Rollback,
Next or
Fixed?
Test with `heaptrack`
to find next
Area of interest
@CarlBrwn
Darwin’s cool tools aren’t for you…
Nothing remotely like it on Linux

Even if you test on Darwin, there may still be Linux leaks
@CarlBrwn
…And that’s a real shame
Because the leaks that happen when you don’t have an 

App Delegate structure can get REALLY convoluted
@CarlBrwn
Use heaptrack & heaptrack_gui
Not constrained to single thread like valgrind

But no awareness of Swift reference counts or structure
@CarlBrwn
Wish there was a better way
Run long test,
grab `ps` logs,
graph
Rollback,
Next or
Fixed?
Test with `heaptrack`
to find next
Area of interest
Change code
@CarlBrwn
So what code do you change?
• heaptrack only gives us the line of code that allocated the object
that is “stuck" in memory (& you may want to run swift-demangle)

• Find all the places that object is used and especially all the times it’s
passed to a closure or as an argument 

• See if you can make them weak without crashing (or unowned, but
only if you have to - it’s not safe)

• See if making it weak at that point changes the slope of the graph

• Make only one change at a time, and measure

• Continue until you’ve found the right place(s) to make weak

• NOTE: This will only work if ownership is clear. If not, refactor
Good Question
@CarlBrwn
Most of my changes turn out to be dead-ends
The ones that make the slope better get merged to `master`
@CarlBrwn
Thank You
& Good Luck
@CarlBrwn
Thank You
& Good Luck
@CarlBrwn

More Related Content

What's hot

Method Swizzling with Objective-C
Method Swizzling with Objective-CMethod Swizzling with Objective-C
Method Swizzling with Objective-C
AdamFallon4
 
Reactive Streams, j.u.concurrent & Beyond!
Reactive Streams, j.u.concurrent & Beyond!Reactive Streams, j.u.concurrent & Beyond!
Reactive Streams, j.u.concurrent & Beyond!
Konrad Malawski
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
Betclic Everest Group Tech Team
 
CQRS Evolved - CQRS + Akka.NET
CQRS Evolved - CQRS + Akka.NETCQRS Evolved - CQRS + Akka.NET
CQRS Evolved - CQRS + Akka.NET
David Hoerster
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
Not Only Streams for Akademia JLabs
Not Only Streams for Akademia JLabsNot Only Streams for Akademia JLabs
Not Only Streams for Akademia JLabs
Konrad Malawski
 
Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)
Betclic Everest Group Tech Team
 
Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16
allingeek
 
How Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM EcosystemHow Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM Ecosystem
Konrad Malawski
 
Scala adoption by enterprises
Scala adoption by enterprisesScala adoption by enterprises
Scala adoption by enterprises
Mike Slinn
 
Erlang as a cloud citizen, a fractal approach to throughput
Erlang as a cloud citizen, a fractal approach to throughputErlang as a cloud citizen, a fractal approach to throughput
Erlang as a cloud citizen, a fractal approach to throughput
Paolo Negri
 
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
Konrad Malawski
 
Training - What is Performance ?
Training  - What is Performance ?Training  - What is Performance ?
Training - What is Performance ?
Betclic Everest Group Tech Team
 
The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...
Holden Karau
 
FunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang EcosystemFunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang Ecosystem
Robert Virding
 
Serverless observability - a hero's perspective
Serverless observability - a hero's perspectiveServerless observability - a hero's perspective
Serverless observability - a hero's perspective
Yan Cui
 
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slapDEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
Felipe Prado
 
Springone2gx 2015 Reactive Options for Groovy
Springone2gx 2015  Reactive Options for GroovySpringone2gx 2015  Reactive Options for Groovy
Springone2gx 2015 Reactive Options for Groovy
Steve Pember
 
Infinum Android Talks #05 - Square tape
Infinum Android Talks #05 - Square tapeInfinum Android Talks #05 - Square tape
Infinum Android Talks #05 - Square tape
Infinum
 

What's hot (20)

Method Swizzling with Objective-C
Method Swizzling with Objective-CMethod Swizzling with Objective-C
Method Swizzling with Objective-C
 
Reactive Streams, j.u.concurrent & Beyond!
Reactive Streams, j.u.concurrent & Beyond!Reactive Streams, j.u.concurrent & Beyond!
Reactive Streams, j.u.concurrent & Beyond!
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
CQRS Evolved - CQRS + Akka.NET
CQRS Evolved - CQRS + Akka.NETCQRS Evolved - CQRS + Akka.NET
CQRS Evolved - CQRS + Akka.NET
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Not Only Streams for Akademia JLabs
Not Only Streams for Akademia JLabsNot Only Streams for Akademia JLabs
Not Only Streams for Akademia JLabs
 
Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)
 
Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16
 
How Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM EcosystemHow Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM Ecosystem
 
Scala adoption by enterprises
Scala adoption by enterprisesScala adoption by enterprises
Scala adoption by enterprises
 
Erlang as a cloud citizen, a fractal approach to throughput
Erlang as a cloud citizen, a fractal approach to throughputErlang as a cloud citizen, a fractal approach to throughput
Erlang as a cloud citizen, a fractal approach to throughput
 
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
 
Training - What is Performance ?
Training  - What is Performance ?Training  - What is Performance ?
Training - What is Performance ?
 
The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...The magic of (data parallel) distributed systems and where it all breaks - Re...
The magic of (data parallel) distributed systems and where it all breaks - Re...
 
FunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang EcosystemFunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang Ecosystem
 
Serverless observability - a hero's perspective
Serverless observability - a hero's perspectiveServerless observability - a hero's perspective
Serverless observability - a hero's perspective
 
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slapDEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
 
Springone2gx 2015 Reactive Options for Groovy
Springone2gx 2015  Reactive Options for GroovySpringone2gx 2015  Reactive Options for Groovy
Springone2gx 2015 Reactive Options for Groovy
 
Migrating big data
Migrating big dataMigrating big data
Migrating big data
 
Infinum Android Talks #05 - Square tape
Infinum Android Talks #05 - Square tapeInfinum Android Talks #05 - Square tape
Infinum Android Talks #05 - Square tape
 

Similar to Managing Memory in Swift (Yes, that's a thing)

Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
Techizzaa
 
Solving k8s persistent workloads using k8s DevOps style
Solving k8s persistent workloads using k8s DevOps styleSolving k8s persistent workloads using k8s DevOps style
Solving k8s persistent workloads using k8s DevOps style
MayaData
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
David Martínez Rego
 
Perl in Teh Cloud
Perl in Teh CloudPerl in Teh Cloud
Perl in Teh Cloud
Pedro Figueiredo
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
Konstantin Gredeskoul
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by Accident
Gleicon Moraes
 
DefCore: The Interoperability Standard for OpenStack
DefCore: The Interoperability Standard for OpenStackDefCore: The Interoperability Standard for OpenStack
DefCore: The Interoperability Standard for OpenStack
Mark Voelker
 
Comment choisir entre Parse, Heroku et AWS ?
Comment choisir entre Parse, Heroku et AWS ?Comment choisir entre Parse, Heroku et AWS ?
Comment choisir entre Parse, Heroku et AWS ?
TheFamily
 
Server’s variations bsw2015
Server’s variations bsw2015Server’s variations bsw2015
Server’s variations bsw2015
Laurent Cerveau
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
Davide Mauri
 
Container Attached Storage with OpenEBS - CNCF Paris Meetup
Container Attached Storage with OpenEBS - CNCF Paris MeetupContainer Attached Storage with OpenEBS - CNCF Paris Meetup
Container Attached Storage with OpenEBS - CNCF Paris Meetup
MayaData Inc
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
Abdelmonaim Remani
 
Agile Data: Building Hadoop Analytics Applications
Agile Data: Building Hadoop Analytics ApplicationsAgile Data: Building Hadoop Analytics Applications
Agile Data: Building Hadoop Analytics Applications
DataWorks Summit
 
The economies of scaling software - Abdel Remani
The economies of scaling software - Abdel RemaniThe economies of scaling software - Abdel Remani
The economies of scaling software - Abdel Remani
jaxconf
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Maarten Balliauw
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
Stanojko Markovik
 
Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...
Nikolay Savvinov
 
Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
Nina Zakharenko
 
A tale of two proxies
A tale of two proxiesA tale of two proxies
A tale of two proxies
SensePost
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 

Similar to Managing Memory in Swift (Yes, that's a thing) (20)

Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
 
Solving k8s persistent workloads using k8s DevOps style
Solving k8s persistent workloads using k8s DevOps styleSolving k8s persistent workloads using k8s DevOps style
Solving k8s persistent workloads using k8s DevOps style
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
 
Perl in Teh Cloud
Perl in Teh CloudPerl in Teh Cloud
Perl in Teh Cloud
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by Accident
 
DefCore: The Interoperability Standard for OpenStack
DefCore: The Interoperability Standard for OpenStackDefCore: The Interoperability Standard for OpenStack
DefCore: The Interoperability Standard for OpenStack
 
Comment choisir entre Parse, Heroku et AWS ?
Comment choisir entre Parse, Heroku et AWS ?Comment choisir entre Parse, Heroku et AWS ?
Comment choisir entre Parse, Heroku et AWS ?
 
Server’s variations bsw2015
Server’s variations bsw2015Server’s variations bsw2015
Server’s variations bsw2015
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
 
Container Attached Storage with OpenEBS - CNCF Paris Meetup
Container Attached Storage with OpenEBS - CNCF Paris MeetupContainer Attached Storage with OpenEBS - CNCF Paris Meetup
Container Attached Storage with OpenEBS - CNCF Paris Meetup
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
 
Agile Data: Building Hadoop Analytics Applications
Agile Data: Building Hadoop Analytics ApplicationsAgile Data: Building Hadoop Analytics Applications
Agile Data: Building Hadoop Analytics Applications
 
The economies of scaling software - Abdel Remani
The economies of scaling software - Abdel RemaniThe economies of scaling software - Abdel Remani
The economies of scaling software - Abdel Remani
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...
 
Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
 
A tale of two proxies
A tale of two proxiesA tale of two proxies
A tale of two proxies
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
 

More from Carl Brown

GDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your AppsGDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your Apps
Carl Brown
 
New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4
Carl Brown
 
Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06
Carl Brown
 
Generics, the Swift ABI and you
Generics, the Swift ABI and youGenerics, the Swift ABI and you
Generics, the Swift ABI and you
Carl Brown
 
Swift GUI Development without Xcode
Swift GUI Development without XcodeSwift GUI Development without Xcode
Swift GUI Development without Xcode
Carl Brown
 
what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23
Carl Brown
 
Open Source Swift: Up and Running
Open Source Swift: Up and RunningOpen Source Swift: Up and Running
Open Source Swift: Up and Running
Carl Brown
 
Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016
Carl Brown
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016
Carl Brown
 
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Carl Brown
 
Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watchCarl Brown
 
iOS8 and the new App Store
iOS8 and the new App Store   iOS8 and the new App Store
iOS8 and the new App Store
Carl Brown
 
Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014
Carl Brown
 
Intro to cloud kit Cocoader.org 24 July 2014
Intro to cloud kit   Cocoader.org 24 July 2014Intro to cloud kit   Cocoader.org 24 July 2014
Intro to cloud kit Cocoader.org 24 July 2014
Carl Brown
 
Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)
Carl Brown
 
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Carl Brown
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
Carl Brown
 
REST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A TourREST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A Tour
Carl Brown
 
360iDev iOS AntiPatterns
360iDev iOS AntiPatterns360iDev iOS AntiPatterns
360iDev iOS AntiPatterns
Carl Brown
 

More from Carl Brown (20)

GDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your AppsGDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your Apps
 
New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4
 
Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06
 
Generics, the Swift ABI and you
Generics, the Swift ABI and youGenerics, the Swift ABI and you
Generics, the Swift ABI and you
 
Swift GUI Development without Xcode
Swift GUI Development without XcodeSwift GUI Development without Xcode
Swift GUI Development without Xcode
 
what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23
 
Open Source Swift: Up and Running
Open Source Swift: Up and RunningOpen Source Swift: Up and Running
Open Source Swift: Up and Running
 
Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016
 
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
 
Gcd cc-150205
Gcd cc-150205Gcd cc-150205
Gcd cc-150205
 
Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watch
 
iOS8 and the new App Store
iOS8 and the new App Store   iOS8 and the new App Store
iOS8 and the new App Store
 
Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014
 
Intro to cloud kit Cocoader.org 24 July 2014
Intro to cloud kit   Cocoader.org 24 July 2014Intro to cloud kit   Cocoader.org 24 July 2014
Intro to cloud kit Cocoader.org 24 July 2014
 
Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)
 
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
 
REST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A TourREST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A Tour
 
360iDev iOS AntiPatterns
360iDev iOS AntiPatterns360iDev iOS AntiPatterns
360iDev iOS AntiPatterns
 

Recently uploaded

Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 

Recently uploaded (20)

Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 

Managing Memory in Swift (Yes, that's a thing)

  • 1. Managing Memory in Swift (Yes, that's a thing) Swift Cloud Workshop 2 Austin, TX 30 September 2017 Illustration Renders by https://pixabay.com/en/users/3dman_eu-1553824/ @CarlBrwn
  • 2. Managing Memory in Swift (Yes, that's a thing) Swift Cloud Workshop 2 Austin, TX 30 September 2017 Illustration Renders by https://pixabay.com/en/users/3dman_eu-1553824/ @CarlBrwn
  • 3. Obligatory Bio • Swift on the Server Developer at IBM • First iOS App in 2008, many projects since • Author, App Accomplished • Meetup Organizer • SwiftAustin & CocoaCoders • Parent @CarlBrwn
  • 4. Is Swift on the Server READY? • In my (personal) Opinion, [NOT speaking on behalf of IBM], this answer depends on two things: 1. Do you need the ecosystem to have more features that you can build yourself? in the Cloud [For your app?] @CarlBrwn
  • 5. Tale of Two Ecosystems • NPM Claims 475,000 available node.js packages. • IBM’s Swift Package Catalog has 4000 entries (last I looked), and that number includes at least some iOS-only packages. • So if you want to do Server-Side Swift, be prepared to roll your own. • Personally, I’m okay with writing my own left-pad, but YMMV. @CarlBrwn
  • 6. Is Swift on the Server READY? • In my (personal) Opinion, [NOT speaking on behalf of IBM], this answer depends on two things: 1. Do you need the ecosystem to have more features that you can build yourself? 2. Can you manage your own memory? in the Cloud [For your app?] @CarlBrwn
  • 7. Previously on “Conferences with Carl”: try!Swift NYC 2017 • This talk expands on that talk • It should be up on video at Realm sometime soon • Not necessary to have seen it @CarlBrwn
  • 8. 0 35 70 105 140 PR Fixes CodeOrdering Counting Encapsulation Logic(App) Memory Naming Optionals Performance Threading Typing Unclear PRs Meeting Criteria (502 total) ? Today’s Talk @CarlBrwn
  • 9. Memory (6.4%) • Most people I talk to don’t think of Swift Memory Management as a problem • Some of the Apple folks I talked to about it at WWDC this year were surprised, too (at first) • It is a real problem, and it’s serious • And it’s worse on Linux… • For 3 primary reasons: @CarlBrwn
  • 10. Reason 1: Duration • Cloud apps run longer (in general) • Cloud apps can’t restart themselves in the background while you’re checking Facebook • Memory leaks are cumulative • Cloud costs scale with dedicated RAM @CarlBrwn
  • 11. Reason 2: Tools • We don’t have Xcode • We don’t have Instruments • We don’t have cycle detectors • In fact, we don’t have any Swift-aware memory diagnostics on Linux at all @CarlBrwn
  • 12. Reason 3: Structure • iOS (and Mac) Apps have a Structure: • Application Delegate • View Controllers • Views • Well-tested clean-up code • (e.g. Views reclaimed when removed from the screen) • By contrast, Linux has: main • Biggest problem w/Swift on Linux (IMNSHO) @CarlBrwn
  • 13. BRIEF Intro to ARC • Automatic Reference Counting • In the Old Days (2009), we would call retain, release or autorelease on each object by hand • Apple published rules about when you were supposed to use each • Apple wrote an Analyzer tool that would tell you when you broke the rules • ARC does for you what those rules said to do 1/6 @CarlBrwn
  • 14. BRIEF Intro to ARC • Each object has a counter • (Originally this was part of the NSObject implementation, but now it applies to more things, but I’m probably still going to say “object” today) • When you take a reference to something (like putting it in an ivar), you increment its counter • When you’re done with it (or the system is done with you), its counter is decremented • The object only knows its count. It has no idea who is pointing at it. 2/6 @CarlBrwn
  • 15. BRIEF Intro to ARC • ARC keeps things around as long as their reference counts are greater than zero • When a counter drops to zero, the object is available to be reclaimed • Reclamation happens periodically and deterministically, but not necessarily instantly 3/6 @CarlBrwn
  • 16. BRIEF Intro to ARC • When an object is reclaimed, all the things it’s referencing have their counts reduced by one • That often causes those things to get reclaimed in turn, and so on • Unlike GC, no marking or sweeping is needed during reclamation • The system doesn’t need to pause the whole system to free up memory RAM ARC Object 4/6 @CarlBrwn
  • 17. ARC-Optimized Architecture This is the world ARC wants (and expects) 5/6 BRIEF Intro to ARC @CarlBrwn
  • 18. BRIEF Intro to ARC • When two things refer to each other, ARC is powerless • Neither count will ever be set to zero • The objects can never be reclaimed • Some Garbage Collectors in Other Languages can find and reclaim these kinds of cycles at runtime (with a performance penalty), but in Swift it’s not happening • If no other objects can reach either of these, you get a classic leak 6/6 @CarlBrwn
  • 19. Good Structures • Clear lines of ownership • Clear relationships & hierarchies • Tend to use ivars/properties • Tend not to remember things passed in from outside (especially from caller) • Tend to reinforce existing relationships & events • e.g. Memory gets naturally reclaimed when views leave the screen or network connections drop @CarlBrwn
  • 20. Bad Structures • Tangled or unclear relationships • Strong references to things passed in from outside • Things captured in closures • Hierarchies not rooted in natural events • e.g. Needs clean up functions instead of it “just happening” • “Missed Dominos” @CarlBrwn
  • 21. You have to Measure • This is a necessary step - you can’t reliably quantify memory issues from reading code (if you can, you should be mining bitcoin in your head) • This has to happen at runtime, static analysis won’t help • You need either a production-like synthetic load, or to measure in actual production (or both) • Honestly, I don’t think many people do this (even for iOS) @CarlBrwn
  • 22. My Measuring Scripts are Available https://github.com/carlbrown/SwiftServerComparison May or may not work for your use-case, but you’re welcome to them @CarlBrwn
  • 23. Wish there was a better way Run long test, grab `ps` logs, graph Rollback, Next or Fixed? Test with `heaptrack` to find next Area of interest Change code @CarlBrwn
  • 24. Graph of RSS from `ps aux` Moving the slope, One little fix at a time @CarlBrwn
  • 25. Variability can be a Problem Hard to see the leak (signal) when leak is small compared to the spread (noise) This was happening because of clean-up functions As you run the test longer, it becomes less of an issue@CarlBrwn
  • 26. Duration This is a half hour run @CarlBrwn
  • 27. Duration (cont) Same run, full duration (~5 hours) @CarlBrwn
  • 28. Wish there was a better way Run long test, grab `ps` logs, graph Rollback, Next or Fixed? Test with `heaptrack` to find next Area of interest @CarlBrwn
  • 29. Darwin’s cool tools aren’t for you… Nothing remotely like it on Linux Even if you test on Darwin, there may still be Linux leaks @CarlBrwn
  • 30. …And that’s a real shame Because the leaks that happen when you don’t have an App Delegate structure can get REALLY convoluted @CarlBrwn
  • 31. Use heaptrack & heaptrack_gui Not constrained to single thread like valgrind But no awareness of Swift reference counts or structure @CarlBrwn
  • 32. Wish there was a better way Run long test, grab `ps` logs, graph Rollback, Next or Fixed? Test with `heaptrack` to find next Area of interest Change code @CarlBrwn
  • 33. So what code do you change? • heaptrack only gives us the line of code that allocated the object that is “stuck" in memory (& you may want to run swift-demangle) • Find all the places that object is used and especially all the times it’s passed to a closure or as an argument • See if you can make them weak without crashing (or unowned, but only if you have to - it’s not safe) • See if making it weak at that point changes the slope of the graph • Make only one change at a time, and measure • Continue until you’ve found the right place(s) to make weak • NOTE: This will only work if ownership is clear. If not, refactor Good Question @CarlBrwn
  • 34. Most of my changes turn out to be dead-ends The ones that make the slope better get merged to `master` @CarlBrwn
  • 35. Thank You & Good Luck @CarlBrwn
  • 36. Thank You & Good Luck @CarlBrwn