Memory management

iPhone and iPad devices are resource-constrained on memory. An app may be terminated by the operating system if it crosses the established per-process limit.1 As such, successfully managing memory plays a central role in implementing an iOS app.

At WWDC 2011, Apple revealed that about 90% of device crashes happened due to issues pertaining to memory management. And of these, the biggest causes are either bad memory access or memory leaks due to retain cycles.

Unlike the Java runtime (which uses garbage collection), iOS runtimes for Objective-C and Swift use reference counting. The downsides of using reference counting include possible overrelease of memory and cyclic references if the developer is not careful.

As such, it is important to understand how memory is managed in iOS.

In this lesson, we study the following:

  • Memory consumption (i.e., how an app consumes memory)

  • The memory management model (i.e., how the iOS runtime manages memory)

  • Language constructs—we’ll take a look at Objective-C constructs and the available features you can use

  • Best practices for minimizing memory usage without degrading the user experience

Memory Consumption

Memory consumption refers to the RAM that an app consumes.

The iOS virtual memory model does not include swap memory, which means that, unlike with desktop apps, the disk cannot be used to page memory. The end result is that the apps are restricted to available RAM, which is used not only by the app in the foreground ...

Get Memory management now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.