Memory Management on iOS

Preview:

Citation preview

MEMORY MANAGEMENT ON IOS

AGENDA

Reference Count Based Memory Management

Manual Reference Counting

Automatic Reference Counting (ARC)

ARC vs. Tracing Garbage Collectors

Gotchas

REFERENCE COUNT BASED MEMORY MANAGEMENT

WHY DO WE NEED MEMORY MANAGEMENT?

func createPerson() { var p = Person()}

main

createPerson()

Person

Stack Heap

WHY DO WE NEED MEMORY MANAGEMENT?

class Business { var owner: Person init() { self.owner = Person() } }

1

1

2

Person

Stack Heap

Business

ViewController

Reference Count

MANUAL REFERENCE COUNTING

MANUAL REFERENCE COUNTING

Objects are held in memory as long as they have a retain

count that is larger than 0

MANUAL REFERENCE COUNTING

Retain count of an object is mainly determined by calls to three

different methods: retain, release, autorelease

Additionally objects you create have a retain count of 1 - in Obj-

C this is indicated by calling any method named “alloc”, “new”,

“copy”, or “mutableCopy”

MANUAL REFERENCE COUNTING

- (void)createPerson { Person *person = [[[Person alloc] init] autorelease]; return person;}

+1 -1

MANUAL REFERENCE COUNTING@interface Business : NSObject

@property (nonatomic, strong) Person *name;

@end

+1 -1- (void)createOwner { self.owner = [[[Person alloc] init] autorelease];} +1

- (void)freeOwner { self.owner = nil;}

-1

AUTOMATIC REFERENCE COUNTING (ARC)

AUTOMATIC REFERENCE COUNTING (ARC)

No manual calls to retain, release, autorelease anymore

Memory Management calls are inserted at compile time

Think in strong, weak and unowned references

AUTOMATIC REFERENCE COUNTING

class Business { var owner: Person weak var building: Building? init() { self.owner = Person() self.building = Building() } }

+1+0

ARC VS. TRACING GARBAGE COLLECTORS

ARC VS. TRACING GARBAGE COLLECTORS

Tracing Garage Collectors are used in many higher level languages

such as C#, Java and Ruby (Python uses reference counts)

ARC VS. TRACING GARBAGE COLLECTORS

Tracing GCs collect unreferenced objects after certain time periods at

runtime, typically work in two phases:

1. Mark all objects that are reachable from a root object

2. Delete all objects that are not reachable from a root object

GOTCHAS

GOTCHAS

Pure reference counting garbage collection mechanisms cannot detect retain cycles!

Retain cycles occur when multiple objects reference each

other strongly, but aren’t strongly referenced by any root object

GOTCHASRetain cycles created by the usage of closures:

class UserViewController: UIViewController { var callback: UserDetailsCallback?

override func viewDidAppear(animated: Bool) { callback = { user in self.showUser(user) } } func showUser(user: User?) { //... } }

UserViewController

UserDetailsCallback+1

+1

GOTCHASFix retain cycles using weak/unowned:

class UserViewController: UIViewController { var callback: UserDetailsCallback?

override func viewDidAppear(animated: Bool) { callback = { [unowned self] user in self.showUser(user) } } func showUser(user: User?) { //... } }

UserViewController

UserDetailsCallback+1

+0

GOTCHASRetain cycles created by the usage of strong delegates:

protocol UserViewDelegate {}

class UserView { var delegate: UserViewDelegate? }

class UserViewController: UIViewController { var userView: UserView = UserView() func viewDidLoad() { super.viewDidLoad() userView.delegate = self } }

UserViewController

UserView+1

+1

GOTCHASFix using a weak reference to the delegate:

protocol UserViewDelegate {}

class UserView { weak var delegate: UserViewDelegate? }

class UserViewController: UIViewController { var userView: UserView = UserView() func viewDidLoad() { super.viewDidLoad() userView.delegate = self } }

UserViewController

UserView+1

+0

SUMMARY

SUMMARYSwift’s memory management model is based on reference

counting

With ARC reference counting works semi-automatically, retain

and release calls are inferred from memory management specifiers (strong, weak, unowned)

Some types of memory issues that can be detected by tracing

GCs cannot be detected by reference counting, e.g. retain cycles

ADDITIONAL RESOURCES

ADDITIONAL RESOURCES

Apple: Advanced Memory Management Programming Guide

iOS Memory Management Using Autorelease

Wikipedia: Tracing Garbage Collectors

Recommended