oc对象内存
1 影响对象大小的是什么? 属性,方法,成员变量?
@interface HFPerson : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSString *nick;
@property (nonatomic, assign) double height;
@end
HFPerson *p = [HFPerson alloc];
NSLog(@"%d --- %d --- %d", sizeof(p), class_getInstanceSize([p class]), malloc_size((__bridge void *)p))
结果:2021-06-16 11:21:47.461473+0800 StoreKitDemo[1141:352637] 8 --- 40 --- 48
当我们添加两个成员方法
- (void)getAge {
}
- (void)getName {
}
2021-06-16 11:27:45.272980+0800 StoreKitDemo[1153:354711] 8 --- 40 --- 48
@interface HFPerson : NSObject
{
NSString *_a;
NSInteger _b;
}
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSString *nick;
@property (nonatomic, assign) double height;
2021-06-16 11:29:33.099457+0800 StoreKitDemo[1162:355559] 8 --- 56 --- 64
有上面的示例可以总结出,对象的大小由成员变量和属性决定
2 探究类的本质
@interface HFStudent : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
@implementation HFStudent
@end
int main(int argc, char * argv[]) {
@autoreleasepool {
// return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
HFStudent *s = [[HFStudent alloc] init];
s.name = @"zhangsan";
s.age = 20;
}
}
通过clang编译成cpp文件
clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk main.m
通过main.cpp文件搜索HFStudent
typedef struct objc_object HFStudent;
struct HFStudent_IMPL {
struct NSObject_IMPL NSObject_IVARS;
NSString *_name;
NSInteger _age;
};
struct NSObject_IMPL {
Class isa;
};
这边可以看出类的本质就是结构体,结构体里面有三个属性,name和age是我们自己定义的,NSObject_IVARS是编译加进去的,NSObject_IVARS里面是isa指针
3 结构体内存计算
内存计算规则
1、数据成员对齐规则:结构体或者联合体的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储的起始位置要从该成员大小或者成员的子成员大小(只要该成员有子成员,比如数组,结构体等)的整数倍开始(比如int为4字节,则要从4的整数倍地址开始存储)。通俗来讲就是除了第一个位置,后面的每个成员存放的位置起始位置都要存放的那个成员(如果有子成员那就是子成员)的整数倍位置。
2、结构体作为成员:如果一个结构体里面有某些结构体成员,则结构体成员要从其内部最大元素大小的整数倍地址开始存储。
3、收尾工作:结构体的总大小,也就是sizeof的结果,必须是其内部最大成员的整数倍,不足的要补齐。
我们来看个示例
struct LGStruct1 {
double a;
char b;
int c;
short d;
}struct1;
struct LGStruct2 {
double a;
int b;
char c;
short d;
}struct2;
struct LGStruct3 {
double a;
int b;
char c;
short d;
int e;
struct LGStruct1 str;
}struct3;
NSLog(@"%d---%d --- %d", sizeof(struct1), sizeof(struct2), sizeof(struct3));
2021-06-16 13:26:43.661680+0800 StoreKitDemo[1260:385702] 24---16 --- 48
两个结构体明明内部成员都是一样只是顺序不一样,但是大小分别是24和16,为什么呢?
我们就来按照上面的规则来分析一下:
struct LGStruct1 {
double a;
char b;
int c;
short d;
}struct1;
struct LGStruct2 {
double a; [0 - 7]
int b; [ 8-12]
char c; [13]
short d; [14-15]
}struct2; 字节对齐8的整数倍16,所以是16字节
接下来看一个复杂一点的示例
struct LGStruct3 {
double a; [0 - 7]
int b; [ 8-12]
char c; [13]
short d; [14-15]
int e; [16-19]
struct LGStruct1 str; (20, 21, 22, 23, [24 - 48]
}struct3; 字节对齐,最大是8字节,所以刚好48
这样我们已经知道结构体的内存对齐和计算方式,接下来看看oc对象
@interface HFStudent : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSString *nick;
@property (nonatomic, assign) double height;
@property (nonatomic, assign) BOOL flag;
@property (nonatomic, assign) BOOL flag2;
@property (nonatomic, assign) short b;
@end
HFStudent *p = [[HFStudent alloc] init];
p.name = @"HF";
p.age = 20;
p.nick = @"Hongfa";
p.height = 180.0;
p.flag = YES;
p.flag2 = YES;
p.b = 100;
NSLog(@"%d --- %d --- %d", sizeof(p), class_getInstanceSize([p class]), malloc_size((__bridge void *)p));
2021-06-16 14:09:12.324686+0800 StoreKitDemo[1336:398318] 8 --- 48 --- 48
(lldb) p p
(HFStudent *) $0 = 0x000000028220e490
(lldb) x/6gx 0x000000028220e490
0x28220e490: 0x000001a1040e59a5 0x0000000000640101 -> (b, flag, flag2)
0x28220e4a0: 0x00000001040e40c8 0x0000000000000014
0x28220e4b0: 0x00000001040e4168 0x4066800000000000
从运行结果可以看出oc会对对象里面的属性排布进行优化