NSString的内存优化
OC中的NSString不论是在编译时还是在运行时都做了很多的优化,并不同于普通的对象,它是一个非常复杂的存在。
首先定义几个宏定义方便打印观察结果:
#if __has_feature(objc_arc)
#define Obj_RetainCount(obj) \
CFGetRetainCount((__bridge CFTypeRef)(obj))
#else
#define Obj_RetainCount(obj) \
DebugLog(@"%lu",[obj retainCount]);
#endif
#define XFLog(_var) NSLog(@"%@ : class = %@ p = %p retainCount = %d",@#_var,NSStringFromClass([_var class]),_var,Obj_RetainCount(_var));
测试代码如下:
NSString *a = @"str";
NSString *b = [[NSString alloc]init];
NSString *c = [[NSString alloc]initWithString:@"str"];
NSString *d = [[NSString alloc]initWithFormat:@"str"];
NSString *e = [NSString stringWithFormat:@"str"];
NSString *f = [NSString stringWithFormat:@"123456789"];
NSString *g = [NSString stringWithFormat:@"1234567890"];
XFLog(a);
XFLog(b);
XFLog(c);
XFLog(d);
XFLog(e);
XFLog(f);
XFLog(g);
打印结果:
a : class = __NSCFConstantString p = 0x10eb5d090 retainCount = -1
b : class = __NSCFConstantString p = 0x10ee90470 retainCount = -1
c : class = __NSCFConstantString p = 0x10eb5d090 retainCount = -1
d : class = NSTaggedPointerString p = 0xa000000007274733 retainCount = -1
e : class = NSTaggedPointerString p = 0xa000000007274733 retainCount = -1
f : class = NSTaggedPointerString p = 0xa1ea1f72bb30ab19 retainCount = -1
g : class = __NSCFString p = 0x7ff449c2a5c0 retainCount = 2
可以看到,不同方式创建的字符串类型不同,引用计数也有所区别,并不是我们常规理解的对象初始化后引用计数为1。创建的字符串有3种类型
- __NSCFConstantString
- __NSCFString
- NSTaggedPointerString
造成这种结果的原因是由于OC对字符串做的内存优化。
__NSCFConstantString
对变量类型名上就可以看出,这种类型的字符串是常量字符串。该类型的字符串以字面量的方式创建,保存在字符串常量区,是在编译时创建的。例如:
NSString *a = @"good afternoon!";
NSString *b = [[NSString alloc]initWithString:@"good afternoon!"];
对于 initWithString 实例方法以及 stringWithString 类方法,编译器会给出redundant警告,原因是该方法创建字符串等同于直接复制字符串字面量。
当创建的字符串变量值在常量区已经存在时,会指向那个字符串,这是编译器做的优化。
由于是常量,因此其内存管理并不同于对象的内存管理,引用计数用整形格式打出来始终为-1。
__NSCFString
__NSCFString 表示对象类型的字符串,在运行时创建,保存在堆区,初始引用计数为1,其内存管理方式就是对象的内存管理方式。该种类型字符串通过format方式创建,并且字符串内容仅由数字、字母和常规ASCII字符构成,且其长度不能太小,否则创建的是NSTaggedPointerString类型。
NSString *d = [[NSString alloc]initWithFormat:@"我是对象"];
NSString *e = [NSString stringWithFormat:@"1234567890"]; //__NSCFString
NSTaggedPointerString
NSTaggedPointerString 类型的字符串是对__NSCFString类型的一种优化,在运行时创建字符串时,会对字符串内容及长度作判断,若内容由ASCII字符构成且长度较小(具体要多小暂时不太清楚),这时候创建的字符串类型就是 NSTaggedPointerString (标签指针字符串),字符串直接存储在指针的内容中(这个不太理解)。NSTaggedPointerString 类型的字符串引用计数同样为-1,不适用对象的内存管理策略。
NSString *e = [NSString stringWithFormat:@"123456789"]; //NSTaggedPointerString
而上面以同样的方式创建的1234567890字符串却为 __NSCFString 类型。
探究NSDictionary NSArray NSValue NSNumber是否也做了优化
NSDictionary测试代码如下:
NSDictionary *d1 = @{@"aa":@"11",@"bb":@"22"};
NSDictionary *d2 = [[NSDictionary alloc]init];
NSDictionary *d3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"a",@"1", nil];
NSDictionary *d4 = [NSDictionary dictionaryWithObjectsAndKeys:@"b",@"2", nil];
XFLog(d1);
XFLog(d2);
XFLog(d3);
XFLog(d4);
打印结果:
d1 : class = __NSDictionaryI p = 0x7fddbad1e9d0 retainCount = 1
d2 : class = __NSDictionary0 p = 0x7fddbae00dd0 retainCount = -1
d3 : class = __NSDictionaryI p = 0x7fddbad044f0 retainCount = 1
d4 : class = __NSDictionaryI p = 0x7fddbadbd8a0 retainCount = 2
NSArray测试代码如下:
NSArray *a1 = @[@"1",@"2"];
NSArray *a2 = [[NSArray alloc]init];
NSArray *a3 = [[NSArray alloc]initWithObjects:@"1", nil];
NSArray *a4 = [[NSArray alloc]initWithArray:@[@"a",@"b"]];
NSArray *a5 = [NSArray arrayWithObjects:@"m",@"n", nil];
XFLog(a1);
XFLog(a2);
XFLog(a3);
XFLog(a4);
XFLog(a5);
打印结果:
a1 : class = __NSArrayI p = 0x7fddbae16970 retainCount = 1
a2 : class = __NSArray0 p = 0x7fddbae00ce0 retainCount = -1
a3 : class = __NSArrayI p = 0x7fddbae23ea0 retainCount = 1
a4 : class = __NSArrayI p = 0x7fddbae0ab30 retainCount = 1
a5 : class = __NSArrayI p = 0x7fddbae0ab10 retainCount = 2
NSValue测试代码如下:
NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(10, 10)];
CGPoint point = CGPointMake(20, 20);
NSValue *v2 = [[NSValue alloc]initWithBytes:&point objCType:@encode(CGPoint)];
XFLog(v1);
XFLog(v2);
打印结果:
v1 : class = NSConcreteValue p = 0x7fddbae153e0 retainCount = 2
v2 : class = NSConcreteValue p = 0x7fddbae21fa0 retainCount = 1
NSNumber测试代码如下:
NSNumber *n1 = [NSNumber numberWithInt:123456789];
NSNumber *n2 = [NSNumber numberWithInt:1234567890];
int i = 10;
NSNumber *n3 = [[NSNumber alloc]initWithBytes:&i objCType:@encode(int)];
XFLog(n1);
XFLog(n2);
XFLog(n3);
打印结果:
n1 : class = __NSCFNumber p = 0xb000000075bcd152 retainCount = -1
n2 : class = __NSCFNumber p = 0xb000000499602d22 retainCount = -1
n3 : class = NSConcreteValue p = 0x7fddbadb4df0 retainCount = 1
结论:
对于NSDictionary、NSArray以及NSValue实例,除了空字典和空字符串,其余实例都遵循对象的内存管理策略。
NSNumber类创建的对于普通数据类型的封装的实例,其内存管理同样做了优化,不遵循对象的内存管理策略。
另外可以看到,有些新创建的对象引用计数为1,有些为2。这是因为用类方法创建的实例,系统自动将其置入自动释放池。
mutable variable
测试代码如下:
//NSMutableString
NSMutableString *ms1 = [[NSMutableString alloc]init];
NSMutableString *ms2 = [[NSMutableString alloc]initWithString:@"str"];
NSMutableString *ms3 = [[NSMutableString alloc]initWithFormat:@"str"];
NSMutableString *ms4 = [NSMutableString stringWithFormat:@"str"];
NSMutableString *ms5 = [NSMutableString stringWithFormat:@"123456789"];
NSMutableString *ms6 = [NSMutableString stringWithFormat:@"1234567890"];
XFLog(ms1);
XFLog(ms2);
XFLog(ms3);
XFLog(ms4);
XFLog(ms5);
XFLog(ms6);
//NSMutableDictionary
NSMutableDictionary *md1 = [[NSMutableDictionary alloc]init];
NSMutableDictionary *md2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"a",@"1", nil];
NSMutableDictionary *md3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"b",@"2", nil];
XFLog(md1);
XFLog(md2);
XFLog(md3);
//NSMutableArray
NSMutableArray *ma1 = [[NSMutableArray alloc]init];
NSMutableArray *ma2 = [[NSMutableArray alloc]initWithObjects:@"1", nil];
NSMutableArray *ma3 = [[NSMutableArray alloc]initWithArray:@[@"a",@"b"]];
NSMutableArray *ma4 = [NSMutableArray arrayWithObjects:@"m",@"n", nil];
XFLog(ma1);
XFLog(ma2);
XFLog(ma3);
XFLog(ma4);
测试结果如下:
ms1 : class = __NSCFString p = 0x7fd31070e6b0 retainCount = 1
ms2 : class = __NSCFString p = 0x7fd310716d40 retainCount = 1
ms3 : class = __NSCFString p = 0x7fd310719910 retainCount = 1
ms4 : class = __NSCFString p = 0x7fd31071b610 retainCount = 2
ms5 : class = __NSCFString p = 0x7fd31071bc40 retainCount = 2
ms6 : class = __NSCFString p = 0x7fd310715350 retainCount = 2
md1 : class = __NSDictionaryM p = 0x7fd31071be00 retainCount = 1
md2 : class = __NSDictionaryM p = 0x7fd31070c270 retainCount = 1
md3 : class = __NSDictionaryM p = 0x7fd310717960 retainCount = 2
ma1 : class = __NSArrayM p = 0x7fd31060d860 retainCount = 1
ma2 : class = __NSArrayM p = 0x7fd310605730 retainCount = 1
ma3 : class = __NSArrayM p = 0x7fd310607f80 retainCount = 1
ma4 : class = __NSArrayM p = 0x7fd310606dc0 retainCount = 2
结论:
可变变量实例均以对象的形式保存在堆中。
————————————————
转载文章:NSString内存管理