做了几年的iOS开发,现在如果问起对象的引用计数,反而有点懵。是时候好好补习一下基础知识了。
Objective-C的内存管理不同于C的忘却手动的内存管理方式,直接使用malloc和free操控内存,也不同于基于GC语言(Java&SmallTalk&Ruby)的自动回收方式。它采用了比较折中的管理方式:半自动和半手动的结合。所谓半自动就是采用自动对象释放池进行管理,而手动就是采用应用计数值进行管理。
方法 引用计数值
alloc +1
release -1
copy +1
retain +1
1、引用计数值
Objective-C语言提供了应用计数值的管理方式。即对象每alloc1尺计数值为1同事获得内存,每retain1次计数值加1,每release1次计数值减1,知道计数值为0,对象就会自动销毁。
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NsString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSValue.h>
int main(int argc, charchar *argv[]) {
//初始化"对象释放池"
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
NSNumber *myInt = [ [ NSNumber alloc ] initWidthInt:0 ];
//因为调用了一次alloc, 所以此时 "引用计数值" 为1
NSLog( @"after alloc, myInt retain count %lx", (unsigned long) [ myInt retainCount ] );
[ myInt retain ];
//因为调用了一次retain , "引用计数值"再加1,所以此时 "引用计数值"为2
NSLog( @"after myInt retain count %lx", (unsigned long) [ myInt retainCount ] );
[ myInt release ];
//因为调用了一次release, "引用计数值"减1,所以此时 "引用计数值"为2
NSLog( @"after release, myInt retain count %lx", (unsigned long) [ myInt retainCount ] );
NSNumber *newInt = [ myInt copy ];
//复制出的新对象与原先对象是完全不同的 对象, 所以新对象 "引用计数值" 为1,原对象的 "引用计数值" 不变
NSLog( @"after copy, myInt retain count %lx", (unsigned long) [ myInt retainCount ] );
NSLog( @"after copy, newInt retain count %lx", (unsigned long) [ myInt newCount ] );
//newInt 对象呼叫一次release方法, newInt的 "引用计数值" 减 1,此时为0,newInt被彻底释放掉了[ newInt release ];
//myInt 对象呼叫一次release方法, myInt的 "引用计数值" 减 1,此时为0,myInt被彻底释放掉了
[ myInt release ];
//drain的英文意思是"流干"、”排尽“
[ pool drain ];
return 0;
}
对象释放池 有时候程序员编写的代码会返回临时变量,典型的C++做法是进行动态内存分配或者采用全局变量,谁最后使用谁释放。而Objective-C则提供了比较优雅的方式——延迟释放,即对象自动释放池。当NSAutoreleasePool被建立以后,系统会自动生成一个数组。只要当前上下文中有变量被置为autorelease,系统就会把此变量加到数组当中,然后在[pool drain]时,变量的内存参考计数值减
因此,每当有变量被设置为autorelease时,千万别手动release它,除非你有retain它。
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
@interface Test : NSObject {
int x;
}
-(void) print;
@end
@implementation Test
-(void) print {
NSLog( @"The value is %d", x );
}
@end
int main(int argc, charchar *argv[]) {
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
Test *myTest = [ [ Test alloc ] init ];
//由于调用了alloc, 此时 "引用计数值"为1
NSLog(@"myTest retain count = %x", [ myTest retainCount ] );
[ pool drain ];
//由于变量没有设置autorelease, 所以[pool drain]不会改变其 "引用计数值"
NSLog(@"after pool drain, myTest retain count = %x", [ myTest retainCount ]);
pool = [ [ NSAutoreleasePool alloc ] init ];
[ myTest autorelease ];
//autorelease并不改变 "引用计数值"
NSLog(@"after autorelease, myTest retain count = %x", [ myTest retainCount ]);
[ myTest retain ];
//经过reatin之后变量的 "引用计数值" +1
NSLog(@"after retain , myTest retain count = %x", [ myTest retainCount ]);
[ pool drain];
//由于变量设置了autorelease, 所以当对象释放池排干(drain)之后, 对象的"引用计数值"会自动减1
NSLog(@"after pool drain, myTest retain count = %x", [ myTest retainCount ]);
[ myTest release ];
}