+ All Categories
Home > Software > Memory management in iOS.

Memory management in iOS.

Date post: 10-Feb-2017
Category:
Upload: hsieh-ching-fan
View: 335 times
Download: 0 times
Share this document with a friend
21
Study group Memory management 2014/12/16 JoeHsieh
Transcript
Page 1: Memory management in iOS.

Study group Memory management

2014/12/16 JoeHsieh

Page 2: Memory management in iOS.

Outline

• Item 29 : Understand Reference Counting

• Item 30 : Use ARC to Make Reference Counting Easier.

Page 3: Memory management in iOS.

Manual Retain Release (MRR)

Page 4: Memory management in iOS.

Manual Retain Release(1/2)

• Tracks how many owners the object has.When it reaches zero, the OS allowed to destroy it.

Page 5: Memory management in iOS.

Manual Retain Release(2/2)• increment

• alloc : Creates an object and claims ownership of it.

• retain : Claims ownership of an existing object

• copy : Copy an object and claims ownership of it

• decrement

• release : Relinquishes ownership of an object and destroy it immediately

• autorelease : Relinquishes ownership of an object but defer its destruction

Page 6: Memory management in iOS.

In case of retain/release is not balanced

• Forgets to release, retain cycle, etc… : memory leak

• Releases too many times, etc… : dangling pointer(zombie)

Page 7: Memory management in iOS.

ExampleMemory Management in Property Accessors

Page 8: Memory management in iOS.

CarStore

Page 9: Memory management in iOS.

Crash?

Page 10: Memory management in iOS.

Dangling pointer and memory leak.

setter will not retain inventory

superstore forget to release

Page 11: Memory management in iOS.

Fixes it, but it’s still memory leak?

Page 12: Memory management in iOS.

Finally…

Page 13: Memory management in iOS.

Automatic Reference Counting (ARC)

In order to solve the verbose problem of MMR, savior is coming!

@property (strong, nonatomic) NSMutableArray *inventory;

For property accessor

Page 14: Memory management in iOS.

ARC• Complier will add retain, release and autorelease

for you automatically.You cannot add these by yourself.

• Memory leak and dangling pointer exist still.

• ARC is only for Objective-C.That means memory management of CoreFundation and C is still MRR.(malloc, free, CFRetain, CFRelease)

Page 15: Memory management in iOS.

Retain Cycle

Page 16: Memory management in iOS.

How to prevent leak ?• Pointer to any parent must not be retained.

Page 17: Memory management in iOS.

Retain Cycle Example

• strong delegate

• retain variables in block

Page 18: Memory management in iOS.

How to solve leak ?• Breaks retain relationship explicitly.

Page 19: Memory management in iOS.

Retain Cycle Example

• Timer

• NSURLConnectionDelegate

Page 20: Memory management in iOS.

Retain Cycle Demo

PrinterVC

Printer

Page 21: Memory management in iOS.

References

• http://rypress.com/tutorials/objective-c/memory-management

• http://www.cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html

• https://www.mikeash.com/pyblog/friday-qa-2010-04-30-dealing-with-retain-cycles.html


Recommended