This chapter focuses on structure alignment, apple attribute rearrangement, and the 16-byte alignment algorithm
0x00 — Three ways to get memory size
Person *person = [Person alloc];
person.name = @"xxxxx";
person.nickName = @"ddd";
NSLog(@"%@ - %lu - %lu - %lu",
person,
sizeof(person),
class_getInstanceSize([Person class]),
malloc_size((__bridge const void *)(person)));
// 输出 8 - 40 - 48
Copy the code
There are three ways to get memory size:
sizeof(expression-or-type)
Tip 💡 : Three syntactic forms of sizeof:
int a = 10;
size_t a1 = sizeof(a); / / 4
size_t a2 = sizeof a; / / 4
size_t a3 = sizeof(int); / / 4
int *pa = &a;
size_t p1 = sizeof(pa); / / 8
size_t p2 = sizeof pa; / / 8
size_t p3 = sizeof(int *); / / 8
NSObject *obj = [NSObject alloc] ;
size_t o1 = sizeof(obj); / / 8
size_t o2 = sizeof obj; / / 8
size_t o3 = sizeof(NSObject*);/ / 8
Copy the code
Sizeof () is an operator, not a function; This returns the number of bytes of memory used by an object or type.
Basic data types Simple built-in data types such as int,char,double,float, etc. Their sizes are system dependent and their values vary from system to system.
In the case of structures, sizeof refers to the problem of byte alignment, which is taught by computer composition principles to help speed up the computer’s fetch speed, otherwise multi-word instruction cycles. Let abridged data types, such as short, with width of 2, be in low IQ divisible by 2; Having basic data types, int, etc. of width 4, at addresses divisible by 4, and so on, may require padding bytes between them, so that the sizeof the entire structure increases.
class_getInstanceSize
The RuntimeAPI is the actual memory size of the instance object created by the CLS class
size_t class_getInstanceSize(Class cls) { if (! cls) return 0; return cls->alignedInstanceSize(); } // Class's ivar size rounded up to a pointer-size boundary. uint32_t alignedInstanceSize() const { return word_align(unalignedInstanceSize()); } #ifdef __LP64__ # define WORD_SHIFT 3UL # define WORD_MASK 7UL # define WORD_BITS 64 #else # define WORD_SHIFT 2UL # define WORD_MASK 3UL # define WORD_BITS 32 #endif static inline uint32_t word_align(uint32_t x) { return (x + WORD_MASK) & ~WORD_MASK; }Copy the code
The class_getInstanceSize function retrieves an object size aligned with 8 bytes.
This function changes according to the properties inside the object; If you have no attributes and just inherit NSObject, the actual memory size of the instance object of the class is 8,
malloc_size
This function, in the #import
header, gets the size of the memory to which the pointer points
Malloc_size is the actual allocated memory space, and class_getInstanceSize is the actual occupied memory space.
Print verifiable according to code example 1 at the top of the article. This is done by the system. From the above analysis, it can be seen that the size of the actual occupation and the actual allocation is not the same. For example, the system allocates 32 bytes for your object based on certain alignment algorithms, but the object created by your Class only takes 16 bytes. That’s all it means.