标签:多线程
作用
autorelease
的对象,在出了作用域之后,会被自动添加到最近创建的
自动释放池中release
消息,释放池中对象ARC
& MRC
程序中,同样有效自动释放池是什么时候创建的?什么时候销毁的?
创建,运行循环检测到事件并启动后,就会创建自动释放池
for (long i = 0; i < largeNumber; ++i) {
NSString *str = [NSString stringWithFormat:@"hello - %ld", i];
str = [str uppercaseString];
str = [str stringByAppendingString:@" - world"];
}
提问:以上代码是否有问题?如果有,如何解决?
解决方法:引入自动释放池
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"start");
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
[self answer1];
NSLog(@"外 %f", CFAbsoluteTimeGetCurrent() - start);
start = CFAbsoluteTimeGetCurrent();
[self answer2];
NSLog(@"内 %f", CFAbsoluteTimeGetCurrent() - start);
}
- (void)answer1 {
@autoreleasepool {
for (long i = 0; i < largeNumber; ++i) {
NSString *str = [NSString stringWithFormat:@"hello - %ld", i];
str = [str uppercaseString];
str = [str stringByAppendingString:@" - world"];
}
}
}
- (void)answer2 {
for (long i = 0; i < largeNumber; ++i) {
@autoreleasepool {
NSString *str = [NSString stringWithFormat:@"hello - %ld", i];
str = [str uppercaseString];
str = [str stringByAppendingString:@" - world"];
}
}
}
标签:多线程
原文地址:http://blog.csdn.net/jiahao8915/article/details/47688721