Using unit tests to identify and avoid memory leaks in Swift

Click for: original source

John Sundell blog post about memory leaks in Swiflang. In it he discusses managing memory and avoiding leaks and how it is a crucial part of building any kind of program. According to author Swift makes this relatively easy in most situations – thanks to Automatic Reference Counting (or ARC for short).

Memory leaks in Swift are often the product of a retain cycle, when one object will hold a strong reference to an object that also strongly references the original object. So A retains B and B retains A.

He then guides you through setting up of unit tests to both help us identify memory leaks and also make it easier to avoid common mistakes that could end up causing leaks in the future.

For a purpose of common memory leaks sources, following patterns are mentioned:

  • Delegate pattern
  • Observer pattern
  • Closures – closure-based API

The usage of these in unit tests is explained in detail plus code examples. Even though tests like presented in the article won’t completely protect you against memory leaks, they can potentially reduce the amount of time you’ll have to spend hunting down the cause of memory related issues in the future. Good read!

[Read More]

Tags programming swiftlang tdd