int main()
{
/* my first program in Objective-C */
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass sampleMethod];
NSLog(@"Retain Count after initial allocation: %d",
[sampleClass retainCount]);
[sampleClass retain];
NSLog(@"Retain Count after retain: %d", [sampleClass retainCount]);
[sampleClass release];
NSLog(@"Retain Count after release: %d", [sampleClass retainCount]);
[sampleClass release];
NSLog(@"SampleClass dealloc will be called before this");
// Should set the object to nil
sampleClass = nil;
return 0;
}
当我们编译上面的程序,我们会得到下面的输出。
2013-09-28 04:39:52.310 demo[8385] Hello, World!
2013-09-28 04:39:52.311 demo[8385] Retain Count after initial allocation: 1
2013-09-28 04:39:52.311 demo[8385] Retain Count after retain: 2
2013-09-28 04:39:52.311 demo[8385] Retain Count after release: 1
2013-09-28 04:39:52.311 demo[8385] Object deallocated
2013-09-28 04:39:52.311 demo[8385] SampleClass dealloc will be called before this
“自动引用计数”或ARC
在自动引用计数或ARC时,系统使用相同的引用计数系统MRR,但它插入相应的内存管理方法要求我们在编译时间。我们强烈鼓励使用ARC新的项目中。如果我们使用ARC,通常是有没有需要了解本文档中描述的底层实现,尽管它可能在某些情况下是有益的。如需更多关于ARC,请参阅 过渡到ARC发布注释.