Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Memory Use in Extensions

Conrad Kramer
September 10, 2015

Memory Use in Extensions

It's been a year since Apple introduced extensions in iOS 8, and developing with them still can be a black box sometimes. I will be talking about the little-known memory limits in extensions, and some common techniques you can use to decrease dirty memory usage. I'll be drawing from the experience we used to fit the entire Workflow engine into the 16 MB memory limit for notification center widgets.

This talk was given on September 10, 2015 at the Philly CocoaHeads monthly meeting.

Conrad Kramer

September 10, 2015
Tweet

More Decks by Conrad Kramer

Other Decks in Programming

Transcript

  1. "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
  2. 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.
  3. 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
  4. 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...
  5. 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
  6. 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)
  7. Maximize Purgeable Memory • Use NSPurgeableData instead of NSData •

    Use NSCache The system will automa/cally clear these for you
  8. 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