MEMORY MANAGEMENT

Memory management in Objective-C programming (especially for iOS) is a very important topic that every iOS developer needs to be aware of. Like all other popular languages, Objective-C supports garbage collection, which helps to remove unused objects when they go out of scope and hence releases memory that can be reused. However, because of the severe overhead involved in implementing garbage collection, the iOS does not support garbage collection. This leaves you, the developer, to manually allocate and de-allocate the memory of objects when they are no longer needed.

This section discusses the various aspects of memory management on the iOS.

Reference Counting

To help you allocate and de-allocate memory for objects, the iOS uses a scheme known as reference counting to keep track of objects to determine whether they are still needed or can be disposed of. Reference counting basically uses a counter for each object; and as each object is created, the count increases by 1. When an object is released, the count decreases by 1. When the count reaches 0, the memory associated with the object is reclaimed by the OS.

In Objective-C, a few important keywords are associated with memory management. The following sections take a look at each of them.

NEW FEATURE: AUTOMATIC REFERENCE COUNTING

In iOS 5, Objective-C now supports a new feature known as Automatic Reference Counting (ARC). Instead of needing you to keep track of each object's ownership, ARC enables the compiler ...

Get Beginning iOS 5 Application Development 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.