偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

 

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

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

在這個(gè)小程序中,我們先生成了一個(gè)autorelease pool,然后生成一個(gè)autorelease的ClassA的實(shí)例,再在一個(gè)新的run loop中生成一個(gè)autorelease的ClassB的對(duì)象(注意,我們并沒有手動(dòng)在新run loop中生成autorelease pool)。精簡的示例代碼如下,詳細(xì)代碼請(qǐng)見附件中的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實(shí)例 
  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實(shí)例就已經(jīng)被銷毀了。

有人可能會(huì)說,這并不能說明新的run loop自動(dòng)生成了一個(gè)新的autorelease pool,說不定還只是用了老的autorelease pool,只不過后來drain了一次而已。我們可以在main函數(shù)中不生成autorelease pool。精簡的示例代碼如下,詳細(xì)代碼請(qǐng)見附件中的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實(shí)例 
  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的實(shí)例依然被自動(dòng)銷毀了,這說明新的run loop自動(dòng)創(chuàng)建了一個(gè)autorelease pool,這個(gè)pool在新的run loop結(jié)束的時(shí)候會(huì)銷毀自己(并自動(dòng)release所包含的對(duì)象)。

補(bǔ)充說明

在研究retain count的時(shí)候,我不建議用NSString。因?yàn)樵谙旅娴恼Z句中,

 

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

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

當(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-20 17:04:43

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

2011-07-21 09:32:07

Objective-C 內(nèi)存 Autoreleas

2011-07-21 10:10:42

Objective-C 內(nèi)存 Autoreleas

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 對(duì)象 模板

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
點(diǎn)贊
收藏

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