//
// ViewController.m
// 10-自动释放池
//
// Created by gzxzmac on 16/2/16.
// Copyright © 2016年 gzxzmac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 使用weak 修饰没有报错或者警告(类方法)
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 会添加autorelease (延时释放)
self.button = btn;
// self.button = [[UIButton alloc]init]; // 没有添加autorelease
self.button.frame = CGRectMake(0, 0, 100, 100);
self.button.backgroundColor = [UIColor redColor];
[self.view addSubview:self.button]; // 强引用
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 调用方法 -> 调用其他方法 return
// 问:以下代码有没有问题,如果有问题怎么解决?
// 答:没定义 largeNumber
// 问:回去等通知
// 答:如果 largeNumber 足够大,内存很高。有可能造成闪退.加入自动释放池
// 问:自动释放池加在哪里
// for循环内,for循环外
// 怎么可能是循环内添加,每次都添加一个肯定是比较慢
// 亲测
[self test1];
}
- (void)test1 {
long long largeNumber = 1000000;
@autoreleasepool {
for (long i = 0; i < largeNumber; ++i) {
NSString *str = [NSString stringWithFormat:@"hello - %ld", i];
str = [str uppercaseString];
str = [str stringByAppendingString:@" - world"];
}
}
}
- (void)test {
long long largeNumber = 100000000;
for (long i = 0; i < largeNumber; ++i) {
NSLog(@"hello");
@autoreleasepool {
NSString *str = [NSString stringWithFormat:@"hello - %ld", i];
str = [str uppercaseString];
str = [str stringByAppendingString:@" - world"];
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
多线程:自动释放池(含一个经典面试题)
最新推荐文章于 2021-11-25 06:41:39 发布