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
GOTCHAS
Retain 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
GOTCHAS
Fix 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
GOTCHAS
Retain 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
GOTCHAS
Fix 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
SUMMARY
Swift’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

Memory Management on iOS

  • 2.
  • 3.
    AGENDA Reference Count BasedMemory Management Manual Reference Counting Automatic Reference Counting (ARC) ARC vs. Tracing Garbage Collectors Gotchas
  • 4.
  • 5.
    WHY DO WENEED MEMORY MANAGEMENT? func createPerson() { var p = Person() } main createPerson() Person Stack Heap
  • 6.
    WHY DO WENEED MEMORY MANAGEMENT? class Business { var owner: Person init() { self.owner = Person() } } 1 1 2 … … Person Stack Heap Business ViewController Reference Count
  • 7.
  • 8.
    MANUAL REFERENCE COUNTING Objectsare held in memory as long as they have a retain count that is larger than 0
  • 9.
    MANUAL REFERENCE COUNTING Retaincount 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”
  • 10.
    MANUAL REFERENCE COUNTING -(void)createPerson { Person *person = [[[Person alloc] init] autorelease]; return person; } +1 -1
  • 11.
    MANUAL REFERENCE COUNTING @interfaceBusiness : NSObject @property (nonatomic, strong) Person *name; @end +1 -1- (void)createOwner { self.owner = [[[Person alloc] init] autorelease]; } +1 - (void)freeOwner { self.owner = nil; } -1
  • 12.
  • 13.
    AUTOMATIC REFERENCE COUNTING (ARC) Nomanual calls to retain, release, autorelease anymore Memory Management calls are inserted at compile time Think in strong, weak and unowned references
  • 14.
    AUTOMATIC REFERENCE COUNTING class Business{ var owner: Person weak var building: Building? init() { self.owner = Person() self.building = Building() } } +1 +0
  • 15.
    ARC VS. TRACINGGARBAGE COLLECTORS
  • 16.
    ARC VS. TRACINGGARBAGE COLLECTORS Tracing Garage Collectors are used in many higher level languages such as C#, Java and Ruby (Python uses reference counts)
  • 17.
    ARC VS. TRACINGGARBAGE 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
  • 18.
  • 19.
    GOTCHAS Pure reference countinggarbage 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
  • 20.
    GOTCHAS Retain cycles createdby 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
  • 21.
    GOTCHAS Fix retain cyclesusing 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
  • 22.
    GOTCHAS Retain cycles createdby 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
  • 23.
    GOTCHAS Fix using aweak 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
  • 24.
  • 25.
    SUMMARY Swift’s memory managementmodel 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
  • 26.
  • 27.
    ADDITIONAL RESOURCES Apple: AdvancedMemory Management Programming Guide iOS Memory Management Using Autorelease Wikipedia: Tracing Garbage Collectors