WWDC 2015

Optimizing Your App for Multitasking on iPad in iOS 9

In Multitasking and PiP for iOS 9, the apps are suggested to be good multitasking citizen in order to keep the best user experience even in a resource constrainted environment. This session aims to provide useful information for adopting new multitasking features.

Sample Code : iconReel

The Easy stuff

  • use instructment to find and fix memory leaks
  • fix retain cycles and unbound memory growth
  • fix inefficient algorithms like high space and time complexities

Great Performance Involves Tradeoffs

Great Performance is a trade-off between memory, CPU, Disk space, I/O and GPU.

Working Set is a set of objects and resources is required right now

  • Keep it small
  • may change based on context
  • Keep it bounded in terms of memory

CPU Management

  • Main thread's top priority is responding to user events
  • Don't do unnecessary work
  • use QoS for setting higher priority of task
  • use Qos overriding to boost when the lower priority task is being waited by a higher priority
  • providing a hint for QoS overriding
  • dispatch_sync() and dispatch_block_wait()

Memory Warnings

Occured when

  • system is under memory pressure
  • The app process is approaching its memory limit
  • Clear cache data
  • release images
  • release view controllers

API for indicating memory warning

-[UIApplicationDelegate applicationDidReceiveMemoryWarning:]

-[UiViewController didReceiveMemoryWarning]

UIApplicationDidReceiveMemoryWarningNotification
DISPATCH_SOURCE_TYPE_MEMORYPRESSURE

NSCache

  • NSDictionary-like
  • Used for objects that can be regenerated on demand
  • Trims under memory pressure
  • Trims for application lifecycle change
  • NOT ON DISK

Advanced Adaptive Memory

  • Minimize dirty memory usage
    • use less of it
    • convert to purgeable as if:
      • automatically reclaimed when not in use
      • "nice to have" data can be recomputed
  • Maximize purgeable memory usage
  • NSPurgeableData
    • beginContentAccess considered as dirty
    • endContentAccess considered as purgeable
    • isContentDiscarded has memory been reclaimed

File data characteristics

  • Absolutely essential
  • Expensive to generate
  • Can be pre-computed
  • Static once generated

Maximize clean memory

* Data in a file can be "memory mapped" (not caching the whole file in the memory but a pointer to a location of the file location)
* memory and file content must be match exactly so it is useful for read-only data
* Data can be removed and reload when needed

API for memory mapped

typedef NS_OPTIONS(NSUInteger, NSDataReadingOptions) {
    NSDataReadingMappedIfSafe,
    NSDataReadingMappedAlways,
};
@interface NSData (NSDataCreation)
- (nullable instancetype)initWithContentsOfFile:(NSString *)path
                                         options:(NSDataReadingOptions)readOptionsMask
                                         error:(NSError **)errorPtr;
@end
  • not useful for small chunks of data cause
    • fragmentation
    • Exhaustion