可把 main.m 文件转换c++文件查看里面的内部实现
clang -rewrite-objc main.m -o main.cpp
iOS一些架构类型
模拟器i386
32bit(armv7)
64bit (arm64 )
一些内存信息
NSObject 的内存大小
16字节。
这是内部实现
struct NSObject_IMPL {
Class isa;
};
isa指针占8个字节,然后由于系统默认给堆对象的内存大小是16的倍数,所以实际给了16个字节的大小。可以通过下面代码查看内存信息
#import <Foundation/Foundation.h>
#import "objc/runtime.h"
#import "malloc/malloc.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSObject *obj = [[NSObject alloc] init];
// 8
NSLog(@"class_getInstanceSize %zd", class_getInstanceSize([NSObject class])); // 实际实例变量占用的对齐内存大小
// 16
NSLog(@"malloc_size %zd", malloc_size((__bridge const void *)obj)); // 系统给的内存大小
}
return 0;
}
一些例子
定义一个 Student class
@interface Student : NSObject {
@public
int _age;
int _no;
}
// int 4个字节
// class_getInstanceSize = 16
// malloc_size = 16
Student 类的内部数据类型
struct Student_IMPL {
struct NSObject_IMPL NSObject_IVARS;
NSInteger _age;
NSInteger _no;
};
NSInteger 的数据类型
@interface Student : NSObject {
@public
NSInteger _age;
NSInteger _no;
}
// NSInteger 8个字节
// class_getInstanceSize = 24
// malloc_size = 32