© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
#WWDC15
Optimizing Your App for Multitasking on iPad in iOS 9
Brittany Paine iOS SpringBoard EngineerJon Drummond iOS SpringBoard Engineer
App Frameworks
Session 212
Multitasking Sessions
Getting Started with Multitasking on iPad in iOS 9 Presidio Tuesday 4:30PM
Multitasking Essentials for Media-Based Apps on iPad in iOS 9 Pacific Heights Wednesday 2:30PM
Optimizing Your App for Multitasking in iOS Presidio Wednesday 3:30PM
Multitasking Sessions
Getting Started with Multitasking on iPad in iOS 9 Presidio Tuesday 4:30PM
Multitasking Essentials for Media-Based Apps on iPad in iOS 9 Pacific Heights Wednesday 2:30PM
Optimizing Your App for Multitasking in iOS Presidio Wednesday 3:30PMOptimizing Your App for Multitasking in iOS
CPU
CPU
1 frame
CPU
1 frame
CPU
1 frame
FreeSystem Primary App
Memory
FreeSystem Primary App Secondary App
Memory
System Secondary App PiP
Memory
Primary App
System Secondary App PiP
Memory
!!
System Secondary App PiP
Memory
What Is SpringBoard?
idle timer
app launching
event routing
application lifecycleproximity detection
orientation lock
external display management home screen
lock screen
control center
app switcher
voice control
phone calls
wallpaper
notification center
icons
folders
notificationsremote notifications
alarms
do not disturbsystem gestures
alerts
What Is SpringBoard?
UIApplication
What Is SpringBoard?
SpringBoard
SpringBoard
The original multitasking app
SpringBoard
The original multitasking appFaced the same multitasking challenges as you do now
SpringBoard
The original multitasking appFaced the same multitasking challenges as you do nowLearned a lot of lessons along the way
Optimizing Your AppThe easy stuff
Optimizing Your App
Fix memory leaks
The easy stuff
Optimizing Your App
Fix memory leaksFix retain cycles and unbounded memory growth
The easy stuff
Optimizing Your App
Fix memory leaksFix retain cycles and unbounded memory growthFix inefficient algorithms
The easy stuff
Great Performance Involves Tradeoffs
a
Memory
b
CPU
c
Disk Space I/O
e
GPU
Great Performance Involves Tradeoffs
a
Memory CPU
c
Disk Space I/O
e
GPU
b
Great Performance Involves Tradeoffs
a
Memory CPU
c
Disk Space I/O
e
GPU
b
Great Performance Involves Tradeoffs
a
Memory
b
CPU
c
Disk Space I/O
e
GPU
a
Memory
b
CPU I/O
Digging In
a
Memory
b
CPU I/O
Digging In
CPU and disk I/O are minimalwhile scrolling
Digging In
a
Memory
b
CPU I/O
CPU and disk I/O are minimalwhile scrolling
Digging In
a
Memory
b
CPU I/O
All icons kept in memory
CPU and disk I/O are minimalwhile scrolling
Digging In
a
Memory
b
CPU I/O
All icons kept in memory
CPU and disk I/O are minimalwhile scrolling
Digging In
a
Memory
b
CPU I/O
System reclaims memory by terminating processes
All icons kept in memory
CPU and disk I/O are minimalwhile scrolling
Memory ManagementWorking set
Memory ManagementWorking set
The critical objects and resources you need right now
Memory ManagementWorking set
The critical objects and resources you need right now• Keep it small
Memory ManagementWorking set
The critical objects and resources you need right now• Keep it small• It might change based on context
Memory ManagementWorking set
The critical objects and resources you need right now• Keep it small• It might change based on context• Don’t let it grow unbounded
Working Set = Every Page
Working Set = Every Page
Working Set = Every Page
Working Set = 1 Page
Working Set = 1 Page
Working Set = 1 Page
a
Memory
b
CPU
Digging In
I/O
a
Memory
b
CPU
Digging In
I/O
CPU and disk I/O are highwhile scrolling
Digging In
a
Memory CPU I/O
b
CPU and disk I/O are highwhile scrolling
Digging In
Memory CPU I/O
a b
CPU and disk I/O are highwhile scrolling
Very few icons in memory
Digging In
Memory CPU I/O
a b
CPU and disk I/O are highwhile scrolling
Very few icons in memory
CPU ManagementMain thread
Main thread’s top priority is responding to user eventsDon’t do unnecessary work
CPU ManagementQuality of service
User Interactive* Is this work actively involved in updating the UI?
User Initiated Is this work required to continue user interaction?
Utility Is the user aware of the progress of this work?
Background Can this work be deferred to start at a better time?
UT
IN
UI
BG
* Main thread
Building Responsive and Efficient Apps with GCD Nob Hill Friday 10:00AM
Power, Performance, and Diagnostics: What’s New in GCD and XPC WWDC14
dispatch_queue_t iconQueue = dispatch_queue_create("IconGenerationQueue", DISPATCH_QUEUE_SERIAL);
for (IRIcon *icon in [iconPage icons]) { dispatch_async(iconQueue, ^{ [self _iconQueue_generateImageForIcon:icon]; }); }];
dispatch_queue_t iconQueue = dispatch_queue_create("IconGenerationQueue", DISPATCH_QUEUE_SERIAL);
for (IRIcon *icon in [iconPage icons]) { dispatch_async(iconQueue, ^{ [self _iconQueue_generateImageForIcon:icon]; }); }];
dispatch_queue_t iconQueue = dispatch_queue_create("IconGenerationQueue", DISPATCH_QUEUE_SERIAL);
for (IRIcon *icon in [iconPage icons]) { dispatch_async(iconQueue, ^{ [self _iconQueue_generateImageForIcon:icon]; }); }];
CPU ManagementQoS overrides
Lower priority queues can be temporarily boosted when a higher priority queue is waiting for itThe system needs a hint that you want a QoS override to occur
Strategy Overrides?
dispatch_sync()
dispatch_block_wait()
dispatch_group_wait()
dispatch_semaphore_wait()
QoS OverridesDispatch
Building Responsive and Efficient Apps with GCD Nob Hill Friday 10:00AM
Power, Performance, and Diagnostics: What’s New in GCD and XPC WWDC14
dispatch_sync(iconQueue, ^{ // ensure icon generation completes at caller QoS });
UIImage *image = [icon image];
Working Set = 1 Page
2 3 4
e fa c - overlay bd - overlay
2 3 4
e f
Working Set = 3 Pages
c - overlayd - overlay
43 5
Working Set = 3 Pages
gc - overlayd - overlay
fe
43 5
Working Set = 3 Pages
gc - overlayd - overlay
e
h
4
e
2 3
Working Set = 3 Pages
c - overlay gd - overlay
h
42 3
Working Set = 3 Pages
c - overlay gd - overlay
s
FreeSystem IconReel
Working Set = 1 Page
FreeSystem IconReel
Working Set = 3 Pages
FreeSystem IconReel
Working Set = 3 Pages
Secondary App
FreeSystem IconReel
Working Set = 3 Pages
Secondary App
Managing Memory GrowthMemory warnings
Managing Memory GrowthMemory warnings
Happens when• The system is under memory pressure• Your process is approaching its memory limit
Managing Memory GrowthMemory warnings
Happens when• The system is under memory pressure• Your process is approaching its memory limit
What to do• Remove anything not in the working set
- Clear cached data- Release images- Release view controllers
Memory WarningsAPIs
In your UIApplicationDelegate and UIViewControllers-[UIApplicationDelegate applicationDidReceiveMemoryWarning:] -[UIViewController didReceiveMemoryWarning]
Anywhere in an application processUIApplicationDidReceiveMemoryWarningNotification
In libraries and extensionsDISPATCH_SOURCE_TYPE_MEMORYPRESSURE
Managing Memory GrowthNSCache
NSDictionary-likeIdeal for objects that can be regenerated on demand Trims under memory pressureTrims for application lifecycle changes
FreeSystem
Working Set = 3 Pages
Secondary AppIconReel
FreeSystem
Working Set = 3 Pages
Secondary AppIconReel
System
Working Set = 3 Pages
FreeSecondary AppIconReel
System
Working Set = 3 Pages
FreeSecondary AppIconReel
System
Working Set = 3 Pages
FreeSecondary AppIconReel
a
Recap
Memory CPU I/O
a
Recap
Memory CPU I/O
a
Recap
Memory CPU I/O
Working Set
a
Recap
NSCache{Adaptive
Memory CPU I/O
Working Set
Advanced Adaptive MemoryLeveraging the Virtual Memory System
Jon Drummond iOS SpringBoard Engineer
Primary AppSystem Secondary App
Primary AppSystem
Memory Warning
Secondary App
!!
Primary AppSystem
Memory Warning
Secondary App
!!
System Primary App Secondary App
System Primary App PiPSecondary App
!
System Primary App Secondary App PiP
Secondary AppSystem Primary App PiP
Secondary AppSystem PiP
!!
System PiPSecondary App
“The world outside your process should be regarded as hostile and bent upon your destruction.”
Me, almost everyday
Advanced Adaptive Memory
Advanced Adaptive Memory
Memory is the most constrained resource on iOS• No swap
Advanced Adaptive Memory
Memory is the most constrained resource on iOS• No swap
System can require memory faster than it can be released• CPU contention during memory
warnings can slow or prevent apps from releasing it
Classification Description Reclaimable?
Dirty Memory in active use
Purgeable Otherwise-dirty memory designated as not in-use
Clean Read-only memorybacked by files on disk
Memory Classification
Classification Description Reclaimable?
Dirty Memory in active use
Purgeable Otherwise-dirty memory designated as not in-use
Clean Read-only memorybacked by files on disk
Memory Classification
Classification Description Reclaimable?
Dirty Memory in active use
Purgeable Otherwise-dirty memory designated as not in-use
Clean Read-only memorybacked by files on disk
Memory Classification
Classification Description Reclaimable?
Dirty Memory in active use
Purgeable Otherwise-dirty memory designated as not in-use
Clean Read-only memorybacked by files on disk
Memory Classification
Memory Classification
Clean or purgeable memory is reclaimable
Memory Classification
Leeway
Leveraging the Virtual Memory SystemGoals
Minimize dirty memory usageMaximize purgeable and clean memory usage
iOS App Performance: Memory WWDC12
Minimizing Dirty Memory
Minimizing Dirty Memory
Use less of it!
Minimizing Dirty Memory
Use less of it!Classify otherwise dirty memory as purgeable• Can be automatically reclaimed when not in use• Best for “nice to have” data that can be recomputed
IconReelAdopting purgeable data
Working SetNSCache NSCache
IconReelAdopting purgeable data
Working SetNSCache NSCache
IconReelAdopting purgeable data
Working SetNSCache NSCache
IconReelAdopting purgeable data
Working SetNSCache NSCache
IconReelAdopting purgeable data
Working SetNSCache NSCache
IconReelAdopting purgeable data
Working SetNSCache NSCache
In Use/Dirty
IconReelAdopting purgeable data
Working SetNSCache NSCache
In Use/Dirty PurgeablePurgeable
IconReelAdopting purgeable data
Working SetNSCache NSCache
In Use/Dirty PurgeablePurgeable
IconReelAdopting purgeable data
Working SetNSCache NSCache
In Use/Dirty PurgeablePurgeable
IconReelAdopting purgeable data
Working SetNSCache NSCache
In Use/Dirty PurgeablePurgeable
IconReelAdopting purgeable data
Working SetNSCache NSCache
In Use/Dirty PurgeablePurgeable
IconReelAdopting purgeable data
Working SetNSCache NSCache
In Use/Dirty PurgeablePurgeable
System Primary App Secondary App PiP
!!
System
Reclaimable
Primary App Secondary App PiP
!!
System
Reclaimable
Primary App Secondary App PiP
!!
System Primary App Secondary App PiP
System Primary App Secondary App PiP
System Primary App Secondary App PiP
System Primary App PiPSecondary App
System Primary App PiPSecondary App
Memory Warning!!
System Primary App PiPSecondary App
Memory Warning!!
System Primary App PiPSecondary App
System Primary App PiPSecondary App
NSPurgeableData• beginContentAccess • endContentAccess • isContentDiscarded
APIPurgeable Data
- memory now considered dirty- memory now considered purgeable- has the memory been reclaimed?
NSCache
CPU I/O
Working Set
Memory
{Adaptive
IconReelSystem resources
NSCache
Purgeable
CPU I/O
Working Set
Memory
{Adaptive
IconReelSystem resources
IconReelData characteristics
IconReelData characteristics
Absolutely essential
IconReelData characteristics
Absolutely essentialExpensive to generate
IconReelData characteristics
Absolutely essentialExpensive to generateCan be pre-computed
IconReelData characteristics
Absolutely essentialExpensive to generateCan be pre-computedStatic once generated
IconReelData characteristics
Absolutely essentialExpensive to generateCan be pre-computedStatic once generatedPerfect candidate for caching to a file!
Working Set
IconReelSystem resources
NSCache
CPU I/OMemory
Purgeable
Working Set
IconReelSystem resources
NSCache
CPU I/OMemory Disk Space
Purgeable
Working Set
CPU I/OMemory Disk Space
IconReelSystem resources
NSCache
Purgeable
CPU I/OMemory Disk Space
Working Set
IconReelSystem resources
NSCache
Purgeable
Maximizing Clean Memory
Maximizing Clean Memory
Memory backed by a file on disk is considered clean• Data in such a file can be “memory mapped”
Maximizing Clean Memory
Memory backed by a file on disk is considered clean• Data in such a file can be “memory mapped”
Memory and file contents must match exactly• Ideal for read-only data
Maximizing Clean Memory
Memory backed by a file on disk is considered clean• Data in such a file can be “memory mapped”
Memory and file contents must match exactly• Ideal for read-only data
Data in memory is evicted and reloaded on your behalf• Allows for random access
Memory Mapped Data
Data in Memory
Memory Mapped Data
Data in Memory
File on Disk
Memory Mapped Data
Memory Mapped Data
File on Disk
Memory Mapped Data
Memory Mapped Data
Memory Mapped Data
IconReel
IconReel
IconReel
IconReel
Memory Mapped Image Data
IconReel
IconReel
IconReel
IconReel
IconReel
IconReel
IconReelSystem resources
CPU I/OMemory Disk Space
NSCache/Purgeable
Working Set
IconReelSystem resources
CPU I/OMemory Disk Space
Working Set
APIMemory Mapped Data
typedef NS_OPTIONS(NSUInteger, NSDataReadingOptions) { NSDataReadingMappedIfSafe, NSDataReadingMappedAlways, … };
@interface NSData (NSDataCreation)
- (nullable instancetype)initWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;
@end
APIMemory Mapped Data
typedef NS_OPTIONS(NSUInteger, NSDataReadingOptions) { NSDataReadingMappedIfSafe, NSDataReadingMappedAlways, … };
@interface NSData (NSDataCreation)
- (nullable instancetype)initWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;
@end
APIMemory Mapped Data
typedef NS_OPTIONS(NSUInteger, NSDataReadingOptions) { NSDataReadingMappedIfSafe, NSDataReadingMappedAlways, … };
@interface NSData (NSDataCreation)
- (nullable instancetype)initWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;
@end
CaveatsLeveraging the Virtual Memory System
VM Addressable Space0x00000000 0xFFE00000
CaveatsLeveraging the Virtual Memory System
Not appropriate for small chunks of data
VM Addressable Space0x00000000 0xFFE00000
CaveatsLeveraging the Virtual Memory System
Not appropriate for small chunks of dataVirtual Memory misuse• Fragmentation• Exhaustion
VM Addressable Space0x00000000 0xFFE00000
CaveatsLeveraging the Virtual Memory System
Not appropriate for small chunks of dataVirtual Memory misuse• Fragmentation• Exhaustion
Abusing the VM system can result in process termination
VM Addressable Space0x00000000 0xFFE00000
Summary
Summary
Summary
Use Instruments to identify and fix bugs
Summary
Use Instruments to identify and fix bugsPrioritize your work appropriately and don’t block the main thread
Summary
Use Instruments to identify and fix bugsPrioritize your work appropriately and don’t block the main threadIdentify and manage your working set
Summary
Use Instruments to identify and fix bugsPrioritize your work appropriately and don’t block the main threadIdentify and manage your working setUse caches and respond tomemory warnings
Summary
Use Instruments to identify and fix bugsPrioritize your work appropriately and don’t block the main threadIdentify and manage your working setUse caches and respond tomemory warningsLeverage the virtual memory system to increase reclaimable memory
Summary
Use Instruments to identify and fix bugsPrioritize your work appropriately and don’t block the main threadIdentify and manage your working setUse caches and respond tomemory warningsLeverage the virtual memory system to increase reclaimable memoryGreat performance requires tradeoffs
“It is not the strongest or the most intelligent who survive, but those who can best manage change.”
Charles Darwin (apocryphal)
More Information
DocumentationAdopting MultitaskingEnhancements on iPadPerformance OverviewInstruments User Guidehttp://developer.apple.com/iOS
Technical SupportApple Developer Forumshttp://developer.apple.com/forums
Developer Technical Supporthttp://developer.apple.com/support/technical
General InquiriesCurt Rothert, App Frameworks [email protected]
Related Sessions
Getting Started with Multitasking on iPad in iOS 9 Presidio Tuesday 4:30PM
Multitasking Essentials for Media-Based Apps oniPad in iOS 9 Pacific Heights Wednesday 2:30PM
Performance on iOS and watchOS Presidio Friday 11:00AM
Building Responsive and Efficient Apps with GCD Nob Hill Friday 10:00AM
Labs
Cocoa Touch and Multitasking Lab Frameworks Lab B Wednesday 9:00AM
Power and Performance Lab Frameworks Lab C Friday 12:00PM