Summary of iOS underlying principles –OC object nature (a) – digging gold
Summary of iOS underlying principles –OC object nature (ii) – digging gold
OC object classification: Instance, class, META -calss object ISA and Superclass – gold mining
Summary of basic principles of iOS — the essence of KVO/KVC — gold digging
Summary of iOS underlying principles – Using Runtime source code to analyze the low-level implementation of categories
.
Consider: if my Student has three member variables, how many bytes will it take? What is the output of class_getInstanceSize([Student class])? Malloc_size ((__bridge const void *) what is the output of stu?)
#import <malloc/malloc.h>
#import <OBJC/runtime.h>
/ / / > Student class
@interface Student: NSObject{
@public
int _no;
int _age;
int _gender;
}
///> The actual underlying structure structure
//struct Student_IMPL{
// Class isa,
// int _no,
// int _age;
// int _gender;
/ /}
@end
@implementation Student
@end
///> main
int main(int argc, char * argv[]) {
@autoreleasepool {
Student *stu = [[Student alloc]init];
stu->_no = 4;
stu->_age = 5;
stu->_gender = 1;
NSLog(@"%zd", class_getInstanceSize([Student class]));
NSLog(@"%zd", malloc_size((__bridge const void *)stu));
24 32 */ is displayed
}
return 0;
}
Copy the code
- The final output is:
- Class_getInstanceSize: 24
- Malloc_size: 32
First, why is malloc_size 32?
You can use Xcode’s own tools to see how much memory is allocated and used.
First we need to get the memory address of the STU object:
Xcode debugger LLDB – Nuggets
<Student: 0x600002746b60>
- Green area: The first 8 bits are essentially what we talked about last time. Objects are essentially structures: however, structures have Class ISA Pointers in them, and every object contains this Class ISA pointer. This pointer takes up eight bytes.
- Blue area: the member variable _no: is really 4 bytes because it is Int.
- Yellow area: the member variable _age: is really 4 bytes because it is Int.
- Gray area: the member variable _gender: is really 4 bytes because it is Int.
- White area: all areas are 00, which can be considered as open memory but not used.
From the above analysis, we can see that STU actually allocates 32 bytes of memory space in memory, which is the number of bytes printed by malloc_size() to open memory space.
Why is the output of class_getInstanceSize 24?
Class_getInstanceSize Gets the instance size of the class as the name implies. Isa occupies 8 + _NO: 4 + _age4 + _gender4
@interface Student: NSObject{
@public
Class isa; / / / > 8
int _no; / / / > 4
int _age; / / / > 4
int _gender; / / / > 4
} /// add up to 20,
Copy the code
The structure has a memory operation on it, which facilitates CPU access. One of the rules used in CO is memory operation on it: In order to ensure that the heaviest true memory used by the structure isa multiple of the largest variable, here we use 8 bytes for isa, so although only 20 bytes are actually used, 24 bytes are used to ensure that the memory rules for it.
If we have four member variables:
@interface Student: NSObject{
@public
Class isa; / / / > 8
int _no; / / / > 4
int _age; / / / > 4
int _gender; / / / > 4
int _height; / / / > 4
} /// add up to 24,
Copy the code
We're still using 24 bytes of memory, 32 bytes of memory.
If you are adding a member variable:
@interface Student: NSObject{
@public
Class isa; / / / > 8
int _no; / / / > 4
int _age; / / / > 4
int _gender; / / / > 4
int _height; / / / > 4
int _weight; / / / > 4
} // Add up to 28,
Copy the code
In order to ensure that the memory size is 32 bytes, open the same 32 bytes.
Malloc_size () also uses memory. The previous article explained why 16 bytes are allocated to the memory of the class, so 32 bytes are allocated to the stu class because of memory.
- The article is summarized from MJ teacher’s bottom video.