Slide 1

Slide 1 text

Memory Use in Extensions Conrad Kramer @conradev

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

"Memory limits for running app extensions are significantly lower than the memory limits imposed on a foreground app ... Some extensions may have lower memory limits than others: For example, widgets must be especially efficient because users are likely to have several widgets open at the same Ame." apple.co/1Qr4c4L

Slide 4

Slide 4 text

Jetsam Jetsam (noun) - unwanted material or goods that have been thrown overboard from a ship and washed ashore, especially material that has been discarded to lighten the vessel. – or – the kernel subsystem that is responsible for je#soning processes to maintain free memory. There is zero swap space on iOS. If the system runs out of memory it will crash.

Slide 5

Slide 5 text

A system of priori%es and limits

Slide 6

Slide 6 text

When Jetsam needs to reclaim memory, it kills lower priority processes first It also kills any process that goes over its limit, or high water mark

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Low Memory Warning • -[UIApplicationDelegate applicationDidReceiveMemoryWarning:] is called • Purgeable memory is freed • Clean memory is flushed to disk • If the process is s;ll using too much memory...

Slide 10

Slide 10 text

Jetsam Event (also known as an out of memory event)

Slide 11

Slide 11 text

Dirty, purgeable and clean memory Minimize dirty memory, and maximize your purgeable and clean memory WWDC 2015 Session 212 has a fantas3c discussion of this

Slide 12

Slide 12 text

Minimize Dirty Memory • Use NSURLSessionUploadTask instead of NSURLSessionDataTask • Use HTTPBodyStream instead of HTTPBody on NSURLRequest • Use CGImageDestinationCreateWithURL instead of CGImageDestinationCreateWithData for ImageIO • Manage object life4mes • Lazily ini4alize everything (and avoid objc_copyClassList)

Slide 13

Slide 13 text

Maximize Purgeable Memory • Use NSPurgeableData instead of NSData • Use NSCache The system will automa/cally clear these for you

Slide 14

Slide 14 text

Maximize Clean Memory • Only read large files into memory if you use NSDataReadingMappedAlways • Create memory-mappable cache files, and store data in them (for example, FastImageCache). The system will automa/cally evict the file from memory when it's not needed

Slide 15

Slide 15 text

Thanks!