//全局Autoreleasepool释放
- (void)viewDidLoad
{
[super viewDidLoad];
Person *p1 = [[[Person alloc] init] autorelease];
NSLog(@"%s",__func__);
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"%s",__func__);
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%s",__func__);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
Person *p1 = [[[Person alloc] init] autorelease];
}
//结果:
2018-08-06 10:40:03.480734+0800 testDemo[50830:20242191] -[ViewController viewDidLoad]
2018-08-06 10:40:03.480916+0800 testDemo[50830:20242191] -[ViewController viewWillAppear:]
2018-08-06 10:40:03.482709+0800 testDemo[50830:20242191] -[Person dealloc]
2018-08-06 10:40:03.483981+0800 testDemo[50830:20242191] -[ViewController viewDidAppear:]
//局部Autoreleasepool释放
- (void)viewDidLoad
{
[super viewDidLoad];
@autoreleasepool {
Person *p1 = [[[Person alloc] init] autorelease];
//两种方式都可以
// NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Person *p1 = [[Person alloc] init];
// [p1 autorelease];
// [pool drain];
//以下不会释放
// @autoreleasepool {
// Person *p1 = [[Person alloc] init];
// }
}
NSLog(@"%s",__func__);
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"%s",__func__);
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%s",__func__);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
Person *p1 = [[[Person alloc] init] autorelease];
}
//结果:
2018-08-06 10:43:28.531730+0800 testDemo[50863:20265706] -[Person dealloc]
2018-08-06 10:43:28.531854+0800 testDemo[50863:20265706] -[ViewController viewDidLoad]
2018-08-06 10:43:28.531984+0800 testDemo[50863:20265706] -[ViewController viewWillAppear:]
2018-08-06 10:43:28.534778+0800 testDemo[50863:20265706] -[ViewController viewDidAppear:]
网友评论