一、从一个引用计数的问题说起
题目详情:下面代码输出结果
NSString *string = [[NSString alloc]initWithString:@"hello world"];
NSLog(@"stringRetainCount:%ld",[string retainCount]);
想当然以为结果是1,但是实际结果是-1
YQSourceCodeTest[916:12587] stringRetainCount:-1
二、然后进一步探索,发现有如下发现:
NSString *str = [[NSString alloc]initWithFormat:@"123456789"];
NSLog(@"initWithFormat(%@)-stringClass(%@)-retainCount(%ld)",str,[str class],[str retainCount]);
NSString *str1 = [[NSString alloc]initWithFormat:@"1234567890"];
NSLog(@"initWithFormat(%@)-stringClass(%@)-retainCount:%ld",str1,[str1 class],[str1 retainCount]);
NSString *str2 = [[NSString alloc]initWithFormat:@"文"];
NSLog(@"initWithFormat(%@)-stringClass(%@)-retainCount:%ld",str2,[str2 class],[str2 retainCount]);
NSString *str3 = @"123456789";
NSLog(@"(%@)-stringClass(%@)-retainCount:%ld",str3,[str3 class],[str3 retainCount]);
NSString *str4 = @"1234567890";
NSLog(@"(%@)-stringClass(%@)-retainCount:%ld",str4,[str4 class],[str4 retainCount]);
NSString *str5 = [[NSString alloc]initWithString:@"123456789"];
NSLog(@"initWithString(%@)-stringClass(%@)-retainCount:%ld",str5,[str5 class],[str5 retainCount]);
NSString *str6 = [[NSString alloc]initWithString:@"1234567890"];
NSLog(@"initWithString(%@)-stringClass(%@)-retainCount:%ld",str6,[str6 class],[str6 retainCount]);
具体输出结果如下:
YQSourceCodeTest[1206:33010] initWithFormat(123456789)-stringClass(NSTaggedPointerString)-retainCount(-1)
YQSourceCodeTest[1206:33010] initWithFormat(1234567890)-stringClass(__NSCFString)-retainCount:1
YQSourceCodeTest[1206:33010] initWithFormat(文)-stringClass(__NSCFString)-retainCount:1
YQSourceCodeTest[1206:33010] (123456789)-stringClass(__NSCFConstantString)-retainCount:-1
YQSourceCodeTest[1206:33010] (1234567890)-stringClass(__NSCFConstantString)-retainCount:-1
YQSourceCodeTest[1206:33010] initWithString(123456789)-stringClass(__NSCFConstantString)-retainCount:-1
YQSourceCodeTest[1206:33010] initWithString(1234567890)-stringClass(__NSCFConstantString)-retainCount:-1
三、输出结果探究
1、class类型共有三种:
- NSTaggedPointerString :类型为标记指针(Tagged Pointer:是一种为了提高iOS和macOS性能而设计的内存优化技术),
- __NSCFString :运行时创建的一种 NSString 子类,数据存储在堆上
- __NSCFConstantString:字符串常量,数据存储在字符串常量区
2、retainCount
- initWithFormat:字符串小于等于9长度,retainCount = -1
- initWithFormat:字符串大于9/为中文,retainCount = 1
- 其他方法不受以上限制,始终retainCount = -1
四、其他延伸探究
1、编译器内存自动优化
NSString *str = @"abc";
NSLog(@"str:%p",str);
NSString *str1 = @"abc";
NSLog(@"str1:%p",str1);
NSString *str2 = [[NSString alloc]initWithString:@"abc"];
NSLog(@"str2:%p",str2);
NSString *str3 = [[NSString alloc]initWithFormat:@"abc"];
NSLog(@"str3:%p",str3);
前三个地址相同,输出结果如下:
YQSourceCodeTest[1559:52231] str:0x100004058
YQSourceCodeTest[1559:52231] str1:0x100004058
YQSourceCodeTest[1559:52231] str2:0x100004058
YQSourceCodeTest[1559:52231] str3:0xbe8e4a5ff910c311
2、copy 和 mutableCopy 对 retainCount影响
NSString *str = @"abc";
NSLog(@"str-retainCount:%ld",[str retainCount]);
NSString *str1 = [str copy];
NSLog(@"str-retainCount:%ld ; str1-retainCount:%ld",[str retainCount],[str1 retainCount]);
NSMutableString *strM = [str mutableCopy];
NSLog(@"str-retainCount:%ld ; strM-retainCount:%ld",[str retainCount],[strM retainCount]);
NSString *str2 = [[NSString alloc]initWithFormat:@"中文"];
NSLog(@"str2-retainCount:%ld",[str2 retainCount]);
NSString *str3 = [str2 copy];
NSLog(@"str2-retainCount:%ld ; str3-retainCount:%ld",[str2 retainCount],[str3 retainCount]);
NSMutableString *strM1 = [str2 mutableCopy];
NSLog(@"str2-retainCount:%ld ; strM1-retainCount:%ld",[str2 retainCount],[strM1 retainCount]);
针对NSTaggedPointerString 或者 __NSCFConstantString类型,copy和mutableCopy并不会增加引用技术,__NSCFString类型下,copy使引用计数加1,具体结果如下:
YQSourceCodeTest[1664:58763] str-retainCount:-1
YQSourceCodeTest[1664:58763] str-retainCount:-1 ; str1-retainCount:-1
YQSourceCodeTest[1664:58763] str-retainCount:-1 ; strM-retainCount:1
YQSourceCodeTest[1664:58763] str2-retainCount:1
YQSourceCodeTest[1664:58763] str2-retainCount:2 ; str3-retainCount:2
YQSourceCodeTest[1664:58763] str2-retainCount:2 ; strM1-retainCount:1