`
zcw_java
  • 浏览: 296844 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ARC(Automatic Reference Counting)

 
阅读更多
ARC是iOS 5推出的新功能,全称叫 ARC(Automatic Reference Counting)。简单地说,就是代码中自动加入了retain/release,原先需要手动添加的用来处理内存管理的引用计数的代码可以自动地由编译器完成了。

该机能在 iOS 5/ Mac OS X 10.7 开始导入,利用 Xcode4.2 可以使用该机能。简单地理解ARC,就是通过指定的语法,让编译器(LLVM 3.0)在编译代码时,自动生成实例的引用计数管理部分代码。有一点,ARC并不是GC,它只是一种代码静态分析(Static Analyzer)工具。

//ARC使用前
@interface NonARCObject : NSObject {  
    NSString *name;  
}  
-(id)initWithName:(NSString *)name;  
@end  
 
@implementation NonARCObject  
-(id)initWithName:(NSString *)newName {  
    self = [super init];  
    if (self) {  
        name = [newName retain];  
    }  
    return self;  
}  
 
-(void)dealloc {  
    [name release];  
    [Super dealloc];  
}  
@end  
//ARC使用后
@interface ARCObject : NSObject {  
    NSString *name;  
}  
-(id)initWithName:(NSString *)name;  
@end  
 
@implementation ARCObject  
-(id)initWithName:(NSString *)newName {  
    self = [super init];  
    if (self) {  
        name = newName;  
    }  
    return self;  
}  
@end  

曾经都是这么使用,retain使引用计数+1后使用autorelease自动释放
return [[object retain] autorelease];
而使用ARC后,我们可以不需要这样做了,甚至连最基础的release都不需要了。

ARC的好处

如果有像我一样从Java转oc的朋友一定觉得这个太棒了,
1、内存的管理交给编译器处理,不需要我们在担心内存管理上的不当造成泄漏
2、代码变少了,-(void)dealloc{ [x release]; [y release]; }
3、降低大家的错误释放造成低效代码
ARC的缺点
1、有朋友说游戏开发的一些大牛还是习惯手动释放,这样效率会更高!个人比较向往
2、重构一些旧项目,XCode4.2 中缺省ARC就是 ON 的状态,所以编译旧代码的时候往往有"Automatic Reference Counting Issue"的错误信息。



这个时候,可以将项目编译设置中的“Objectice-C Auto Reference Counteting”设为NO



如果只想对某个.m文件不适应ARC,可以只针对该类文件加上 -fno-objc-arc 编译FLAGS


ARC基本规则

    retain, release, autorelease, dealloc由编译器自动插入,不能在代码中调用
    dealloc虽然可以被重载,但是不能调用[super dealloc]
由于ARC并不是GC,并需要一些规则让编译器支持代码插入,所以必须清楚清楚了这些规则后,才能写出健壮的代码。

OC中的对象

强参照(Strong reference)和弱参照(Weak reference)之分,当需要保持其他对象的时候,需要retain以确保对象引用计数加1。对象的持有者(owner)只要存在,那么该对象的强参照就一直存在。
//1
NSString *test1 = [[NSString alloc] initWithString:@"test1"];    
//2        
NSString *test2 = test1;
//3        
test1 = [[NSString alloc] initWithString:@"test1Change"];
//4        
NSString *test3 = test1;
//5        
test2 = test3;

1、test1作为'test1'对象的最初持有者,是NSString类型对象的强参照
2、将test1带入test2,这里test2也是强参照
3、test1改变内容,生成test1Change字符串,test1变成了该字符串的拥有者,而‘test1’字符串只有test2拥有,他们都在内存中存在
4、test1赋值给test3,这里也是强参照
5、test3赋值给test2,这里的‘Test1’字符串没有拥有者,该对象被释放

弱参照
//1
NSString *test1 = [[NSString alloc] initWithString:@"test1"];    
_weak NSString *test2 = test1;

这里当test1被释放时,test2为nil。

NSString __weak *string = [[NSString alloc] initWithFormat:@"First Name: %@", [self firstName]]; 
NSLog(@"string: %@", string); //此时 string为空

  • 描述: 1
  • 大小: 16.3 KB
  • 大小: 78.8 KB
  • 大小: 245.3 KB
分享到:
评论

相关推荐

    ARC (Automatic Reference Counting)自动引用计数的使用详解

    详细介绍了IOS中ARC的使用方法。一看便懂 。

    Object-C语言教程&案例,要点难点,代码示例,代码解析

    内存管理:了解ARC(Automatic Reference Counting,自动引用计数)和MRC(Manual Reference Counting,手动引用计数)的工作原理。 Foundation框架:学习Foundation框架中提供的常用类,如NSString、NSArray、...

    The Swift Programming Language_笔记

    通过ARC(Automatic Reference Counting,自动引用计数)来简化内存管理。我们的框架栈则一直基于Cocoa。Objective-C进化支持了块、collection literal和模块,允许现代语言的框架无需深入即可使用。(by gashero)感谢...

    iOS开发系列-ARC浅解

     所谓ARC是Automatic Reference Counting , 即自动引用计数。ARC是自iOS5引入的。ARC机制的引入是为了简化开发过程的内存管理的。相对于之前的MRC (Manual Reference Counting) , ARC机制显得更加自动化。在使用...

    iOS 弹出式菜单 MGTileMenu.zip

    iOS 弹出式菜单 MGTileMenu ,这个控件基于iOS 5开发,使用了ARC(Automatic Reference Counting)...

    EhLib_9.2_Build_9.2.020_Evaluation_version

    + Supports of ARC (Automatic Reference Counting) Delphi compilers + Compatibility with the CrossVCL library. Now you can compile your VCL project into a GUI application for OS X and Linux. + ...

    Apple Swift编程语言入门教程.doc

    通过ARC(Automatic Reference Counting,自动引用计数)来简化内存管理。我们的框架栈则一直基于Cocoa。Objective-C进化支持了块、collection literal和模块,允许现代语言的框架无需深入即可使用。(by gashero)感谢...

    Objective C Memory Management Essentials(PACKT,2015)

    You will also see what ARC (Automatic Reference Counting) is and how it helps in memory management. Finally, you will cover examples on how to use the various tools provided by Xcode to help in ...

    Programming in Objective-C 4th Edition Stephen G. Kochan

    The fourth edition of this book has been updated to cover the significant changes that first appeared in iOS 5 and Xcode 4.2, including the use of Automatic Reference Counting (ARC) to improve and ...

    ios开发技巧总结.docx

    ios开发 iOS开发涵盖了许多方面,以下是一些关键的开发技巧和最佳实践: 熟悉Swift或Objective-C:Swift是苹果...内存管理:理解ARC(Automatic Reference Counting)自动引用计数机制,避免内存泄漏和循环引用问题。

    Programming in Objective-C, 第四版,ePub格式

    17 Memory Management and Automatic Reference Counting (ARC) 18 Copying Objects 19 Archiving Part III: Cocoa, Cocoa Touch, and the iOS SDK 20 Introduction to Cocoa and Cocoa Touch 21 Writing iOS ...

    Programming in Objective-C(最新第四版)

    The fourth edition of this book has been updated to cover the significant changes that first appeared in iOS 5 and Xcode 4.2, including the use of Automatic Reference Counting (ARC) to improve and ...

    swift编程语言教程 2014 官方正式版.zip

    5.通过ARC(Automatic Reference Counting,自动引用计数)来简化内存管理。我们的框架栈则一直基于Cocoa 6.Objective-C进化支持了块、collection literal和模块,允许现代语言的框架无需深入即可使用 7.集成了现代...

    swift中文教程

    使用自动引用计数(Automatic Reference Counting, ARC)来简化内存管理。我们在 Foundation 和 Cocoa 的 基础上构建框架栈使其完全现代化和标准化。 Objective-C 本身支持块、集合语法和模块,所以框架可以轻松支 ...

    详解优化iOS程序性能的25个方法

    ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露。它自动为你管理retain和release的过程,所以你就不必去手动干预了。忘掉代码...

    OCBorghettiView手风琴式折叠菜单特效

    OCBorghettiView手风琴式折叠菜单特效,源码OCBorghettiView,OCBorghettiView可实现手风琴式折叠菜单效果,设计可重新使用,适合iPhone和iPad应用,使用ARC (Automatic Reference Counting),支持设备旋转,同时还...

    Application Development with Swift(PACKT,2015)

    Swift has simplified its memory management with Automatic Reference Counting (ARC) and it is compatible with Objective-C. This book has been created to provide you with the information and skills ...

    swift开发文档中文版

    我们使用自动引用计数(Automatic Reference Counting, ARC)来简化内存管理。我们在 Foundation 和 Cocoa的基础上构建框架栈并将其标准化。Objective-C 本身支持块、集合语法和模块,所以框架可以轻松支持现代编程...

    Objective-C学习之ARC的实现方法

    自动引用计数(Automatic Reference Counting, ARC)把压在程序员们肩头的管理内存的重担卸除了不少,更不用说让跟踪内存泄漏那样的烦心事也少了很多。下面这篇文章主要给大家介绍了关于Objective-C学习之ARC的实现...

    Clang GNUstep Objective C for Windows

    所以,这个是GNUstep,再加上新一代的編譯器架構Clang與LLVM(這也是Apple在新版Xcode裡預設使用的編譯器),這樣就能有Objective-C 2.0的功能,以及block與ARC(automatic reference counting)。 問:想開發...

Global site tag (gtag.js) - Google Analytics