preface

Through the above exploration of alloc process, the call process of alloc method has been clear, but how much memory space should be opened up to create objects is still a question, with such a question, start the following exploration

1. First create an XQPerson class without declaring any properties or methods

2. Declare attributes only

3. Declare only member variables

4. Declare only methods

5. Declare member variables and methods

Conclusion: 1. When no attributes or methods are declared, the memory size of the power object of the class is 8 bytes of isa pointer. After adding member variables or attributes, the memory size of the isa pointer is 8 bytes plus the sum of the bytes of each member variable and then aligned with 8 bytes.

2. Only member variables affect the object’s memory space, methods do not affect the object’s memory space (instance methods in the class method_list, class methods in the metaclass method_list)

Byte alignment

As mentioned above, the object takes up 8 bytes of memory alignment, we find the object memory 8-byte alignment code as follows:

UnalignedInstanceSize (x + WORD_MASK) & ~WORD_MASK (x + WORD_MASK) & ~WORD_MASK (x + WORD_MASK) So the expression is:

(20 + 7) &~ 7 = 0001 0100 + 0000 0111&1111 1000 = 0001 1011&1111 1000 = 0001 1000 = 24Copy the code

2.16-byte alignment

(x + size_t(15)) & ~size_t(15), x is the argument, the type is size_t, represents the current object to declare the sum of the bytes of each member variable is 8-byte aligned, suppose x = 24 then the expression is:

(24 + 15) &~ 15 = 0001 1000 + 0000 1111&1111 0000 = 0010 0111&1111 0000 = 0010 0000 = 32Copy the code

The reason for memory alignment is as follows: for the object of the following class, ISA occupies 8 bytes, and each member variable occupies 8, 1, and 4 bytes respectively, occupying a total of 21 bytes. If the data is read according to figure 1, the length of read bytes needs to be changed several times.

@interface XQPerson : NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,assign)char sex;
@property(nonatomic,assign)int height;
@end
Copy the code

Internal alignment of the structure

Memory alignment principles:

  • Data member alignment rules: Structure (struct) or group (union) data members, members of the first data in the offset to zero, after each data member of the storage location from the members of the member or members of the size of size (as long as the son of the members members, such as array structure, etc.) of integer times to start (such as int is 4 bytes, It starts with an integer multiple of 4.

  • Struct b (char, int, double); struct B (char, int, double); struct B (char, int, double);

  • The total sizeof the structure, the result of sizeof, must be an integer multiple of the sizeof its largest internal member, with any gaps to be filled in.

Object memory alignment

We know that the members of objects are 8-byte aligned, so are objects also 8-byte aligned between objects?

As you can see from the print below, objects are aligned in hexadecimal with each other

When did the system align in hexadecimal?

In the instanceSize method, 16-byte alignment is performed when size is cached.

But what about 16-byte alignment when there’s no caching?

Return cache.fastInstancesize (extraBytes); When calloc is used with size 24, the object is aligned in hexadecimal or 8 bytes. When calloc is used with size 24, the object is aligned in hexadecimal or 8 bytes.

As you can see from the print above, the object is still 16-byte aligned at this point. So when did the 16-byte alignment take place?

Jump to the calloc method and find the source of this method in malloc

We found the libmalloc source code

Execute calloc(1,24) through the following process to find the hexadecimal alignment method as follows

aftercallocThe output is as follows:

Malloc flowchart:

Conclusion:

  • Only member variables affect the memory of the object
  • Object member variables, 8 bytes aligned, add less than 8 bytes optimized together, insufficient complement 0.
  • Object to object, 16 bytes aligned.