SlideShare a Scribd company logo
1 of 15
Download to read offline
Autorelease Pool
Michael 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⽇日星期⼀一
Recap
NSMutableString * 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
staticArray
s1
iOS 4
13年5月27⽇日星期⼀一
⾃自⾏行產⽣生Pool
int main(){
@autoreleasepool{
//...
}
}
iOS 5
13年5月27⽇日星期⼀一
Clean pool nicely
int 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 nicely
int 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⽇日星期⼀一
Equal
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
[obj autorelease];
[pool drain];
@autoreleasepool{
id __autoreleasing obj2;
obj2 = obj;
}
ARC
no ARC
13年5月27⽇日星期⼀一

More Related Content

What's hot

可用空間的管理
可用空間的管理可用空間的管理
可用空間的管理倩怡 賴
 
Web scraping入門1
Web scraping入門1Web scraping入門1
Web scraping入門1兆欽 丘
 
基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册Zhen Li
 
由Hash Set谈重用
由Hash Set谈重用由Hash Set谈重用
由Hash Set谈重用yiditushe
 
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2Yang FaFa
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharingYi-Jun Zheng
 

What's hot (7)

可用空間的管理
可用空間的管理可用空間的管理
可用空間的管理
 
Zsh
ZshZsh
Zsh
 
Web scraping入門1
Web scraping入門1Web scraping入門1
Web scraping入門1
 
基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册
 
由Hash Set谈重用
由Hash Set谈重用由Hash Set谈重用
由Hash Set谈重用
 
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing
 

Viewers also liked

Homework2 play cards
Homework2 play cardsHomework2 play cards
Homework2 play cardsMichael Pan
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 StevenMichael Pan
 
Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Michael Pan
 
Core data lightweight_migration
Core data lightweight_migrationCore data lightweight_migration
Core data lightweight_migrationMichael Pan
 
Strategy Pattern for Objective-C
Strategy Pattern for Objective-CStrategy Pattern for Objective-C
Strategy Pattern for Objective-CMichael Pan
 

Viewers also liked (6)

Homework2 play cards
Homework2 play cardsHomework2 play cards
Homework2 play cards
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 Steven
 
Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Opening iOS App 開發者交流會
Opening iOS App 開發者交流會
 
Strategy pattern
Strategy patternStrategy pattern
Strategy pattern
 
Core data lightweight_migration
Core data lightweight_migrationCore data lightweight_migration
Core data lightweight_migration
 
Strategy Pattern for Objective-C
Strategy Pattern for Objective-CStrategy Pattern for Objective-C
Strategy Pattern for Objective-C
 

Similar to Autorelease pool

iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门Lucien Li
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门Lucien Li
 
冲浪 Object-c
冲浪 Object-c冲浪 Object-c
冲浪 Object-cjeff kit
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013Michael Pan
 
iOS程序设计-数据持久化
iOS程序设计-数据持久化iOS程序设计-数据持久化
iOS程序设计-数据持久化qiyutan
 

Similar to Autorelease pool (6)

ios分享
ios分享ios分享
ios分享
 
iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门
 
冲浪 Object-c
冲浪 Object-c冲浪 Object-c
冲浪 Object-c
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013
 
iOS程序设计-数据持久化
iOS程序设计-数据持久化iOS程序设计-数据持久化
iOS程序设计-数据持久化
 

More from Michael Pan

Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android StudioMichael Pan
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and GenymotionMichael Pan
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Michael Pan
 
Prototype by Xcode
Prototype by XcodePrototype by Xcode
Prototype by XcodeMichael Pan
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdfMichael Pan
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperMichael Pan
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. JackyMichael Pan
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017Michael Pan
 

More from Michael Pan (13)

Activity
ActivityActivity
Activity
 
Shootting Game
Shootting GameShootting Game
Shootting Game
 
Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android Studio
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and Genymotion
 
Note something
Note somethingNote something
Note something
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4
 
Prototype by Xcode
Prototype by XcodePrototype by Xcode
Prototype by Xcode
 
Nimbus
NimbusNimbus
Nimbus
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdf
 
ADB - Arthur
ADB - ArthurADB - Arthur
ADB - Arthur
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game Developer
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. Jacky
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017
 

Autorelease pool

  • 2. 回傳新產⽣生的物件 -(NSString *) name{ NSString * name= [[NSString alloc] initWithString:@”Bob”]; return name; } Memory leak!!! iOS 4 13年5月27⽇日星期⼀一
  • 3. 回傳新產⽣生的物件 -(NSString *) name{ NSString * name= [[NSString alloc] initWithString:@”Bob”]; [name release]; // ?? return name; } iOS 4 13年5月27⽇日星期⼀一
  • 4. 回傳新產⽣生的物件 -(NSString *) name{ NSString * name= [[NSString alloc] initWithString:@”Bob”]; return name; [name release]; // ?? } iOS 4 13年5月27⽇日星期⼀一
  • 5. 回傳新產⽣生的物件 -(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⽇日星期⼀一
  • 6. 回傳新產⽣生的物件 -(NSString *) name{ NSString * name= [[NSString alloc] initWithString:@”Bob”]; // [name autorelease]; return name; } ARC 13年5月27⽇日星期⼀一
  • 7. 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⽇日星期⼀一
  • 9. Pool操作物件時機 • 加⼊入 • 使⽤用[obj autorelease] • 常數字串 • class level constructor • 呼叫[obj release] • 當[pool drain] • event loop 結束 autorelease pool 無法 retain NO Arc 13年5月27⽇日星期⼀一
  • 10. ⾃自⾏行產⽣生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⽇日星期⼀一
  • 11. Recap NSMutableString * 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 staticArray s1 iOS 4 13年5月27⽇日星期⼀一
  • 13. Clean pool nicely int 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⽇日星期⼀一
  • 14. Clean pool nicely int 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⽇日星期⼀一
  • 15. Equal NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; [obj autorelease]; [pool drain]; @autoreleasepool{ id __autoreleasing obj2; obj2 = obj; } ARC no ARC 13年5月27⽇日星期⼀一