This article will first briefly describe some basic concepts and technologies involved in iOS memory management, and we will discuss related knowledge points in more depth in the next series of chapters.

First, iOS memory management

IOS memory management, or memory management in any language, is a must for programmers to understand and is the most common topic in interviews. Before iOS 5, you need to manually manage memory, that is, Manual Reference Counting (MRC). In 2011, Apple launched ARC (Automatic Reference Counting) technology, which eliminates the need for programmers to manually manage memory, significantly improving development efficiency and App stability. Whether MRC or ARC, the essence of Apple memory management remains the same -- the management of reference counts for OC objects. The concept of reference counting is similar to the C++ concept of smart Pointers in that compile time and run time help us to automatically maintain the life and death of an object. So reference counting is the ** root of iOS memory management, and everything to do with memory management comes down to reference counting.Copy the code

Second, memory layout

Before getting into the technical details of iOS memory management, we need to understand the various variables and how objects are laid out in memory. We ran the following test (on a real iPad Pro, the results may vary from device to device), Corresponding code - [memory] layout (https://github.com/wenghengcong/LearnCollection/tree/master/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/%E5%86%85% E5 AD 83% % 98% B8 E5% % % % E5 B1%80).Copy the code

Thus, we drew the following simple memory layout diagram:

In the figure below, there is one thing to note – the object address we print out, that is, the address allocated to the heap, is actually larger than the stack address. This is not the case in the figure below. The following diagram illustrates the general rule, but it varies across operating systems and compile-time implementations, and addresses allocated in the heap tend to have more virtual memory.

OC, of course, is derived from C, and its memory layout is similar. Here is a picture of C’s memory layout (source network, intrusion deletion) :

The figure illustrates some sections in more detail:

  • .bss: global variables that hold uninitialized data, and static modified variables;
  • .data: stores initialized global variables and static modified variables;
  • .text: stores code, and constants such as const, which include const modifiers, and constant strings.

Third, memory management technology

3.1 Tagged Pointer

**Tagged Pointer** is a technology that stores data ** in the Pointer that ** is assigned to an object and uses tags to identify the type of data. An object using Tagged Pointer does not count references and does not need to be retained or released. Even after a variable is out of scope, it can still be accessed. The classes that can use the **Tagged Pointer** technique are: 'NSNumber', 'NSString', 'NSDate', etc. For example, 'NSNumber' is stored in memory as follows:Copy the code

I’ll cover this technique in detail in the next article, Memory Management (ii) Tagged Pointer.

3.2 Optimized ISA pointer

After the launch of 64-bit devices, Apple found the ISA pointer for 64-bit objects to be wasteful, so they improved it. The results are as follows:

The 'ISA pointer' has been optimized to store reference counts in extra_rc with its 19-bit extra_rc.Copy the code

But if exTAR_rc is not enough, you need to store the reference count in a data structure called Side Table.

If that's not enough, so sorry, you should reflect on why such a bizarre thing happens.Copy the code

3.3 Reference Counting

Back to the roots of iOS memory management — reference counting.

NSObject provides a method to control the reference count above:

3.4 Object Life Cycle

For the above description of reference counting, I believe that everyone has a basic understanding of the object reference counting, so how is the object reference counting related to the life cycle of the object?

Four, attributes,

In development, attributes are so commonly used that there are no attributes to write code.Copy the code

4.1 What are attributes

Properties are a feature of Objective-C and are declared with '@property'. A variable declared with '@property' is automatically generated at compile time for its corresponding accessor, that is, setter and getter methods. In addition, it allows developers to specify @property parameters, including atomicity, memory management semantics, etc., and compile-time implementations of 'setters' and' getters' will vary depending on the properties. The essence of attribute can be simply summed up as: attribute = member variable + 'setter' + 'getter', but it is more than this, for the rich change of attribute characteristics, is the weapon of the developer.Copy the code

4.2 @propertytrait

Here, we describe only the attributes associated with memory management, as follows:

Five, the problem of

Apple provides such a convenient count for developers, and developers are so good. How could it go wrong. Problems do occur, usually in two categories: bad memory access and circular references.Copy the code

5.1 Accessing Bad Memory

Access to bad memory is also divided into two parts:

5.1.1 wild pointer

The memory address of the object has been released, and then access, will produce wild pointer problems, may cause a crash;

5.1.2 null pointer

The object accessing the memory address is freed and set to nil. No result is obtained from the re-access, resulting in a null pointer problem.

5.2 Memory Leakage

5.2.1 Memory Leakage

A memory leak is a failure to free objects that are no longer referenced. For example, under MRC:

- (void)test {BFPerson *person = [[BFPerson alloc] init]; }Copy the code

However, for the above case, under ARC, compile time will automatically release it for us.

Another, more common memory leak under ARC is circular references.

5.2.2 Circular Reference

Circular references are created when two objects have a strong reference to each other, as is the case with three or four objects. A refers to B, and B refers to A. Both parties keep A reference to each other at the same time. As A result, the reference count is not zero at any time and cannot be released.Copy the code

We will detail this issue in a later article on memory management (7) circular reference – placeholders.

5.3 analysis

We can analyze the above problems through Xcode or other tools.Copy the code

5.3.1 Monitoring zombie Objects

5.3.2 Instruments

In the figure below, Instruments provides a set of tools for analyzing all aspects of an App, circled with memory-related tools.

5.3.3 FBMemoryProfiler

GitHub is the address of facebook’s memory detection tool.

reference

link

  1. Advanced Memory Management Programming Guide
  2. Memory Management Programming Guide for Core Foundation
  3. Automatic Reference Counting
  4. Objective-C Automatic Reference Counting (ARC)
  5. FBMemoryProfiler

The sample code

  1. Memory layout