Easy Tutorial
❮ Att Ios Ui Toolbar Ios Intro ❯

iOS Memory Management


Introduction

The fundamental concept of memory management in iOS is reference counting, where the lifecycle of memory objects is controlled through the reference count of objects. Specifically in programming practice, there are two main methods:

  1. MRR (Manual Retain-Release), where the creation, destruction, and changes in reference counting of objects are handled by the developer.

  2. ARC (Automatic Reference Counting), which only manages the creation of objects, with other processes, such as destruction, no longer requiring attention from the developer. This method is similar to garbage collection but is essentially still based on reference counting.

Challenges

According to Apple's documentation, the two main challenges are:

Using data that has been released or overwritten. This causes memory corruption, typically leading to application crashes, or worse, corrupting user data.

Not releasing data that is no longer in use leads to memory leaks. Allocated memory that leaks is not released, even if it is never used again. Leaks can cause the application's memory usage to increase over time, which in turn can lead to poor system performance or system crashes.

Memory Management Rules

We create our own objects and release them when they are no longer needed.

Retain objects that need to be used. There is no need to release these objects if not necessary.

Do not release objects that we do not own.

Using Memory Management Tools

Xcode's Instruments can be used to analyze memory usage. The included tools are Activity Monitor, Allocations, Leaks, Zombies, etc.

Steps to Analyze Memory Allocations

  1. Open an existing application.

  2. Select Product, then Profile as shown.

  3. In the following interface, select Allocations and Profile.

  4. We can see the memory usage of different objects.

  5. You can switch view controllers to check if memory is being released.

  6. Similarly, we can use the Activity Monitor to view memory allocation within the application.

  7. These tools help us understand memory usage and where potential leaks might occur.

❮ Att Ios Ui Toolbar Ios Intro ❯