IOS Basics Exploration ii – Object Principles (Middle)
A, bytes
A Byte is a unit of measurement used in computer information technology to measure storage capacity. It also represents data types and language characters in some computer programming languages. Byte is an unsigned type from 0 to 255, so it cannot represent a negative number.
Memory size (bytes) for C++ base data types
Second, the influence factors of object memory size
Preparation: Define a class: LGPerson and get the initial memory size from the Runtime method: class_getInstanceSize()
1, properties,
Add the attribute name attribute to the Person class and get the memory size of the class after adding the attribute
Add attributes:
Memory size:
The memory size of the Person class increased from 8 bytes to 16 bytes after the addition of attributes, indicating that attributes have an impact on the memory size of the class.
2. Member variables
Add the attribute member variable to the Person class and get the memory size of the class after adding the attribute
Add a member variable:
Memory size:
After adding member variables, the memory size of the Person class increases from 8 bytes to 16 bytes, indicating that member variables have an impact on the memory size of the class.
3, methods,
Add methods to the Person class and get the memory size of the class after adding attributes
Add method:
Memory size:
The memory size of the Person class is 8 bytes after the method is added, which means that the method has no effect on the memory size of the class.
4. Class methods
Add a generic class method to the Person class and get the memory size of the class after adding attributes
Add class methods:
Memory size:
The memory size of the Person class is 8 bytes after the addition of class methods, indicating that class methods have no effect on the memory size of the class.
Summary: Attributes and member variables affect the memory size of a class. Since attributes are compiled as member variables, it is also possible to say that member variables have an impact on the size of a class’s memory
Three, memory alignment principle
Data member alignment: The first data member of a struct or union is stored at offset 0, and the starting position of each data member is changed from the size of the member or the size of the member’s children (as long as the member has children, such as an array). Structure, etc.) (e.g., if int is 4 bytes, start with the address that is a multiple of 4)
Struct alignment: If a struct has some struct members, the struct members are stored from an integer multiple of the size of the largest element inside them. (struct b={char, int, double})
The total sizeOf the structure, the result of sizeOf(), must be an integer multiple of the largest member within the structure.
1. Structure operation:
Struct1 and Struct2 elements are the same, but the memory size is different. Based on the memory rule calculation, to see whether the size of the two structures are the same.
Struct1 calculation:
Struct2 calculation:
According to the above structure calculation, it is found that: the same type of elements, different sorting structure, memory size is not the same.
Do you need to prioritize in your daily development? It’s not necessary, the compiler will automatically optimize, adjust the position.
2. Class calculation:
Add attributes to the Person class and get the memory size of the class after the attributes are added
Add attributes:
Memory size:
sizeof
: is an operator that retrieves the size of a type (int, size_t, structure, pointer variable, etc.) at compile timeclass_getInstanceSize
: is a function that gets the actual memory used by the created object plus all instance variables at runtimemalloc_size
: The amount of space opened in the heap, the amount of space requested from the system
Sizeof gets a pointer to the Person object according to the structure’s rules: the sizeof method is 8. The memory size required for the class_getInstanceSize member variable is 32 bytes, because the class contains the hidden variable ISA pointer, with a pointer size of 8 bytes and all memory sizes of 40 bytes. But why are 48 bytes of memory requested from the system?
malloc_size
1, segregated_size_to_fit
static MALLOC_INLINE size_t
segregated_size_to_fit(nanozone_t *nanozone, size_t size, size_t *pKey)
{
size_t k, slot_bytes;
if (0 == size) {
size = NANO_REGIME_QUANTA_SIZE; // Historical behavior
}
// #define NANO_REGIME_QUANTA_SIZE (1 << SHIFT_NANO_QUANTUM) // 16
// #define SHIFT_NANO_QUANTUM 4
k = (size + NANO_REGIME_QUANTA_SIZE - 1) >> SHIFT_NANO_QUANTUM;
// #define SHIFT_NANO_QUANTUM 4
slot_bytes = k << SHIFT_NANO_QUANTUM;
*pKey = k - 1;
return slot_bytes;
}
Copy the code
Segregated_size_to_fit is the core method for malloc_size to calculate the size of slot_bytes:
All struct internal elements (member variables) are 8-byte aligned, while the heap size is 16-byte aligned.