自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Objective-C內(nèi)存管理教程和原理剖析(四)

移動開發(fā) iOS
初學(xué)Objective-C的朋友都有一個困惑,總覺得對Objective-C的內(nèi)存管理機(jī)制琢磨不透,程序經(jīng)常內(nèi)存泄漏或莫名其妙的崩潰。我在這里總結(jié)了自己對Objective-C內(nèi)存管理機(jī)制的研究成果和經(jīng)驗,寫了這么一個由淺入深的教程。希望對大家有所幫助,也歡迎大家一起探討。

 

系統(tǒng)自動創(chuàng)建新的autorelease pool

在生成新的Run Loop的時候,系統(tǒng)會自動創(chuàng)建新的autorelease pool(非常感謝網(wǎng)友hhyytt和neogui的提醒)。注意,此處不同于xcode在新建項目時自動生成的代碼中加入的autorelease pool,xcode生成的代碼可以被刪除,但系統(tǒng)自動創(chuàng)建的新的autorelease pool是無法刪除的(對于無Garbage Collection的環(huán)境來說)。Objective-C沒有給出實現(xiàn)代碼,官方文檔也沒有說明,但我們可以通過小程序來證明。

在這個小程序中,我們先生成了一個autorelease pool,然后生成一個autorelease的ClassA的實例,再在一個新的run loop中生成一個autorelease的ClassB的對象(注意,我們并沒有手動在新run loop中生成autorelease pool)。精簡的示例代碼如下,詳細(xì)代碼請見附件中的memman-run-loop-with-pool.m。

 

  1. int main(int argc, char**argv)  
  2.          NSLog(@"create an autorelasePool\n"); 
  3.          NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];    
  4.          NSLog(@"create an instance of ClassA and autorelease\n"); 
  5.          ClassA *obj1 = [[[ClassA alloc] init] autorelease]; 
  6.          NSDate *now = [[NSDate alloc] init]; 
  7.          NSTimer *timer = [[NSTimer alloc] initWithFireDate:now 
  8.                    interval:0.0 
  9.                    target:obj1 
  10.                    selector:@selector(createClassB) 
  11.                    userInfo:nil 
  12.                    repeats:NO]; 
  13.          NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 
  14.          [runLoop addTimer:timer forMode:NSDefaultRunLoopMode]; 
  15.          [timer release]; 
  16.          [now release]; 
  17.          [runLoop run]; //在新loop中調(diào)用一函數(shù),生成ClassB的autorelease實例 
  18.          NSLog(@"releasing autorelasePool\n"); 
  19.          [pool release]; 
  20.          NSLog(@"autorelasePool is released\n"); 
  21.          return 0; 
  22. }  

 

輸出如下:

create an autorelasePool

create an instance of ClassA and autorelease

create an instance of ClassB and autorelease

ClassB destroyed

releasing autorelasePool

ClassA destroyed

autorelasePool is released

注意在我們銷毀autorelease pool之前,ClassB的autorelease實例就已經(jīng)被銷毀了。

有人可能會說,這并不能說明新的run loop自動生成了一個新的autorelease pool,說不定還只是用了老的autorelease pool,只不過后來drain了一次而已。我們可以在main函數(shù)中不生成autorelease pool。精簡的示例代碼如下,詳細(xì)代碼請見附件中的memman-run-loop-without-pool.m。

 

  1. int main(int argc, char**argv)  
  2.          NSLog(@"No autorelasePool created\n"); 
  3.          NSLog(@"create an instance of ClassA\n"); 
  4.          ClassA *obj1 = [[ClassA alloc] init]; 
  5.          NSDate *now = [[NSDate alloc] init]; 
  6.          NSTimer *timer = [[NSTimer alloc] initWithFireDate:now 
  7.                    interval:0.0 
  8.                    target:obj1 
  9.                    selector:@selector(createClassB) 
  10.                    userInfo:nil 
  11.                    repeats:NO]; 
  12.          NSRunloop *runLoop = [NSRunLoop currentRunLoop]; 
  13.          [runLoop addTimer:timer forMode:NSDefaultRunLoopMode]; 
  14.          [timer release]; 
  15.          [now release]; 
  16.          [runLoop run]; //在新loop中調(diào)用一函數(shù),生成ClassB的autorelease實例 
  17.          NSLog(@"Manually release the instance of ClassA\n"); 
  18.          [obj1 release]; 
  19.          return 0; 
  20. }  

 

輸出如下:

No autorelasePool created

create an instance of ClassA

create an instance of ClassB and autorelease

ClassB destroyed

Manually release the instance of ClassA

ClassA destroyed

我們可以看出來,我們并沒有創(chuàng)建任何autorelease pool,可是ClassB的實例依然被自動銷毀了,這說明新的run loop自動創(chuàng)建了一個autorelease pool,這個pool在新的run loop結(jié)束的時候會銷毀自己(并自動release所包含的對象)。

補(bǔ)充說明

在研究retain count的時候,我不建議用NSString。因為在下面的語句中,

 

  1. NSString *str1 = @”constant string”; 

str1的retain count是個很大的數(shù)字。Objective-C對常量字符串做了特殊處理。

當(dāng)然,如果你這樣創(chuàng)建NSString,得到的retain count依然為1

  1. NSString *str2 = [NSString stringWithFormat:@”123”]; 

 示例代碼文件鏈接:http://files.cnblogs.com/VinceYuan/objective-c-memman.zip

 

 
責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2011-07-19 15:15:09

Objective-C 內(nèi)存

2011-07-21 09:42:27

Objective-C 內(nèi)存 Autoreleas

2013-04-11 14:32:00

Objective-CiOS開發(fā)內(nèi)存管理@synthesize

2013-04-11 13:57:27

Objective-CiOS開發(fā)內(nèi)存管理

2013-04-11 14:16:57

Objective-CiOS開發(fā)內(nèi)存管理

2011-07-18 17:14:16

Objective-C 內(nèi)存 Cocoa

2011-07-29 16:08:31

Objective-C 內(nèi)存

2011-07-27 17:10:30

Objective-C 持久化

2011-05-11 15:45:50

內(nèi)存管理Objective-C

2011-07-21 10:10:42

Objective-C 內(nèi)存 Autoreleas

2011-07-21 09:32:07

Objective-C 內(nèi)存 Autoreleas

2011-07-20 17:04:43

Objective-C 內(nèi)存 內(nèi)存泄露

2011-08-01 11:37:41

iPhone Objective- 內(nèi)存

2011-08-16 17:43:47

Objective-C內(nèi)存管理Autorelease

2011-08-18 13:28:35

Objective-C內(nèi)存

2011-07-08 13:49:46

Objective-C UUID

2011-08-05 14:03:39

Objective-C 對象 模板

2011-08-22 09:48:16

WindowsObjective-C

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用鍵

2013-05-02 10:51:17

iOS開發(fā)Objective-C@property
點贊
收藏

51CTO技術(shù)棧公眾號