NSString *str1 = @"ABC";
NSString *str2 = [NSString stringWithFormat: @"aaa"];
NSString *str3 = [NSString stringWithString: @"ABC"];
NSString *str4 = [[NSString alloc]initWithFormat: @"aaa"];
NSString *str5 = [[NSString alloc]initWithString: @"ABC"];
NSLog(@"str1 = %@,%p,%lu",str1,str1,str1.retainCount);
NSLog(@"str2 = %@,%p,%lu",str2,str2,str2.retainCount);
NSLog(@"str3 = %@,%p,%lu",str3,str3,str3.retainCount);
NSLog(@"str4 = %@,%p,%lu",str4,str4,str4.retainCount);
NSLog(@"str5 = %@,%p,%lu",str5,str5,str5.retainCount);
运行结果:
2015-07-18 10:50:42.559 NSString内存管理[1260:64557] str1 = ABC,0x100001030,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str2 = aaa,0x61616135,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str3 = ABC,0x100001030,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str4 = aaa,0x61616135,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str5 = ABC,0x100001030,18446744073709551615
结论:
@"ABC" stringWithString initWithString 都在常量区
@"aaa" stringWithFormat initWithFormat 都在堆区
|
|