NSObject* obj = [[NSObject alloc] init];
Student* student = [[Student alloc]init];
NSString* str = @"hello";
student->_no = 25;
student->_age = 30;
NSLog(@"%zd",class_getInstanceSize([NSObject class]));
NSLog(@"%zd",malloc_size((__bridge const void *)(obj)));
NSLog(@"%zd",class_getInstanceSize([Student class]));
NSLog(@"%zd",malloc_size(CFBridgingRetain(student)));
NSLog(@"%zd",class_getInstanceSize([NSString class]));
NSLog(@"%zd",malloc_size(CFBridgingRetain(str)));
struct Student_IMPL * stu = (__bridge struct Student_IMPL *)student;
NSLog(@"no is %d, age is %d",stu->_no,stu->_age);
1. lldb命令
image.png
image.png
image.png
image.png
2. 获取NSObject对象占用内存大小
#import <objc/runtime.h>
NSLog(@"%zd",class_getInstanceSize([NSObject class])); //8
#import <malloc/malloc.h>
NSLog(@"%zd",malloc_size((__bridge const void *)(obj))); //16
3. 内存对齐
-
结构体的最终大小必须是最大成员大小的倍数,iOS都是分配16个字节的倍数
网友评论