iOS NSString 内存管理

iOS中,NSString对象相对其他OC对象的内存管理是复杂的。
按照产生对象的isa,大致可以分为三种情况,如下:

1、__NSCFConstantString

字符串常量,是一种编译时常量,retainCount值很大,对其操作,不会引起引用计数变化。
这种对象存储在字符串常量区。

2、__NSCFString

__NSCFString对象是在运行时创建的一种NSString子类,创建后引用计数会加1。
这种对象存储在堆上。

3、NSTaggedPointerString

标签指针,是苹果在64位环境下对NSString、NSNumber等对象做的一些优化。

简单来讲,可以理解为把指针指向的内容直接放在了指针变量的内存地址中,因为在 64 位环境下指针变量的大小达到了 8 位足以容纳一些长度较小的内容。于是使用了标签指针这种方式来优化数据的存储方式。在运行时根据实际情况创建。

对于 NSString 对象,当字符串是由数字、英文字母组合且长度小于等于 9 的时候会自动成为 NSTaggedPointerString 类型。如果有中文或其他特殊符号,则会直接成为 __NSCFString 类型。

代码验证

环境:Xcode 10.2 、iOS 12.2

一、数字和字母组合(长度小于9)
/**
 测试拷贝
 */
- (void)testCopyAndMutableCopy {
    [self testString];
    [self testStringCopy1];
    [self testStringCopy2];
    [self testStringCopy3];
    [self testMutableStringCopy1];
    [self testMutableStringCopy2];
}

/**
 测试不可变字符串
 */
- (void)testString {
    NSString *testStr00 = @"abc123";
    NSLog(@"testStr00:%p ** %@ ** %@", testStr00, [testStr00 class], testStr00);
    
    printf("\n");
}

/**
 测试不可变字符串
 */
- (void)testStringCopy1 {
    NSString *testStr11 = @"abc123";
    NSLog(@"testStr11:%p ** %@ ** %@", testStr11, [testStr11 class], testStr11);
    
    id testStr21 = [testStr11 copy];
    NSLog(@"testStr21:%p ** %@ ** %@", testStr21, [testStr21 class], testStr21);
    
    id testStr31 = [testStr11 mutableCopy];
    NSLog(@"testStr31:%p ** %@ ** %@", testStr31, [testStr31 class], testStr31);
    
    printf("\n");
}

/**
 测试不可变字符串
 */
- (void)testStringCopy2 {
    NSString *testStr12 = [NSString stringWithFormat:@"abc123"];
    NSLog(@"testStr12:%p ** %@ ** %@", testStr12, [testStr12 class], testStr12);
    
    id testStr22 = [testStr12 copy];
    NSLog(@"testStr22:%p ** %@ ** %@", testStr22, [testStr22 class], testStr22);
    
    id testStr32 = [testStr12 mutableCopy];
    NSLog(@"testStr32:%p ** %@ ** %@", testStr32, [testStr32 class], testStr32);
    
    printf("\n");
}

/**
 测试不可变字符串
 */
- (void)testStringCopy3 {
    NSString *testStr13 = [[NSString alloc] initWithFormat:@"abc123"];
    NSLog(@"testStr13:%p ** %@ ** %@", testStr13, [testStr13 class], testStr13);
    
    id testStr23 = [testStr13 copy];
    NSLog(@"testStr23:%p ** %@ ** %@", testStr23, [testStr23 class], testStr23);
    
    id testStr33 = [testStr13 mutableCopy];
    NSLog(@"testStr33:%p ** %@ ** %@", testStr33, [testStr33 class], testStr33);
    
    printf("\n");
}

/**
 测试可变字符串
 */
- (void)testMutableStringCopy1 {
    NSMutableString *testMuStr14 = [[NSMutableString alloc] initWithString:@"abc123"];
    NSLog(@"testMuStr14:%p ** %@ ** %@",testMuStr14, [testMuStr14 class], testMuStr14);
    
    id testMuStr24 = [testMuStr14 copy];
    NSLog(@"testMuStr24:%p ** %@ ** %@",testMuStr24, [testMuStr24 class], testMuStr24);
    
    id testMuStr34 = [testMuStr14 mutableCopy];
    NSLog(@"testMuStr34:%p ** %@ ** %@",testMuStr34, [testMuStr34 class], testMuStr34);
    
    printf("\n");
}

/**
 测试可变字符串
 */
- (void)testMutableStringCopy2 {
    NSMutableString *testMuStr15 = [[NSMutableString alloc] initWithFormat:@"abc123"];
    NSLog(@"testMuStr15:%p ** %@ ** %@",testMuStr15, [testMuStr15 class], testMuStr15);
    
    id testMuStr25 = [testMuStr15 copy];
    NSLog(@"testMuStr25:%p ** %@ ** %@",testMuStr25, [testMuStr25 class], testMuStr25);
    
    id testMuStr35 = [testMuStr15 mutableCopy];
    NSLog(@"testMuStr35:%p ** %@ ** %@",testMuStr35, [testMuStr35 class], testMuStr35);
    
    printf("\n");
}

测试结果:

testStr00:0x1063cc0a0 ** __NSCFConstantString ** abc123

testStr11:0x1063cc0a0 ** __NSCFConstantString ** abc123
testStr21:0x1063cc0a0 ** __NSCFConstantString ** abc123
testStr31:0x600001bc65b0 ** __NSCFString ** abc123

testStr12:0x91472c787c5ba8b1 ** NSTaggedPointerString ** abc123
testStr22:0x91472c787c5ba8b1 ** NSTaggedPointerString ** abc123
testStr32:0x600001b8b540 ** __NSCFString ** abc123

testStr13:0x91472c787c5ba8b1 ** NSTaggedPointerString ** abc123
testStr23:0x91472c787c5ba8b1 ** NSTaggedPointerString ** abc123
testStr33:0x600001bc65b0 ** __NSCFString ** abc123

testMuStr14:0x600001b8b540 ** __NSCFString ** abc123
testMuStr24:0x91472c787c5ba8b1 ** NSTaggedPointerString ** abc123
testMuStr34:0x600001bc65b0 ** __NSCFString ** abc123

testMuStr15:0x600001b913e0 ** __NSCFString ** abc123
testMuStr25:0x91472c787c5ba8b1 ** NSTaggedPointerString ** abc123
testMuStr35:0x600001b90ed0 ** __NSCFString ** abc123

从上面测试结果来看,有以下结论:

1、testStr00testStr11testStr21具有相同的内存地址(0x1063cc0a0)。对__NSCFConstantString对象进行copy操作,不引起引用计数变化;进行mutableCopy操作,会拷贝对象,生成新的__NSCFString对象(内存地址会发生变化)。

2、testStr12testStr22testStr13testStr12具有相同的内存地址(0x91472c787c5ba8b1)。对NSTaggedPointerString对象进行copy操作,不引起引用计数变化;进行mutableCopy操作,会拷贝对象,生成新的__NSCFString对象(内存地址会发生变化)。

3、比较有意思的是testStr31testStr33具有相同的内存地址(0x600001bc65b0),但是与testStr32是不一样的(0x600001b8b540)。推测多次对同一字符串常量进行mutableCopy操作,生成新的__NSCFString对象,有可能同一对象、也有可能不是同一对象。

4、对__NSCFString对象进行copy操作,可能会生成NSTaggedPointerString对象;也可能会生成__NSCFString对象。(参照下面带中文的字符串测试结果)
进行mutableCopy操作,会拷贝对象,生成新的__NSCFString对象(内存地址会发生变化)。

二、带中文组合
/**
 测试拷贝
 */
- (void)testCopyAndMutableCopy {
    [self testString];
    [self testStringCopy1];
    [self testStringCopy2];
    [self testStringCopy3];
    [self testMutableStringCopy1];
    [self testMutableStringCopy2];
}

/**
 测试不可变字符串
 */
- (void)testString {
    NSString *testStr00 = @"你好abc";
    NSLog(@"testStr00:%p ** %@ ** %@", testStr00, [testStr00 class], testStr00);
    
    printf("\n");
}

/**
 测试不可变字符串
 */
- (void)testStringCopy1 {
    NSString *testStr11 = @"你好abc";
    NSLog(@"testStr11:%p ** %@ ** %@", testStr11, [testStr11 class], testStr11);
    
    id testStr21 = [testStr11 copy];
    NSLog(@"testStr21:%p ** %@ ** %@", testStr21, [testStr21 class], testStr21);
    
    id testStr31 = [testStr11 mutableCopy];
    NSLog(@"testStr31:%p ** %@ ** %@", testStr31, [testStr31 class], testStr31);
    
    printf("\n");
}

/**
 测试不可变字符串
 */
- (void)testStringCopy2 {
    NSString *testStr12 = [NSString stringWithFormat:@"你好abc"];
    NSLog(@"testStr12:%p ** %@ ** %@", testStr12, [testStr12 class], testStr12);
    
    id testStr22 = [testStr12 copy];
    NSLog(@"testStr22:%p ** %@ ** %@", testStr22, [testStr22 class], testStr22);
    
    id testStr32 = [testStr12 mutableCopy];
    NSLog(@"testStr32:%p ** %@ ** %@", testStr32, [testStr32 class], testStr32);
    
    printf("\n");
}

/**
 测试不可变字符串
 */
- (void)testStringCopy3 {
    NSString *testStr13 = [[NSString alloc] initWithFormat:@"你好abc"];
    NSLog(@"testStr13:%p ** %@ ** %@", testStr13, [testStr13 class], testStr13);
    
    id testStr23 = [testStr13 copy];
    NSLog(@"testStr23:%p ** %@ ** %@", testStr23, [testStr23 class], testStr23);
    
    id testStr33 = [testStr13 mutableCopy];
    NSLog(@"testStr33:%p ** %@ ** %@", testStr33, [testStr33 class], testStr33);
    
    printf("\n");
}

/**
 测试可变字符串
 */
- (void)testMutableStringCopy1 {
    NSMutableString *testMuStr14 = [[NSMutableString alloc] initWithString:@"你好abc"];
    NSLog(@"testMuStr14:%p ** %@ ** %@",testMuStr14, [testMuStr14 class], testMuStr14);
    
    id testMuStr24 = [testMuStr14 copy];
    NSLog(@"testMuStr24:%p ** %@ ** %@",testMuStr24, [testMuStr24 class], testMuStr24);
    
    id testMuStr34 = [testMuStr14 mutableCopy];
    NSLog(@"testMuStr34:%p ** %@ ** %@",testMuStr34, [testMuStr34 class], testMuStr34);
    
    printf("\n");
}

/**
 测试可变字符串
 */
- (void)testMutableStringCopy2 {
    NSMutableString *testMuStr15 = [[NSMutableString alloc] initWithFormat:@"你好abc"];
    NSLog(@"testMuStr15:%p ** %@ ** %@",testMuStr15, [testMuStr15 class], testMuStr15);
    
    id testMuStr25 = [testMuStr15 copy];
    NSLog(@"testMuStr25:%p ** %@ ** %@",testMuStr25, [testMuStr25 class], testMuStr25);
    
    id testMuStr35 = [testMuStr15 mutableCopy];
    NSLog(@"testMuStr35:%p ** %@ ** %@",testMuStr35, [testMuStr35 class], testMuStr35);
    
    printf("\n");
}

测试结果:

testStr00:0x108a610a0 ** __NSCFConstantString ** 你好abc

testStr11:0x108a610a0 ** __NSCFConstantString ** 你好abc
testStr21:0x108a610a0 ** __NSCFConstantString ** 你好abc
testStr31:0x6000007f6e50 ** __NSCFString ** 你好abc

testStr12:0x6000007f6d60 ** __NSCFString ** 你好abc
testStr22:0x6000007f6d60 ** __NSCFString ** 你好abc
testStr32:0x600000790bd0 ** __NSCFString ** 你好abc

testStr13:0x6000007f6d30 ** __NSCFString ** 你好abc
testStr23:0x6000007f6d30 ** __NSCFString ** 你好abc
testStr33:0x600000790bd0 ** __NSCFString ** 你好abc

testMuStr14:0x6000007c9350 ** __NSCFString ** 你好abc
testMuStr24:0x6000007f6d30 ** __NSCFString ** 你好abc
testMuStr34:0x600000790bd0 ** __NSCFString ** 你好abc

testMuStr15:0x6000007c9350 ** __NSCFString ** 你好abc
testMuStr25:0x6000007c9650 ** __NSCFString ** 你好abc
testMuStr35:0x600000790bd0 ** __NSCFString ** 你好abc

你可能感兴趣的:(iOS NSString 内存管理)