Memory alignment exploration – early

The underlying exploration will definitely involve memory. The previous alloc exploration talked about the principle of memory creation, so what is the way of memory storage sort, and what is the size of memory related? So let’s verify that.

Object Property Display

When an object has only one attribute variable

The results are as follows:

When an object has two attribute variables

The results are as follows:

I continued to test the effects of adjusting attribute variables to member variables, adding methods, adding class methods, etc., which are not shown here and can be verified by myself. The following conclusions can be drawn from my own verification:

  • sizeofIs to calculate the size of the parameter passed in, because this is the object pointer, and the pointer is 8 bytes
  • class_getInstanceSizeWhat is obtained is the size of the object calculated according to the actual structure
  • malloc_sizeThe actual size of object memory allocated by the system

By adding an NSString attribute variable, the calculated memory for the object is increased by 8, which is the length of a pointer. So why is the memory size different during system allocation? This is our system memory alignment principle.

Memory alignment exploration – Principles

  1. Data member alignment rules: The first data member starts at 0, and each subsequent data member starts at an integer multiple of the size of the data member or its children (arrays and structures have children).
  2. When a structure is a member, the structure members are from within itAn integer multiple of the size of the largest elementStart saving.
  3. The total sizeof the structure (sizeof output) must be an integer multiple of the largest internal member,Less than a filling.

Memory alignment exploration – Data types correspond to memory sizes

Why do you need to make it up?

  • Memory is in the unit of bytes. Data access by CUP is in the unit of blocks. Frequent access to unaligned data increases CPU reading tools and reduces performance. And memory alignment, we talked about space for time.

Memory is based on bytes, while CPU accesses data in blocks rather than bytes. Frequent access to unaligned data can significantly degrade CPU performance. Byte alignment reduces the number of CPU accesses. The purpose of this space – for – time swap is to reduce CPU overhead. CPU access is in blocks, and access to unaligned data may start in one block and end in another. In this way, the middle may be combined together through complex operations, reducing efficiency. Byte alignment improves CPU access speed.

Here’s an example: 🌰

struct LGStruct1 {
    double a;       // 8    [0 7]
    char b;         // 1    [8]
    int c;          // 4    (9 10 11 [12 13 14 15]
    short d;        // 2    [16 17] 24
}struct1;

struct LGStruct2 {
    double a;       // 8    [0 7]
    int b;          // 4    [8 9 10 11]
    char c;         // 1    [12]
    short d;        // 2    (13 [14 15] 16
}struct2;

struct LGStruct3 {
    double a;       // 8    [0 7]
    int b;          // 4    [8 11]
    char c;         // 1    [12]
    short d;        // 2    [14 15]
    int e;          // 4    [16 19] 
    struct LGStruct1 str;  // 24    [24 47]  48
} struct3;

Copy the code

We first calculate the memory size of struct1 according to the alignment principle

  • variablea: offset Starts from 0. The storage position is[0]
  • variableb: offset Starts from 8 (8 is a multiple of data size 1). The storage location is[8]
  • variablec: offset starts from 9 (9 is not a multiple of 4 and starts from 12). The storage position is12 - [15]
  • variabled: offset Starts from 16 (16 is a multiple of data size 2). The storage location is[16-17]

We calculate that the structure size should be 18, following the principle of memory alignment 3,18 is not a multiple of the maximum data size 8, complement is 24, so we get the structure struct1 memory allocation size is 24


Struct2 memory size is calculated as follows:

  • variablea: offset Starts from 0. The storage position is[0]
  • variableb: offset Starts from 8 (8 is a multiple of data size 1). The storage location is[8 to 11]
  • variablec: offset Starts from 12 (11 is a multiple of data size 1). The storage location is[12]
  • variabled: offset starts from 13 (13 is not a multiple of 2 but starts from 14). The storage location is[14, 15]

We calculate that the size of the structure should be 16, following the memory alignment principle 3,16 is a multiple of the maximum data size 8, thus obtaining the structure struct2 memory allocation size of 16.


Struct3 memory size is calculated as follows:

  • variablea: offset Starts from 0. The storage position is[0]
  • variableb: offset Starts from 8 (8 is a multiple of data size 1). The storage location is[8 to 11]
  • variablec: offset Starts from 12 (12 is a multiple of data size 1). The storage location is[12]
  • variabled: offset starts from 13 (13 is not a multiple of 2 but starts from 14). The storage location is[14, 15]
  • variablee: offset Starts from 16 (16 is a multiple of the data size 4). The storage location is[16-19]
  • variablestr: Because it is a struct variable, following principle 2 when a struct is a member, the struct members are from within itAn integer multiple of the size of the largest elementStart saving. The largest variable in LGStruct1 is double, 8 bytes, 20 is not a multiple of 8, the complement starts at 24, and is stored at24 - [42]

We calculate that the actual sizeof the structure should be 43, according to the rule 3- the total sizeof the structure (sizeof output), must be an integer multiple of the largest internal member, is a multiple of the maximum data size STR (8), thus resulting in the structure structure memory allocation size is 48.