Objective C中没有像Java这样的运行时自动内存管理的技术。它采用"引用计数"(Reference Count)的方式进行内存管理。在iOS 5.0之前开发iOS应用需要对内存手动管理(Manual Retain-Release, 简称 MRR)。
从iOS 5.0开始,苹果将Mac上使用多时的自动内存管理技术Auto Reference Count(简称ARC)引入到iOS平台,包括最新的Swift语言也使用ARC的方式进行内存管理。
ARC是一种编译器期间生效的内存管理技术,即在编译器期间插入手动管理内存需要编写的代码 (关于流行的GC方式,请参看《代码未来》的内存管理章节).
ARC引入简化了大量内存管理操作,但是从理解ObjC内存管理方面讲,MRR更有助于理解内存管理。本文将探讨MRU的使用,以便更好的理解ObjC中的内存管理。
NSObject中与内存管理操作相关的方法:
如何进行内存管理:
基本使用:
NSObject *obj = [[NSObject alloc] init]; //使用对象obj... NSObject *ref = [obj retain];//引用计数+1 NSLog(@"retainCount: %d", [obj retainCount]); //=> retainCount: 2 //使用ref [ref release]; NSLog(@"retainCount: %d", [obj retainCount]); //=> retainCount: 1 [obj release];//使用完释放
// new, alloc, copy操作获取一个对象,需要手动释放 NSString *manualReleaseString = [[NSString alloc] initWithFormat:@"manual release"]; // 使用... [manualReleaseString release]; //使用其他方法获取一个对象, 不用手动释放 NSString *autoReleaseString = [NSString stringWithFormat:@"autorelease"]; // 使用,会自动释放 //使用alloc的等价方法 NSString *sameAutoReleaseString = [[[NSString alloc] initWithFormat:@"auto release string"] autorelease]; // 使用,会自动释放
@interface MyObject : NSObject - (instancetype)initWithOptions; + (instancetype)objectWithOptions; @end /* 通过alloc构建对象 */ @implementation MyObject - (instancetype)initWithOptions { self = [super init]; if (self) { } return self; } /* 不通过alloc/new/copy构造对象 */ + (instancetype) objectWithOptions { return [[[MyObject alloc] init] autorelease]; } @end
使用:
MyObject *myObject = [[MyObject alloc] initWithOptions]; [myObject release]; MyObject *autoReleaseMyObject = [MyObject objectWithOptions];
@interface MyObject : NSObject { NSMutableArray *_datasource; } - (instancetype)initWithOptions; + (instancetype)objectWithOptions; @end /* 通过alloc构建对象 */ @implementation MyObject - (instancetype)initWithOptions { self = [super init]; if (self) { //持有NSMutable对象 _datasource = [[NSMutableArray alloc] initWithCapacity:12]; } return self; } - (void)dealloc { // 必须在dealloc方法中手动释放 [_datasource release]; _datasource = nil; [super dealloc]; }
NSString *string = [[NSString alloc] initWithFormat:@"manual release string"]; NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:12]; [mutableArray addObject:string]; NSLog(@"string retainCount: %d", [string retainCount]); //=> retainCount = 2 [string release]; NSString *fromArrayString = [mutableArray objectAtIndex:0]; NSLog(@"%@", fromArrayString);//=> manual release string [mutableArray release];// 会释放容器内所有对象 //NSLog(@"%@", fromArrayString); //访问已dealloc对象会crash
NSString *allocString = [[NSString alloc] initWithFormat:@"manual release allocString"]; NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithCapacity:12]; [mutableDictionary setObject:allocString forKey:@"text"]; NSLog(@"string retainCount: %d", [allocString retainCount]); // => 2 [allocString release]; NSString *stringFromDict = [mutableDictionary objectForKey:@"text"]; NSLog(@"%@", stringFromDict); [mutableDictionary release];//释放容器内所有对象 //NSLog(@"%@", stringFromDict); // crash
在内存管理时使用到autorelease方法,该方法标示当前对象会被自动回收。实质上这些对象都被放倒AutoReleasePool进行管理,当AutoReleasePool释放时,autorelease对象将被释放。
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *string = [[NSString alloc] initWithFormat:@"automatic released"]; [string autorelease]; [pool release];
@property中涉及内存管理的标示:
@interface ViewController : UIViewController @property (nonatomic, retain) UITextField *nameField; @property (nonatomic, assign) int pageNumber; @property (nonatomic, copy) NSString *inputName; //从UITextField中获取可变的text @end
关于,MRR中的retina,assign, ARC中的strong, weak。StackOverflow上有篇很好的解释:
http://stackoverflow.com/questions/8927727/objective-c-arc-strong-vs-retain-and-weak-vs-assign