一、iOS程序的内存布局
在iOS程序的内存布局中从低到高分保留区、代码段、数据段、堆区、栈区、内核区,具体存储信息如下
- 代码段:编译之后的代码
- 数据段
- 字符串常量(NSString *str = @"123")
- 已初始化的数据:已初始化的全局变量、静态变量等
- 未初始化的数据:未初始化的全局变量、静态变量等
- 堆区:通过alloc、malloc、calloc等动态分配的空间,分配的内存空间地址越来越大
- 栈区:函数调用开销,比如局部变量。分配的内存空间地址越来越小 iOS程序内存布局.png
二、Tagged Pointer
- 从64位开始,iOS引入了Tagged Pointer技术,用于优化NSNumber、NSDate、NSString等小对象的存储
- 在没有使用Tagged Pointer之前,NSNumber等对象需要动态分配内存、维护引用计数等,NSNumber指针存储的是堆中NSNumber对象的地址值,
- 使用Tagged Pointer之后,NSNumber指针里面存储的数据变成了:Tag + Data,也就是将数据直接存储在了指针中
- 当指针不够存储数据时,才会使用动态分配内存的方式来存储数据
- objc_msgSend能识别Tagged Pointer,比如NSNumber的intValue方法,直接从指针提取数据,节省了以前的调用开销
- 如何判断一个指针是否为Tagged Pointer
- iOS平台,最高有效位是1(第64位)
- Mac平台,最低有效位是1
源码
static inline bool
_objc_isTaggedPointer(const void * _Nullable ptr)
{
return ((uintptr_t)ptr & _OBJC_TAG_MASK) == _OBJC_TAG_MASK;
}
#if TARGET_OS_OSX && __x86_64__ //Mac平台
// 64-bit Mac - tag bit is LSB
# define OBJC_MSB_TAGGED_POINTERS 0
#else //iOS平台
// Everything else - tag bit is MSB
# define OBJC_MSB_TAGGED_POINTERS 1
#endif
#define _OBJC_TAG_INDEX_MASK 0x7
// array slot includes the tag bit itself
#define _OBJC_TAG_SLOT_COUNT 16
#define _OBJC_TAG_SLOT_MASK 0xf
#define _OBJC_TAG_EXT_INDEX_MASK 0xff
// array slot has no extra bits
#define _OBJC_TAG_EXT_SLOT_COUNT 256
#define _OBJC_TAG_EXT_SLOT_MASK 0xff
#if OBJC_MSB_TAGGED_POINTERS
# define _OBJC_TAG_MASK (1UL<<63)
# define _OBJC_TAG_INDEX_SHIFT 60
# define _OBJC_TAG_SLOT_SHIFT 60
# define _OBJC_TAG_PAYLOAD_LSHIFT 4
# define _OBJC_TAG_PAYLOAD_RSHIFT 4
# define _OBJC_TAG_EXT_MASK (0xfUL<<60)
# define _OBJC_TAG_EXT_INDEX_SHIFT 52
# define _OBJC_TAG_EXT_SLOT_SHIFT 52
# define _OBJC_TAG_EXT_PAYLOAD_LSHIFT 12
# define _OBJC_TAG_EXT_PAYLOAD_RSHIFT 12
#else
# define _OBJC_TAG_MASK 1UL
# define _OBJC_TAG_INDEX_SHIFT 1
# define _OBJC_TAG_SLOT_SHIFT 0
# define _OBJC_TAG_PAYLOAD_LSHIFT 0
# define _OBJC_TAG_PAYLOAD_RSHIFT 4
# define _OBJC_TAG_EXT_MASK 0xfUL
# define _OBJC_TAG_EXT_INDEX_SHIFT 4
# define _OBJC_TAG_EXT_SLOT_SHIFT 4
# define _OBJC_TAG_EXT_PAYLOAD_LSHIFT 0
# define _OBJC_TAG_EXT_PAYLOAD_RSHIFT 12
#endif
Tagged Pointer 和对象存储的区别
- 测试代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
bool isTaggedPointer(id pointer) {
return ((long)((__bridge void *)pointer) & 1<<63) == 1<<63;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSNumber *num1 = @4;
NSNumber *num2 = @5;
NSNumber *num3 = @(0xFFFF);
NSLog(@"%p, %p, %p", num1, num2, num3);
NSLog(@"%d, %d, %d", isTaggedPointer(num2), isTaggedPointer(num2), isTaggedPointer(num2));
}
@end
- 打印输出
2018-07-27 11:00:16.450072+0800 01-tagged Pointer[44145:2749864] 0xb000000000000042, 0xb000000000000052, 0xb0000000000ffff2
2018-07-27 11:00:16.450268+0800 01-tagged Pointer[44145:2749864] 1, 1, 1
- 分析:num1、num2、num3的最高位是1,所以num1、num2、num3都是Tagged Pointer
TaggedPointer和对象在使用的区别
- 测试代码
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, copy) NSString *str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self taggedPointer_test];
}
/**
对象赋值
*/
- (void)objc_test {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
for (int i = 0; i < 1000; i++) {
dispatch_async(queue, ^{
NSLog(@"%s", __func__);
self.str = [NSString stringWithFormat:@"qwertyuiopasdfghjkl"];
});
}
}
/**
TaggedPointer赋值
*/
- (void)taggedPointer_test {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
for (int i = 0; i < 1000; i++) {
dispatch_async(queue, ^{
self.str = [NSString stringWithFormat:@"qwer"];
});
}
}
@end
- 分析
- 调用objc_test方法程序会崩溃,而调用taggedPointer_test方法程序不会崩溃
- objc_test方法和taggedPointer_test方法的区别只是赋值的不同
- 调用objc_test方法崩溃,是因为在给str赋值时,在set方法的底层会先对[_str release],再对_str = str,由于是多线程这样就会造成同时调用[_str release],造成崩溃
- 所以需要给str赋值这句代码进行加锁来解决这个崩溃的问题
网友评论