ios Memory Deep Dive #WWDC18 Kyle Howarth, Software Engineer James Snee, Software Engineer Kris Markel, Software Engineer

Similar documents
Cocoa Touch Best Practices

Integrating Apps and Content with AR Quick Look

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Announcements. Today s Topics

Building Visually Rich User Experiences

View Controller Lifecycle

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

View Controllers CPRE 388

Mastering Drag and Drop

Introducing PDFKit on ios

Lecture 8 Demo Code: Cassini Multithreading

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Advanced Notifications

What s New in imessage Apps

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Getting the Most out of Playgrounds in Xcode

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

ios Mobile Development

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Advanced Memory Analysis with Instruments. Daniel Delwood Performance Tools Engineer

Intro to Development for ios. Dave Koziol Arbormoon Software, Inc.

Designing iphone Applications

What s New in Xcode App Signing

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

What s New in MapKit. App Frameworks #WWDC17. Fredrik Olsson

Mastering UIKit on tvos

Working with Metal Overview

High Performance Auto Layout

iphone App Basics iphone and ipod touch Development Fall 2009 Lecture 5

Intro to Native ios Development. Dave Koziol Arbormoon Software, Inc.

IOS PERFORMANCE. Getting the most out of your Games and Apps

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Using and Extending the Xcode Source Editor

Introducing Metal 2. Graphics and Games #WWDC17. Michal Valient, GPU Software Engineer Richard Schreyer, GPU Software Engineer

Implementing UI Designs in Interface Builder

imessage Apps and Stickers, Part 2

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Data Delivery with Drag and Drop

CSE 438: Mobile Application Development Lab 2: Virtual Pet App

Assignment III: Graphing Calculator

Leveraging Touch Input on ios

lecture 10 UI/UX and Programmatic Design cs : spring 2018

Finding Bugs Using Xcode Runtime Tools

Stanford CS193p. Developing Applications for ios. Spring Stanford CS193p. Spring 2012

CS 333 Introduction to Operating Systems. Class 11 Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

Media Playback and Recording. CS193W - Spring Lecture 3

Building Watch Apps #WWDC15. Featured. Session 108. Neil Desai watchos Engineer

Profiling in Depth. Do you know where your code is? Session 412. Kris Markel Performance Tools Engineer Chad Woolf Performance Tools Engineer

Custom Drawing & Animation. CS 442: Mobile App Development Michael Saelee

App. Chapter 19 App. App (ViewController) App. Single View Application Single View Application View. (View Controller)

ios SDK Release Notes for ios 10.1 Beta 1

Building the App - Part 5 - Adding a Link

Creating Photo and Video Effects Using Depth

Memory Management Topics. CS 537 Lecture 11 Memory. Virtualizing Resources

Optimizing Swift Performance Session 409

Announcement. Final Project Proposal Presentations and Updates

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

} override func didreceivememorywarning() { 26 super.didreceivememorywarning() 27 } 28 } Pause Stop

Another View of the Memory Hierarchy. Lecture #25 Virtual Memory I Memory Hierarchy Requirements. Memory Hierarchy Requirements

Vision Framework. Building on Core ML. Media #WWDC17. Brett Keating, Apple Manager Frank Doepke, He who wires things together

What s New in tvos #WWDC16. App Frameworks. Session 206. Hans Kim tvos Engineer

Enhancing your apps for the next dimension of touch

Building Mapping Apps for ios With Swift

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

Metal for Ray Tracing Acceleration

What s New in tvos 12

Adopting Advanced Features of the New UI

Operating Systems. Overview Virtual memory part 2. Page replacement algorithms. Lecture 7 Memory management 3: Virtual memory

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

What's New in Core Spotlight

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Mysteries of Auto Layout, Part 1

Advanced Scrollviews and Touch Handling Techniques

Advanced Debugging and the Address Sanitizer

Announcements. Lab 2 is due next Monday (Sept 25 th ) by 11:59 PM. Late policy is 10% of lab total per day late. So -7.5 points per day late for lab 2

1 Build Your First App. The way to get started is to quit talking and begin doing. Walt Disney

Views. A view (i.e. UIView subclass) represents a rectangular area Defines a coordinate space

CS 550 Operating Systems Spring Memory Management: Paging

Introducing the Photos Frameworks

What s New in Energy Debugging

Event Delivery: The Responder Chain

What s New in watchos

What s New in SpriteKit

Developing Applications for ios

Thread Sanitizer and Static Analysis

Creating Audio Apps for watchos

ITP 342 Mobile App Dev. Animation

Objective-C Primer. iphone Programmer s Association. Lorenzo Swank September 10, 2008

UC Berkeley CS61C : Machine Structures

A Mad Libs app that you will navigate through 3 UIViewControllers to add text that will be shown in a story on the fourth UIViewController.

Memory Management: High-Level Overview

Managing Documents In Your ios Apps

SceneKit: What s New Session 604

ITP 342 Advanced Mobile App Dev. Memory

What s New in Metal, Part 2

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CS193P - Lecture 10. iphone Application Development. Performance

Using Accelerate and simd

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

Profiling & Optimization

Transcription:

Session #WWDC18 ios Memory Deep Dive 416 Kyle Howarth, Software Engineer James Snee, Software Engineer Kris Markel, Software Engineer 2018 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Why reduce memory Memory footprint Tools for profiling footprint Images Optimizing when in background Demo

Why Reduce Memory

Users have a better experience.

Memory Footprint Not all memory is created equal

Memory Footprint Pages

Memory Footprint Pages

Memory Footprint Pages UIView String UILabel

Memory Footprint Pages UIView Data String UILabel

Memory Footprint Pages Typically 16KB Page types Clean Dirty

Memory Footprint Pages Typically 16KB Page types Clean Dirty Number of pages x Page size = Memory in use

Memory Footprint Clean and dirty pages int *array = malloc(20000 * sizeof(int)); array[0] = 32 array[19999] = 64

Memory Footprint Clean and dirty pages int *array = malloc(20000 * sizeof(int)); array[0] = 32 array[19999] = 64

Memory Footprint Clean and dirty pages int *array = malloc(20000 * sizeof(int)); array[0] = 32 array[19999] = 64

Memory Footprint Clean and dirty pages int *array = malloc(20000 * sizeof(int)); array[0] = 32 array[19999] = 64

Memory Footprint Clean and dirty pages int *array = malloc(20000 * sizeof(int)); array[0] = 32 array[19999] = 64 32

Memory Footprint Clean and dirty pages int *array = malloc(20000 * sizeof(int)); array[0] = 32 array[19999] = 64 32 64

Memory Footprint Memory mapped files File-backed memory Read-only files are clean Kernel manages when file is in RAM

Memory Footprint Memory mapped files 50KB

Memory Footprint Memory mapped files 50KB

Memory Footprint Typical app memory profile Dirty Compressed Clean

Memory Footprint Clean memory Data that can be paged out of memory

Memory Footprint Clean memory Data that can be paged out of memory Clean Memory

Memory Footprint Clean memory Data that can be paged out of memory Memory mapped files Clean Memory

Memory Footprint Clean memory Data that can be paged out of memory Memory mapped files Image.jpg Blob.data Clean Memory Training.model

Memory Footprint Clean memory Data that can be paged out of memory Memory mapped files UIKit Frameworks* DATA_CONST Clean Memory CoreGraphics DATA_CONST

Memory Footprint Dirty memory

Memory Footprint Dirty memory Dirty Memory

Memory Footprint Dirty memory Memory written by an app Dirty Memory

Memory Footprint Dirty memory Memory written by an app All heap allocations malloc Array NSCache Dirty Memory UIViews String

Memory Footprint Dirty memory Memory written by an app All heap allocations CGRasterData Decoded image buffers Dirty Memory ImageIO

Memory Footprint Dirty memory Memory written by an app All heap allocations UIKit DATA Decoded image buffers DATA_DIRTY Frameworks Dirty CoreGraphics Memory DATA DATA_DIRTY

Memory Footprint Frameworks Frameworks contribute to dirty memory Singletons Global initializers +(void)load; +(void)initialize; // Objective-C // Objective-C attribute ((constructor)) // Objective-C initialize(); // Swift

Memory Footprint Compressed memory No traditional disk swap Memory compressor Compresses unaccessed pages Decompresses pages upon access

Memory Footprint Compressed memory No traditional disk swap Memory compressor Compresses unaccessed pages Decompresses pages upon access Dictionary NSData NSData

Memory Footprint Compressed memory No traditional disk swap Memory compressor Compresses unaccessed stale pages pages Decompresses pages upon access Dictionary NSData NSData

Memory Footprint Memory warnings App is not always the cause Compressor complicates freeing memory Prefer policy changes over cache purging

Memory Footprint Memory warnings override func didreceivememorywarning() { cache.removeallobjects() super.didreceivememorywarning() }

Memory Footprint Memory warnings override func didreceivememorywarning() { cache.removeallobjects() super.didreceivememorywarning() }

Memory Footprint Memory warnings override func didreceivememorywarning() { cache.removeallobjects() super.didreceivememorywarning() } Dictionary NSData NSData

Memory Footprint Memory warnings override func didreceivememorywarning() { cache.removeallobjects() super.didreceivememorywarning() } Dictionary NSData NSData

Memory Footprint Memory warnings override func didreceivememorywarning() { cache.removeallobjects() super.didreceivememorywarning() } Dictionary NSData NSData

Memory Footprint Caching Trade-offs between CPU and memory Remember the compressor Prefer NSCache over dictionary

Memory Footprint Typical app memory profile Dirty Compressed Clean

Memory Footprint Typical app memory profile Dirty Compressed Clean

Memory Footprint Typical app memory profile Footprint Dirty Compressed Clean

Memory Footprint Typical app memory profile Footprint limits Footprint Dirty Compressed Clean

Memory Footprint Typical app memory profile Footprint limits Footprint Limits vary by device Dirty Compressed Clean

Memory Footprint Typical app memory profile Footprint limits Footprint Limits vary by device Apps have a fairly high footprint limit Dirty Compressed Clean

Memory Footprint Typical app memory profile Footprint limits Footprint Limits vary by device Apps have a fairly high footprint limit Dirty Extensions have a much lower limit Compressed Clean

Memory Footprint Typical app memory profile Footprint limits Footprint Limits vary by device Apps have a fairly high footprint limit Dirty Extensions have a much lower limit Exception upon exceeding limit Compressed Clean

Memory Footprint Typical app memory profile Footprint limits Footprint Limits vary by device Apps have a fairly high footprint limit Dirty Extensions have a much lower limit Exception upon exceeding limit Compressed EXC_RESOURCE_EXCEPTION Clean

Tools for Profiling Footprint James Snee, Apple/Software Engineer

Tools for Profiling Footprint Xcode memory gauge

Tools for Profiling Footprint Xcode memory gauge

Tools for Profiling Footprint Xcode memory gauge

Tools for Profiling Footprint Instruments

Tools for Profiling Footprint Instruments Allocations Leaks VM Tracker Virtual memory trace

Tools for Profiling Footprint Instruments VM Tracker

Tools for Profiling Footprint Instruments VM Tracker

Tools for Profiling Footprint Instruments VM Tracker

Tools for Profiling Footprint Instruments Virtual Memory Trace

Tools for Profiling Footprint Instruments Virtual Memory Trace

Tools for Profiling Footprint Instruments Virtual Memory Trace

Tools for Profiling Footprint Xcode Debugger memory resource exceptions NEW

Tools for Profiling Footprint Xcode Debugger memory resource exceptions NEW

Tools for Profiling Footprint Xcode Debugger memory resource exceptions NEW

Tools for Profiling Footprint Xcode Memory Debugger

Tools for Profiling Footprint Xcode Memory Debugger

Tools for Profiling Footprint Xcode Memory Debugger

Tools for Profiling Footprint Xcode Memory Debugger

Tools for Profiling Footprint vmmap NEW Shows virtual memory regions allocated in a process vmmap App.memgraph vmmap --summary App.memgraph

Tools for Profiling Footprint vmmap --summary App.memgraph

Tools for Profiling Footprint vmmap --summary App.memgraph

Tools for Profiling Footprint vmmap --summary App.memgraph

Tools for Profiling Footprint vmmap --summary App.memgraph

Tools for Profiling Footprint Zoom and focus vmmap App.memgraph

Tools for Profiling Footprint Zoom and focus vmmap App.memgraph

Tools for Profiling Footprint vmmap App.memgraph

Tools for Profiling Footprint vmmap App.memgraph

Tools for Profiling Footprint vmmap and AWK

Tools for Profiling Footprint vmmap and AWK

Tools for Profiling Footprint vmmap and AWK

Tools for Profiling Footprint vmmap and AWK

Tools for Profiling Footprint vmmap and AWK

Tools for Profiling Footprint vmmap and AWK

Tools for Profiling Footprint leaks NEW Shows objects that are allocated, but no longer referenced leaks App.memgraph

Tools for Profiling Footprint leaks

Tools for Profiling Footprint leaks

Tools for Profiling Footprint leaks MyApp.memgraph NEW

Tools for Profiling Footprint leaks MyApp.memgraph NEW

Tools for Profiling Footprint leaks MyApp.memgraph NEW

Tools for Profiling Footprint leaks MyApp.memgraph NEW

Tools for Profiling Footprint heap NEW Shows objects allocated on the heap Useful for identifying large objects in memory and what allocated it heap App.memgraph heap App.memgraph -sortbysize heap App.memgraph -addresses all <classes-pattern>

Tools for Profiling Footprint heap App.memgraph

Tools for Profiling Footprint heap App.memgraph

Tools for Profiling Footprint heap App.memgraph

Tools for Profiling Footprint heap App.memgraph

Tools for Profiling Footprint heap App.memgraph

Tools for Profiling Footprint heap --sortbysize App.memgraph

Tools for Profiling Footprint heap --sortbysize App.memgraph

Tools for Profiling Footprint heap App.memgraph -addresses all <classes-pattern>

Tools for Profiling Footprint heap App.memgraph -addresses all <classes-pattern>

Tools for Profiling Footprint Enabling malloc stack logging

Tools for Profiling Footprint Enabling malloc stack logging

Tools for Profiling Footprint malloc_history NEW Shows backtraces for malloc and anonymous VM region allocations malloc_history App.memgraph [address]

Tools for Profiling Footprint malloc_history <memgraph> <address>

Tools for Profiling Footprint malloc_history <memgraph> <address>

Tools for Profiling Footprint Which tool to pick?

Tools for Profiling Footprint Which tool to pick? Creation Reference Size

Tools for Profiling Footprint Which tool to pick? Creation Reference Size malloc_history

Tools for Profiling Footprint Which tool to pick? Creation Reference Size malloc_history leaks

Tools for Profiling Footprint Which tool to pick? Creation Reference Size malloc_history leaks vmmap, heap

Images Kyle Howarth, Apple/Software Engineer

Memory use is related to the dimensions of the image, not the file size.

Images 590Kb file size

Images 590KB file size

Images 2048px 1536px 590KB file size

10MB 2048 pixels x 1536 pixels x 4 bytes per pixel

Images Load Decode Render

Images Load 590Kb

Images Load Decode 590KB 10MB

Images Load Decode Render 590KB 10MB Images and Graphics Best Practices WWWDC 2018

Images Image-rendering formats SRGB Format Four bytes per pixel Full color images

Images Image-rendering formats Wide format Eight bytes per pixel Super accurate colors Only useful with wide color displays Wide color capture cameras - iphone 7, iphone 8, iphone X, ipad Pro 10.5, ipad Pro 13 (2nd generation)

Images Image rendering formats Luminance and alpha 8 format Two bytes per pixel Single-color images and alpha Metal shaders

Images Image rendering formats Alpha 8 Format One byte per pixel Useful for monochrome images - Masks - Emoji-free text 75 percent smaller than SRGB

Images Image rendering formats Optimized formats reduce memory use One byte: Alpha 8 Two bytes: luminance and alpha 8 Four bytes: SRGB Eight bytes: wide format

How do I pick the right format?

Images Picking the right format Don t pick the format, let the format pick you Stop using UIGraphicsBeginImageContextWithOptions Four bytes per pixel format Start or keep using UIGraphicsImageRenderer Introduced in ios 10 Automatically picks best graphics format in ios 12

// Circle via UIGraphicsImageContext let bounds = CGRect(x: 0, y: 0, width:300, height: 100) UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0) // Drawing Code UIColor.black.setFill() let path = UIBezierPath(roundedRect: bounds, byroundingcorners: UIRectCorner.allCorners, cornerradii: CGSize(width: 20, height: 20)) path.addclip() UIRectFill(bounds) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()

// Circle via UIGraphicsImageContext let bounds = CGRect(x: 0, y: 0, width:300, height: 100) UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0) // Drawing Code UIColor.black.setFill() let path = UIBezierPath(roundedRect: bounds, byroundingcorners: UIRectCorner.allCorners, cornerradii: CGSize(width: 20, height: 20)) path.addclip() UIRectFill(bounds) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()

// Circle via UIGraphicsImageRenderer let bounds = CGRect(x: 0, y: 0, width:300, height: 100) let renderer = UIGraphicsImageRenderer(size: bounds.size) let image = renderer.image { context in // Drawing Code UIColor.black.setFill() let path = UIBezierPath(roundedRect: bounds, byroundingcorners: UIRectCorner.allCorners, cornerradii: CGSize(width: 20, height: 20)) path.addclip() UIRectFill(bounds) }

// Circle via UIGraphicsImageRenderer let bounds = CGRect(x: 0, y: 0, width:300, height: 100) let renderer = UIGraphicsImageRenderer(size: bounds.size) let image = renderer.image { context in // Drawing Code UIColor.black.setFill() let path = UIBezierPath(roundedRect: bounds, byroundingcorners: UIRectCorner.allCorners, cornerradii: CGSize(width: 20, height: 20)) path.addclip() UIRectFill(bounds) }

// Circle via UIGraphicsImageRenderer let bounds = CGRect(x: 0, y: 0, width:300, height: 100) let renderer = UIGraphicsImageRenderer(size: bounds.size) let image = renderer.image { context in // Drawing Code UIColor.black.setFill() let path = UIBezierPath(roundedRect: bounds, byroundingcorners: UIRectCorner.allCorners, cornerradii: CGSize(width: 20, height: 20)) path.addclip() UIRectFill(bounds) } // Make circle render blue, but stay at 1 byte-per-pixel image let imageview = UIImageView(image: image) imageview.tintcolor =.blue

// Circle via UIGraphicsImageRenderer let bounds = CGRect(x: 0, y: 0, width:300, height: 100) let renderer = UIGraphicsImageRenderer(size: bounds.size) let image = renderer.image { context in // Drawing Code UIColor.black.setFill() let path = UIBezierPath(roundedRect: bounds, byroundingcorners: UIRectCorner.allCorners, cornerradii: CGSize(width: 20, height: 20)) path.addclip() UIRectFill(bounds) } // Make circle render blue, but stay at 1 byte-per-pixel image let imageview = UIImageView(image: image) imageview.tintcolor =.blue

// Circle via UIGraphicsImageRenderer let bounds = CGRect(x: 0, y: 0, width:300, height: 100) let renderer = UIGraphicsImageRenderer(size: bounds.size) let image = renderer.image { context in // Drawing Code UIColor.black.setFill() let path = UIBezierPath(roundedRect: bounds, byroundingcorners: UIRectCorner.allCorners, cornerradii: CGSize(width: 20, height: 20)) path.addclip() UIRectFill(bounds) } // Make circle render blue, but stay at 1 byte-per-pixel image let imageview = UIImageView(image: image) imageview.tintcolor =.blue

Images Downsampling UIImage is expensive for sizing and to resizing Will decompress original image into memory Internal coordinate space transforms are expensive ImageIO can read image sizes and metadata information without dirtying memory ImageIO can resize images at cost of resized image only

// Image size with UIImage import UIKit // Getting image size let filepath = /path/to/image.jpg let image = UIImage(contentsOfFile: filepath) let imagesize = image.size // Resizing image let scale = 0.2 let size = CGSize(image.size.width * scale, image.size.height * scale) let renderer = UIGraphicsImageRenderer(size: size) let resizedimage = renderer.image { context in image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) }

// Image size with UIImage import UIKit // Getting image size let filepath = /path/to/image.jpg let image = UIImage(contentsOfFile: filepath) let imagesize = image.size // Resizing image let scale = 0.2 let size = CGSize(image.size.width * scale, image.size.height * scale) let renderer = UIGraphicsImageRenderer(size: size) let resizedimage = renderer.image { context in image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) }

// Image size with UIImage import UIKit // Getting image size let filepath = /path/to/image.jpg let image = UIImage(contentsOfFile: filepath) let imagesize = image.size // Resizing image let scale = 0.2 let size = CGSize(image.size.width * scale, image.size.height * scale) let renderer = UIGraphicsImageRenderer(size: size) let resizedimage = renderer.image { context in image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) }

// Image size with ImageIO import ImageIO let filepath = /path/to/image.jpg let url = NSURL(fileURLWithPath: path) let imagesource = CGImageSourceCreateWithURL(url, nil) let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) let options: [NSString: Any] = [ kcgimagesourcethumbnailmaxpixelsize: 100, kcgimagesourcecreatethumbnailfromimagealways: true ] let scaledimage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)

// Image size with ImageIO import ImageIO let filepath = /path/to/image.jpg let url = NSURL(fileURLWithPath: path) let imagesource = CGImageSourceCreateWithURL(url, nil) let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) let options: [NSString: Any] = [ kcgimagesourcethumbnailmaxpixelsize: 100, kcgimagesourcecreatethumbnailfromimagealways: true ] let scaledimage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)

// Image size with ImageIO import ImageIO let filepath = /path/to/image.jpg let url = NSURL(fileURLWithPath: path) let imagesource = CGImageSourceCreateWithURL(url, nil) let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) let options: [NSString: Any] = [ kcgimagesourcethumbnailmaxpixelsize: 100, kcgimagesourcecreatethumbnailfromimagealways: true ] let scaledimage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)

// Image size with ImageIO import ImageIO let filepath = /path/to/image.jpg let url = NSURL(fileURLWithPath: path) let imagesource = CGImageSourceCreateWithURL(url, nil) let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) let options: [NSString: Any] = [ kcgimagesourcethumbnailmaxpixelsize: 100, kcgimagesourcecreatethumbnailfromimagealways: true ] let scaledimage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)

Optimizing when in the background

Unload large resources you cannot see.

Optimizing When in the Background Unloading when off-screen App lifecycle Listen for UIApplicationWillEnterForeground and UIApplicationDidEnterBackground notifications Applies to on-screen views UIViewController appearance cycle Leverage viewwillappear and viewdiddisappear Off-screen view controllers in UITabBarController and UINavigationController

// Unloading resources on foreground/background NotificationCenter.default.addObserver(forName:.UIApplicationDidEnterBackground, object: nil, queue:.main) { [weak self] (note) in // Unload large resources when off-screen self.unloadimages() } NotificationCenter.default.addObserver(forName:.UIApplicationWillEnterForeground, object: nil, queue:.main) { [weak self] (note) in // Load large resources when off-screen self.loadimages() }

// Unloading resources on foreground/background NotificationCenter.default.addObserver(forName:.UIApplicationDidEnterBackground, object: nil, queue:.main) { [weak self] (note) in // Unload large resources when off-screen self.unloadimages() } NotificationCenter.default.addObserver(forName:.UIApplicationWillEnterForeground, object: nil, queue:.main) { [weak self] (note) in // Load large resources when off-screen self.loadimages() }

// Unloading resources on foreground/background NotificationCenter.default.addObserver(forName:.UIApplicationDidEnterBackground, object: nil, queue:.main) { [weak self] (note) in // Unload large resources when off-screen self.unloadimages() } NotificationCenter.default.addObserver(forName:.UIApplicationWillEnterForeground, object: nil, queue:.main) { [weak self] (note) in // Load large resources when off-screen self.loadimages() }

// Unloading resources on foreground/background // Unload large resource when off-screen override func viewdiddisappear(_ animated: Bool) { unloadimages() super.viewdiddisappear(animated) } // Load large resources when on-screen override func viewwillappear(_ animated: Bool) { loadimages() super.viewwillappear(animated) }

// Unloading resources on foreground/background // Unload large resource when off-screen override func viewdiddisappear(_ animated: Bool) { unloadimages() super.viewdiddisappear(animated) } // Load large resources when on-screen override func viewwillappear(_ animated: Bool) { loadimages() super.viewwillappear(animated) }

// Unloading resources on foreground/background // Unload large resource when off-screen override func viewdiddisappear(_ animated: Bool) { unloadimages() super.viewdiddisappear(animated) } // Load large resources when on-screen override func viewwillappear(_ animated: Bool) { loadimages() super.viewwillappear(animated) }

Demo Kris Markel, Apple/Software Engineer

Summary

Summary Memory is a finite and shared resource

Summary Memory is a finite and shared resource Monitor memory use when running from Xcode

Summary Memory is a finite and shared resource Monitor memory use when running from Xcode Let ios pick your image formats

Summary Memory is a finite and shared resource Monitor memory use when running from Xcode Let ios pick your image formats Use ImageIO for downsampling images

Summary Memory is a finite and shared resource Monitor memory use when running from Xcode Let ios pick your image formats Use ImageIO for downsampling images Unload large resources that are off-screen

Summary Memory is a finite and shared resource Monitor memory use when running from Xcode Let ios pick your image formats Use ImageIO for downsampling images Unload large resources that are off-screen Use memory graphs to further understand and reduce memory footprint

Summary Memory is a finite and shared resource Monitor memory use when running from Xcode Let ios pick your image formats Use ImageIO for downsampling images Unload large resources that are off-screen Use memory graphs to further understand and reduce memory footprint

More Information https://developer.apple.com/wwdc18/416