iOS memory management


May 21, 2021 08:00 iOS Development Manual


Table of contents


IOS memory management


Brief introduction

The basic idea of memory management under iOS is to reference count, which controls the life cycle of memory objects through the reference count of objects. There are two main ways to program time:

1: MRR (manual retain-release), manual reference count, object generation, destruction, reference count changes are done by the developer.

2: ARC (Automatic Reference Counting), which automatically references counts and is only responsible for the generation of objects, is no longer of concern to other process developers and is used in a manner similar to garbage collection, but is essentially a reference count.

problems

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

The data released or overwritten is still in use. This can cause memory corruption, usually in application crashes, or worse, damage to user data.

Not releasing data that is no longer in use can result in memory leaks. A llocated memory, memory leaks are not released, even if it is never used again. Leaks can lead to increasing memory usage for applications, which in turn can result in poor system performance or crashes.

Memory management rules

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

Keep the objects you need to use. If there is no need to release these objects.

Don't release objects we don't have.

Use memory management tools

Memory usage can be analyzed with the help of Xcode tool instruments. It includes tools such as Activity Monitor, Distribution, Leak, Zombie, etc

Steps to analyze memory allocation

1. Open an existing application.

2. Select the product, as shown in the profile below

iOS memory management

3. Select Allocations and Profile in the following interface.

iOS memory management

4. We can see the memory usage of different objects

5. You can switch view controllers to see if memory is freed.

iOS memory management

6. Similarly, we can use Activity Monitor to see how the allocation is in the application.

iOS memory management

7. These tools can help us understand how memory is used and where leaks can occur.