Objective-c的内存管理MRC与ARC

 

Objective-c中提供了两种内存管理机制MRC(MannulReference Counting)和ARC(Automatic Reference Counting),分别提供对内存的手动和自动管理,来满足不同的需求。注意的是Xcode 4.1及其以前版本没有ARC,MRC与ARC的区别如图1所示。需要理解MRC,但实际使用时强推ARC。

图1  MRC与ARC区别示意图

 

1. Objective-c语言中的MRC(MannulReference Counting)

 

在MRC的内存管理模式下,与对变量的管理相关的方法有:retain,release和autorelease。retain和release方法操作的是引用记数,当引用记数为零时,便自动释放内存。并且可以用NSAutoreleasePool对象,对加入自动释放池(autorelease调用)的变量进行管理,当drain时回收内存。

(1)      retain,该方法的作用是将内存数据的所有权附给另一指针变量,引用数加1,即retainCount+= 1;

(2)      release,该方法是释放指针变量对内存数据的所有权,引用数减1,即retainCount-= 1;

(3)      autorelease,该方法是将该对象内存的管理放到autoreleasepool中。

示例代码:

//假设Number为预定义的类

Number* num = [[Number alloc] init];

Number* num2 = [num retain];//此时引用记数+1,现为2

[num2 release]; //num2 释放对内存数据的所有权 引用记数-1,现为1;

[num release];//num释放对内存数据的所有权 引用记数-1,现为0;

[num add:1 and 2];//bug,此时内存已释放。

//autoreleasepool 的使用 在MRC管理模式下,我们摒弃以前的用法,NSAutoreleasePool对象的使用,新手段为@autoreleasepool

@autoreleasepool {

Number* num = [[Number alloc] init];

[numautorelease];//由autoreleasepool来管理其内存的释放

}

对与Objective-c中属性的标识符可以总结为:

@property (nonatomic/atomic,retain/assign/copy, readonly/readwrite) Number* num;

(1)      nonatomic/atomic,表示该属性是否是对多线程安全的,是不是使用线程锁,默认为atomic,

(2)      retain/assign/copy,是有关对该属性的内存管理的,

l   assign"is the default. In the setter that is created by @synthesize, the value willsimply be assigned to the attribute, don’t operate the retain count. Myunderstanding is that "assign" should be used for non-pointer attributes.

l   "retain"is needed when the attribute is a pointer to an object. The setter generated by@synthesize will retain (aka add a retain count) the object. You will need torelease the object when you are finished with it.

l   "copy"is needed when the object is mutable. Use this if you need the value of theobject as it is at this moment, and you don't want that value to reflect anychanges made by other owners of the object. You will need to release the objectwhen you are finished with it because you are retaining the copy.

(3)      readwrite /readonly -"readwrite" is the default. When you @synthesize, both a getter and asetter will be created for you. If you use "readonly", no setter willbe created. Use it for a value you don't want to ever change after the instantiationof the object.

2. Objective-c语言中的ARC(AutomaticReference Counting)

在ARC中与内存管理有关的标识符,可以分为变量标识符和属性标识符,对于变量默认为__strong,而对于属性默认为unsafe_unretained。也存在autoreleasepool。

对于变量的标识符有:

(1) __strong,is the default. An object remains “alive” as long as there is a strong pointerto it.

(2) __weak,specifies a reference that does not keep the referenced object alive. A weakreference is set to nil when there are no strong references to the object.

(3)__unsafe_unretained,specifies a reference that does not keep the referenced object alive and is notset to nil when there are no strong references to the object. If the object itreferences is deallocated, the pointer is left dangling.

(4)__autoreleasing,is used to denote arguments that are passed by reference (id *) and areautoreleased on return,managedby Autoreleasepool.

 

对于变量标识符的用法:

__strong Number* num = [[Number alloc]init];

 

在ARC内存管理模式下,其属性的标识符存在以下几种:

@property (nonatomic/atomic, assign/retain/strong/weak/unsafe_unretained/copy,readonly/readwrite) Number* num;//默认为unsafe_unretained

其中assign/retain/copy与MRC下property的标识符意义相同,strong类似与retain,assign类似于unsafe_unretained,strong/weak/unsafe_unretained与ARC下变量标识符意义相同,只是一个用于属性的标识,一个用于变量的标识(带两个下划短线__)。所列出的其他的标识符与MRC下意义相同。

(1)对于assign,你可以对标量类型(如int)使用这个属性。你可以想象一个float,它不是一个对象,所以它不能retain、copy。

(2)对于copy,指定应该使用对象的副本(深度复制),前一个值发送一条release消息。基本上像retain,但是没有增加引用计数,是分配一块新的内存来放置它。特别适用于NSString,如果你不想改变现有的,就用这个,因为NSMutableString,也是NSString。

对于Core Foundation与objective-cObject进行交换时,需要用到的ARC管理机制有:

(1) (__bridge_transfer<NSType>) op oralternatively CFBridgingRelease(op) isused to consume a retain-count of a CFTypeRef whiletransferring it over to ARC. This could also be represented by id someObj =(__bridge <NSType>) op; CFRelease(op);

(2) (__bridge_retained<CFType>) op oralternatively CFBridgingRetain(op) isused to hand an NSObject overto CF-land while giving it a +1 retain count. You should handle a CFTypeRefyoucreate this way the same as you would handle a result of CFStringCreateCopy().This could also be represented by CFRetain((__bridge CFType)op); CFTypeRef someTypeRef =(__bridge CFType)op;

(3) __bridge justcasts between pointer-land and Objective-C object-land. If you have noinclination to use the conversions above, use this one.

Objective-c的内存管理MRC与ARC的更多相关文章

  1. iOS-旧项目中手动内存管理(MRC)转ARC

    在ARC之前,iOS内存管理无论对资深级还是菜鸟级开发者来说都是一件很头疼的事.我参 加过几个使用手动内存管理的项目,印象最深刻的是一个地图类应用,由于应用本身就非常耗内存,当时为了解决内存泄露问题, ...

  2. 内存管理-MRC与ARC详解

    Objective-C提供了两种内存管理机制MRC(Mannul Reference Counting)和ARC(Automatic Reference Counting),为Objective-C提 ...

  3. OC内存管理(MRC)

    首先说明一下几块存储区域: 栈区(局部变量.函数参数值) 堆区(对象.手动申请/释放内存) BSS区(未初始化的全局变量.未初始化的静态数据) 常量区(字符串常量以及初始化后的全局变量.初始化后的静态 ...

  4. OC 内存管理之手动内存管理MRC

    一.基本原理 1.什么是内存管理 内存管理的重要性: 移动设备的内存极其有限,每个app所能占用的内存是有限制的 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间. ...

  5. 内存管理-MRC

    MRC内存管理 环境:先关闭arc模式,选中项目->build Settings

  6. object-C 手动内存管理(MRC)

    object-C的内存管理和javascript的垃圾回收不一样,今天总结下手动内存管理,ARC的后边补上. 1:基本铺垫 oc采用引用计数来表示对象的状态,比如通过init创建出来的一个对象引用计数 ...

  7. OC内存管理、非ARC机制、MRR机制

    在Xcode里面,默认为ARC(auto reference counting),也就是自动内存管理机制,在这里我们要了解的是内存管理,肯定是不能让系统帮我们管理内存,我们需要将ARC关闭,首先在左边 ...

  8. 【iOS开发-33】学习手动内存管理临时抛弃ARC以及retain/assign知识——iOSproject师面试必考内容

    我们为什么须要内存管理?当使用内存达到40M和45M时候会发出警告,假设不处理,占用内存达到120M时直接强制关闭程序. 所以出现闪退除了是程序出现逻辑错误,还有可能是内存使用过大. (1)创建一个对 ...

  9. 7内存管理-MRC

    @0简介 内存管理,即内存里各个对象的管理,即内存里各个对象的生命周期的管理,(从面向对象的角度看) @1引用计数器 默认为1,即有一滴的生命血液,若为0就会死去 @2单个对象的管理 自己管理自己,自 ...

随机推荐

  1. [Tools] Eclipse更改类注释自动生成模板

    [背景] 使用之中发现一些eclipse使用的小技巧,记录下来供以后查阅   由于机器是老婆的,创建新类的时候或者生成注释的时候全都是她的名字,避免弄混,需要设置一下: 设置创建新类时自动生成类或方法 ...

  2. PMP 第十一章 项目风险管理

    1规划风险管理 2识别风险 3 风险定性分析 4风险定量分析 5规划风险应对 6监控风险 1.项目风险是什么?已知未知风险.未知未知风险对应应急储备和管理储备的关系.风险承受力和风险偏好是什么? 2. ...

  3. 遍历Map

    Map map = new HashMap(); map.put("1", "value1"); map.put("2", "va ...

  4. linux下epoll如何实现高效处理百万句柄的

    linux下epoll如何实现高效处理百万句柄的 分类: linux 技术分享 2012-01-06 10:29 4447人阅读 评论(5) 收藏 举报 linuxsocketcachestructl ...

  5. 【T_SQL】 基础

    一.T-SQL 的组成 1.DML(数据操作语言 Data Manipulation Language)               查询.插入.删除和修改数据库中的数据.SELECT.INSERT. ...

  6. snakeyaml - Documentation.wiki

    SnakeYAML Documentation This documentation is very brief and incomplete. Feel free to fix or improve ...

  7. java 大数计算

    这几天做了几道用大数的题,发现java来做大数运算十分方便.对acmer来说是十分实用的 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigInteger ...

  8. 记一次小团队Git实践(中)

    对于初学者,从使用上先入手,往往学的最快,并从中汲取教训,再回头更深入的学习,效果尤佳. 安装git 安装git自不必说,mac已经内置了git,linux下一个命令就能搞定,windows下需要下载 ...

  9. 仓库如何盘点 打印扫描一体PDA盘点机提升库存盘点效率

    仓库盘点是对仓储货品的收发结存等活动进行有效控制,保证仓储货品完好无损.帐物相符,确保生产正常进行,规范公司物料的盘点作业.盘点需人工操作,费时费力,PDA盘点机的出现大幅提升了盘点效率,减轻了工作人 ...

  10. footer元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...