autorelease pool
DESCRIPTION
TRANSCRIPT
Autorelease PoolMichael Pan
13年5月27⽇日星期⼀一
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc] initWithString:@”Bob”];
return name;}
Memory leak!!!
iOS 4
13年5月27⽇日星期⼀一
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc] initWithString:@”Bob”];
[name release]; // ??return name;
}
iOS 4
13年5月27⽇日星期⼀一
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc] initWithString:@”Bob”];
return name;[name release]; // ??
}
iOS 4
13年5月27⽇日星期⼀一
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc] initWithString:@”Bob”];
[name autorelease];return name;
}
NSString * outer = [customer name];[outer retain];//....[outer release];
iOS 4
13年5月27⽇日星期⼀一
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc] initWithString:@”Bob”];
// [name autorelease];return name;
}
ARC
13年5月27⽇日星期⼀一
Name does matter• begin with alloc/new/copy/mutableCopy will not be put
into autorelease pool
• others will add object into autorelease pool automatically
-(id) newCar;
13年5月27⽇日星期⼀一
Demo• ARCDemo
13年5月27⽇日星期⼀一
Pool操作物件時機• 加⼊入
• 使⽤用[obj autorelease]
• 常數字串
• class level constructor
• 呼叫[obj release]
• 當[pool drain]
• event loop 結束
autorelease pool 無法 retain
NO Arc
13年5月27⽇日星期⼀一
⾃自⾏行產⽣生Pool• NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];
• 新增多個pool會以stack ⽅方式儲存
• 物件呼叫 autorelease時會被加⼊入其最近的pool
• 某個pool 呼叫 drain 之後,其上的pool 也都會被drain
pool1
pool2
int main(){ NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];}
NO Arc
13年5月27⽇日星期⼀一
RecapNSMutableString * s1 = [[NSMutableString alloc] initWithString:@"hello"];
NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];!NSArray * staticArray = [NSArray arrayWithObject:s1];NSLog(@"s1 retain count is %lu", [s1 retainCount]);[pool1 drain];NSLog(@"s1 retain count is %lu", [s1 retainCount]);
Result is ?pool2
pool1
staticArrays1
iOS 4
13年5月27⽇日星期⼀一
⾃自⾏行產⽣生Poolint main(){ @autoreleasepool{
//...}
}
iOS 5
13年5月27⽇日星期⼀一
Clean pool nicelyint i; for (i = 0; i < 1000000; i++) { id object = [someArray objectAtIndex: i]; NSString *desc = [object description]; // and do something with the description }
NSAutoreleasePool *pool; pool = [[NSAutoreleasePool alloc] init]; int i; for (i = 0; i < 1000000; i++) { id object = [someArray objectAtIndex: i]; NSString *desc = [object description]; // and do something with the description if (i % 1000 == 0) { [pool drain]; pool = [[NSAutoreleasePool alloc] init]; } } [pool release]
iOS 4
13年5月27⽇日星期⼀一
Clean pool nicelyint i; for (i = 0; i < 1000000; i++) { id object = [someArray objectAtIndex: i]; NSString *desc = [object description]; // and do something with the description }
int i; for (i = 0; i < 1000000; i++) { @autoreleasepool{
id object = [someArray objectAtIndex: i]; NSString *desc = [object description]; // and do something with the description
}}
iOS 5
13年5月27⽇日星期⼀一
EqualNSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
[obj autorelease];
[pool drain];
@autoreleasepool{id __autoreleasing obj2;obj2 = obj;
}
ARC
no ARC
13年5月27⽇日星期⼀一