iOS如何在循环提高内存管理效率

经过测试,1,2,3的写法效果是一模一样的,4的写法最糟糕

补充一下:创建一个数组,把animal添加到数组,然后查看内存的变化,效果令人大吃一惊

笔者苦逼的使用第一种方法

 

 1 #import <Foundation/Foundation.h>
2
3 @interface Animal : NSObject
4 {
5 NSString *_name;
6 NSInteger _age;
7 }
8
9 @property (nonatomic,copy) NSString *name;
10 @property (nonatomic,assign) NSInteger age;
11
12 @end
13
14 @implementation Animal
15
16 - (void)dealloc
17 {
18 self.name = nil;
19 [super dealloc];
20 }
21
22 @synthesize name = _name;
23 @synthesize age = _age;
24
25
26 @end



 

 

1、

1         NSUInteger i = 0;
2 while (i<1000000)
3 {
4 Animal *animal = [Animal new];
5 [animal release];
6 i++;
7 }

2、

1          NSUInteger i = 0;
2 while (i<1000000)
3 {
4 NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
5 Animal *animal = [[Animal new] autorelease];
6 [loopPool release];
7 i++;
8 }

3、使用ARC

1         NSUInteger i = 0;
2 while (i<1000000)
3 {
4 Animal *animal = [Animal new];
5 i++;
6 }


4、加不加pool效果一样

1         NSUInteger i = 0;
2 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3 while (i<1000000)
4 {
5 Animal *animal = [[Animal new] autorelease];
6 i++;
7 }
8 [pool release];





posted @ 2012-03-11 00:14  dcty  阅读(478)  评论(0编辑  收藏  举报