Objective-C内存管理教程和原理剖析(四)

移动开发 iOS
初学Objective-C的朋友都有一个困惑,总觉得对Objective-C的内存管理机制琢磨不透,程序经常内存泄漏或莫名其妙的崩溃。我在这里总结了自己对Objective-C内存管理机制的研究成果和经验,写了这么一个由浅入深的教程。希望对大家有所帮助,也欢迎大家一起探讨。

 

系统自动创建新的autorelease pool

在生成新的Run Loop的时候,系统会自动创建新的autorelease pool(非常感谢网友hhyytt和neogui的提醒)。注意,此处不同于xcode在新建项目时自动生成的代码中加入的autorelease pool,xcode生成的代码可以被删除,但系统自动创建的新的autorelease pool是无法删除的(对于无Garbage Collection的环境来说)。Objective-C没有给出实现代码,官方文档也没有说明,但我们可以通过小程序来证明。

在这个小程序中,我们先生成了一个autorelease pool,然后生成一个autorelease的ClassA的实例,再在一个新的run loop中生成一个autorelease的ClassB的对象(注意,我们并没有手动在新run loop中生成autorelease pool)。精简的示例代码如下,详细代码请见附件中的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中调用一函数,生成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实例就已经被销毁了。

有人可能会说,这并不能说明新的run loop自动生成了一个新的autorelease pool,说不定还只是用了老的autorelease pool,只不过后来drain了一次而已。我们可以在main函数中不生成autorelease pool。精简的示例代码如下,详细代码请见附件中的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中调用一函数,生成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

我们可以看出来,我们并没有创建任何autorelease pool,可是ClassB的实例依然被自动销毁了,这说明新的run loop自动创建了一个autorelease pool,这个pool在新的run loop结束的时候会销毁自己(并自动release所包含的对象)。

补充说明

在研究retain count的时候,我不建议用NSString。因为在下面的语句中,

 

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

str1的retain count是个很大的数字。Objective-C对常量字符串做了特殊处理。

当然,如果你这样创建NSString,得到的retain count依然为1

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

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

 

 
责任编辑:闫佳明 来源: oschina
相关推荐

2011-07-21 09:42:27

Objective-C 内存 Autoreleas

2011-07-19 15:15:09

Objective-C 内存

2013-04-11 14:32:00

Objective-CiOS开发内存管理@synthesize

2013-04-11 13:57:27

Objective-CiOS开发内存管理

2013-04-11 14:16:57

Objective-CiOS开发内存管理

2011-07-18 17:14:16

Objective-C 内存 Cocoa

2011-07-29 16:08:31

Objective-C 内存

2011-07-27 17:10:30

Objective-C 持久化

2011-05-11 15:45:50

内存管理Objective-C

2011-07-20 17:04:43

Objective-C 内存 内存泄露

2011-07-21 09:32:07

Objective-C 内存 Autoreleas

2011-07-21 10:10:42

Objective-C 内存 Autoreleas

2011-08-18 13:28:35

Objective-C内存

2011-08-01 11:37:41

iPhone Objective- 内存

2011-08-16 17:43:47

Objective-C内存管理Autorelease

2011-07-08 13:49:46

Objective-C UUID

2011-08-05 14:03:39

Objective-C 对象 模板

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2013-05-02 10:51:17

iOS开发Objective-C@property

2011-08-22 09:48:16

WindowsObjective-C
点赞
收藏

51CTO技术栈公众号