struct NSObject_IMPL {
Class isa; // 指针占8个字节
};
NSObject *obj = [[NSObject alloc] init];
NSLog(@"instanceSize---%ld",class_getInstanceSize([NSObject class]));
// NSObject类的实例对象成员变量所占用的内存大小
NSLog(@"mallocSize---%ld",malloc_size((__bridge const void *)(obj)));
// obj指针所指向的内存大小
1、class_getInstanceSize调用栈

class_getInstanceSize调用栈01

class_getInstanceSize调用栈02
2、malloc_size调用栈

malloc_size调用栈1

malloc_size调用栈2

malloc_size调用栈3

malloc_size调用栈4
3、Student的内存大小
@interface Student : NSObject
{
@public
int _no; //4
int _age; //4
double _weight; //8
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *student = [[Student alloc] init];
student->_age = 4;
student->_weight = 5;
student->_no = 8;
NSLog(@"%ld",class_getInstanceSize([Student class]));
NSLog(@"%ld",malloc_size((__bridge const void *)(student)));
//由于有内存对齐的概念,分配内存大小为16的倍数,结构体的内存也有对齐的概念,是8的倍数
}
return 0;
}

内存对齐
网友评论