iOS引用计数器详细测试

本文最后更新于:2 年前

一、MRC模式

  1. 局部变量
  • ARC模式下局部变量alloc后计数器+1,release后计数器-1,计数器变为0后,对象立即被释放
  • addSubview方法也是强引用,计数器+1,所以在控制器释放后,内部的subviews计数器-1,此时计数器变为0的才会被正确释放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (IBAction)clickButton:(id)sender {
AObject *aObject = [[AObject alloc] init];
NSLog(@"retainCount = %ld", aObject.retainCount);
[aObject release];

BView *bView = [[BView alloc] init];
NSLog(@"retainCount = %ld", bView.retainCount);
[self.view addSubview:bView];
NSLog(@"retainCount = %ld", bView.retainCount);
[bView release];
NSLog(@"retainCount = %ld", bView.retainCount);
NSLog(@"clickButton");
}
- (void)dealloc {
[super dealloc];
NSLog(@"AViewController dealloc");
}

2018-08-30 14:12:06.863748+0800 testram[6748:354224] retainCount = 1
2018-08-30 14:12:06.863935+0800 testram[6748:354224] AObject dealloc
2018-08-30 14:12:06.864148+0800 testram[6748:354224] retainCount = 1
2018-08-30 14:12:06.864393+0800 testram[6748:354224] retainCount = 2
2018-08-30 14:12:06.864472+0800 testram[6748:354224] retainCount = 1
2018-08-30 14:12:06.864560+0800 testram[6748:354224] clickButton
2018-08-30 14:12:11.896023+0800 testram[6748:354224] AViewController dealloc
2018-08-30 14:12:11.896327+0800 testram[6748:354224] BView dealloc
  1. 全局变量
  • ARC模式下,只有先创建局部变量,再使用属性直接赋值,此时引用计数器才会正确累加,如果使用self点语法进行初始化,那么会受到ARC的retain和strong修饰词的影响
  • addSubview的也会使计数器+1,所以在控制器释放后,添加的subViews计数器-1,此时为0的view才会释放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@interface AViewController ()
@property (nonatomic, strong) AObject *aObject;
@property (nonatomic, strong) BView *bView;
@end

@implementation AViewController

- (IBAction)clickButton:(id)sender {
self.aObject = [[AObject alloc] init];
NSLog(@"retainCount = %ld", self.aObject.retainCount);
[self.aObject release];
NSLog(@"retainCount = %ld", self.aObject.retainCount);

BView *bView = [[BView alloc] init];
_bView = bView;
NSLog(@"retainCount = %ld", bView.retainCount);
[self.view addSubview:bView];
NSLog(@"retainCount = %ld", bView.retainCount);
[bView release];
NSLog(@"retainCount = %ld", bView.retainCount);
NSLog(@"clickButton");
}

- (void)dealloc {
[super dealloc];
NSLog(@"AViewController dealloc");
}
@end

2018-08-30 14:16:16.162771+0800 testram[6779:360385] retainCount = 2
2018-08-30 14:16:16.162931+0800 testram[6779:360385] retainCount = 1
2018-08-30 14:16:16.163164+0800 testram[6779:360385] retainCount = 1
2018-08-30 14:16:16.163423+0800 testram[6779:360385] retainCount = 2
2018-08-30 14:16:16.163524+0800 testram[6779:360385] retainCount = 1
2018-08-30 14:16:16.163622+0800 testram[6779:360385] clickButton
2018-08-30 14:16:54.180754+0800 testram[6779:360385] AViewController dealloc
2018-08-30 14:16:54.181212+0800 testram[6779:360385] BView dealloc

二、ARC模式

  1. 局部变量
  • 在ARC模式下,系统对局部变量的自动回收是以方法为界限的,当方法执行完毕后,即开始回收局部变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (IBAction)clickButton:(id)sender {
NSLog(@"start");
AObject *aObject = [[AObject alloc] init];
BView *bView = [[BView alloc] init];
NSLog(@"clickButton");
}

- (void)dealloc {
NSLog(@"AViewController dealloc");
}

2018-08-30 11:06:48.769565+0800 testram[6184:253141] start
2018-08-30 11:06:48.769851+0800 testram[6184:253141] clickButton
2018-08-30 11:06:48.769980+0800 testram[6184:253141] BView dealloc
2018-08-30 11:06:48.770490+0800 testram[6184:253141] AObject dealloc
2018-08-30 11:06:54.061334+0800 testram[6184:253141] AViewController dealloc
  1. 全局变量
  • 在ARC模式下,系统推荐我们使用strong修饰一般属性对象,而且对点语法进行了兼容,所以可以直接使用strong的点语法进行初始化赋值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@interface AViewController ()
@property (nonatomic, strong) AObject *aObject;
@property (nonatomic, strong) BView *bView;
@end

@implementation AViewController

- (IBAction)clickButton:(id)sender {
NSLog(@"start");
self.aObject = [[AObject alloc] init];
self.bView = [[BView alloc] init];
[self.view addSubview:self.bView];
NSLog(@"clickButton");
}

- (void)dealloc {
NSLog(@"AViewController dealloc");
}

@end

2018-08-30 13:49:36.579188+0800 testram[6559:324193] start
2018-08-30 13:49:36.579550+0800 testram[6559:324193] clickButton
2018-08-30 13:49:39.892344+0800 testram[6559:324193] AViewController dealloc
2018-08-30 13:49:39.892746+0800 testram[6559:324193] AObject dealloc
2018-08-30 13:49:39.893146+0800 testram[6559:324193] BView dealloc