Is the default reference type in Swift ▸ Protects the referred object from getting deallocated by ARC ▸ Increases the reference count by one ▸ Can be a let or var ▸ Can be optional or not
Doesn’t increase the reference count ▸ Doesn’t protect the referred object from being deallocated by ARC ▸ Is automatically set to nil on deallocation ▸ Must be a var ▸ Must be optional
Doesn’t increase the reference count ▸ Doesn’t protect the referred object from being deallocated by ARC ▸ Isn't automatically set to nil on deallocation ▸ Can be a let or var ▸ Cannot be an optional
country Object deallocated Pointers to an empty slot in memory; They will cause an error when accessed Empty space in memory nil strongReference Three names unownedImmutableReference unownedReference
REFERENCE TO BECOME NIL AT SOME POINT DURING ITS LIFETIME. CONVERSELY, USE AN UNOWNED REFERENCE WHEN YOU KNOW THAT THE REFERENCE WILL NEVER BE NIL ONCE IT HAS BEEN SET DURING INITIALISATION. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ AutomaticReferenceCounting.html IDENTIFY AND FIX MEMORY LEAKS IN YOUR APP WEAK VS. UNOWNED - AN ADVICE FROM APPLE
LEAKS SO BAD ? ‣ Unwanted side effects: ‣ Memory warnings that can lead to: ‣ Slowness ‣ Screen freezes ‣ Eventual crashes ‣ All of that translated into bad App Store reviews/ratings.
https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html Creates strong reference to a Country object Creates strong reference to a City object A City exists in and knows this Country
IDENTIFYING LEAKS ▸ Running an inspection in your code ▸ Can be very tedious and time consuming ▸ Profile your app with Instruments: ▸ Leaks ▸ Allocations ▸ Debug you memory graph in Xcode Memory Debugger
MEMORY GRAPH DEBUGGER ▸ Put your app in an initial state, such as your main screen, and take a memory snapshot Take memory snapshot In Xcode’s debugging toolbar
MEMORY GRAPH DEBUGGER ▸ On the left panel, take a look at the classes that are in memory and how many instances they have (that is, how many objects of that class are allocated) by checking number in parenthesis. Eg: There is only one instance of the class ViewController
MEMORY GRAPH DEBUGGER ▸ Run through a core flow/feature, repeat it several times and take a memory snapshot of your app again; ▸ On the left panel, observe three things: ▸ Is there any instance that should have been deallocated already? ▸ Has the number of instances increased? ▸ Has the amount of memory consumed increased? If any of these answers is yes, you have probably found a leak!
- CHECKLISTS ▸ Click on add button in navigation bar to create an item ▸ Click on an item’s disclosure button to edit it ▸ Click on an item’s name to toggle its checked state ▸ Swipe left on an item to delete it
occupied by instances of these classes was multiplied by 4 Persistent instances are never erased from memory; Transient instances are created and erased in the time frame we're analysing
CYCLES ‣ Using unowned If A retains B and B retains A, then it's a leak Adapted from https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html Creates strong reference to a Country object Creates strong reference to a City object
LIST ‣ A pairing of the weak or unowned keywords with a reference to a class instance. ‣ Written within a pair of square braces, separated by commas. Place the capture list before a closure’s parameter list and return type if they are provided
Memory leaks can be a little hard to find but easy to fix ▸ Retain cycles, Closures and NotificationCenter are the most common sources of leaks (but not the only ones) ▸ You can easily break retain cycles with weak and unowned ▸ Use swift's capture list for closures ▸ Instruments and Xcode Memory Debugger are great tools to find leaks ▸ Always profile your app and keep an eye on memory consumption