IOS Memory Management
Introduction
The basic idea of memory management in iOS is reference counting, which controls the lifecycle of memory objects through their reference counts. In terms of programming practice, there are mainly two approaches:
1. MRR (manual retain-release), manual reference counting, where object creation, destruction, and reference count changes are all handled by the developer.
2. ARC (Automatic Reference Counting), automatic reference counting, where you only handle object creation and no longer need to worry about destruction; it is used similarly to garbage collection, but its essence is still reference counting.
Common Problems
According to Apple's documentation, the two main problems are:
Releasing or overwriting data that is still in use. This causes memory corruption, usually resulting in an application crash or, worse, corrupted user data.
Not releasing data that is no longer used leads to memory leaks. Leaked memory is not released even if it is never used again. Leaks cause an application's memory usage to grow steadily, which in turn can lead to poor system performance or crashes.
Memory Management Rules
Release the objects we create when they are no longer needed.
Retain the objects you need to use; if unnecessary, release them.
Do not release objects we do not own.
Using Memory Management Tools
You can analyze memory usage with the help of Xcode's Instruments tool. It includes tools such as Activity Monitor, Allocations, Leaks, and Zombies.
Steps to Analyze Memory Allocation
-
Open an existing application.
-
Select Product, then Profile, as shown below

3. Select Allocations and Profile in the following screen.

-
We can see the memory usage of different objects
-
You can switch view controllers to see whether memory is released.

6. Similarly, we can use Activity Monitor to view how memory is allocated in the application.

- These tools help us understand memory usage and where leaks may occur.