Discussion - Allocations and Leaks instruments

Extraordinarily hard to spot Tough to find offending code without help from tools Unbounded Memory Growth (memory growth without a chance to collect (deallocate) memory True Leaks (retain cycles) Allocations

Generation Analysis Tracks allocations still resident when the generation is marked

  • As you do multiple generations you will see only the new allocations since the last generations

Simulate Memory Warning (did it help, do you have anything observing for this?) I have unbounded memory growth, now what?

  • Look for the biggest offenders (sorting)
  • Drill into the code and look for ways to release unnecessary allocations

Good ‘ol fashion memory management If you’re intentionally holding onto objects, consider implementing an observer for UIApplicationDidReceiveMemoryWarningNotification to release them

Leaks / Retain Cycles aka. Strong Reference Cycles

Persistent vs. Transient Static Code Analyis Narrow list to your code Use / Observe (detective work) “You’re in the ballpark” now what?

Reference counting Weak and Unowned

Closure example with capture list; weak and unowned

Apple says, “Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Use an unowned reference when you know that the reference will never be nil once it has been set during initialization.”

Picks

Darryl

Visual Debugging with Xcode WWDC 2016 Session 410 demonstrates the use of the new Memory Graph Debugger starting at about 24 minutes in

John

"Weak, Strong, Unowned, Oh My!" - a Guide to References in Swift by Hector Matos