Objective-c的内存管理MRC与ARC

转自:http://blog.csdn.net/fightingbull/article/details/8098133


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.

 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: "Xcode的Edit菜单的To Objective-C ARC"是一个选项,它可以将项目从手动管理内存转换为自动引用计数(ARC)模式。在ARC模式下,编译器会自动插入内存管理代码,从而减少了程序员需要手动管理内存的工作量,使代码更加简洁和易于维护。 ### 回答2: Xcode的Edit的"Convert to Objective-C ARC"是一个功能,用于将项目的手动引用计数(Manual Reference Counting,简称MRC)代码转换为自动引用计数(Automatic Reference Counting,简称ARC)代码。 在早期的iOS开发开发者需要手动管理内存,使用retain、release和autorelease等方法来管理对象的内存生命周期。这种方式需要开发者花费大量的时间和精力来确保内存管理的正确性,容易出现内存泄露和野指针等问题。 ARC的出现极大地简化了内存管理的过程,开发者不再需要手动管理引用计数,编译器会自动在合适的时候插入retain、release和autorelease等代码。这样可以大大减少内存管理相关的bug,并提高开发效率。 "Convert to Objective-C ARC"功能可以帮助开发者将项目MRC代码转换为ARC代码。使用该功能,开发者可以选择要转换的文件或者整个工程,Xcode会自动检测代码的引用计数,然后自动插入正确的ARC相关代码。在转换完成后,开发者可以更加方便地进行内存管理,减少人为的错误。 总之,Xcode的Edit的"Convert to Objective-C ARC"功能是为了帮助开发者将手动管理引用计数的代码转换为自动管理引用计数的代码,简化内存管理过程,提高开发效率。 ### 回答3: Xcode的Edit功能包括了许多实用的工具和选项,其之一就是"Convert to Objective-C ARC"(转换为Objective-C自动引用计数)。 Objective-C ARC是一种自动内存管理机制,它可以自动处理内存管理,减少了开发者手动管理内存的工作量。这种机制通过跟踪对象的引用计数,并自动释放不再需要的对象来实现。在ARC下,开发者无需手动调用retain、release,以及autorelease等方法来管理内存。 "Convert to Objective-C ARC"是一个非常有用的功能,它可以将项目使用手动引用计数(MRC)的代码转换为使用自动引用计数(ARC)的代码。这个功能简化了开发者的工作,提高了代码的可维护性和性能。转换后的代码可以通过简单地点击几个按钮完成自动迁移,不需要手动修改大量的代码。 使用"Convert to Objective-C ARC"功能的步骤如下: 1. 在Xcode打开需要转换的工程。 2. 点击Xcode菜单栏的"Edit"选项。 3. 在下拉菜单选择"Convert"。 4. 在弹出的菜单选择"To Objective-C ARC"。 5. Xcode将会自动对项目的源代码进行分析,找出需要进行转换的部分。 6. 在转换窗口,选择需要转换的文件和目标。 7. 点击"Finish"按钮开始进行转换。 在转换过程,Xcode会自动对代码进行检查和修改,将使用MRC的代码转换为ARC的代码。转换完成后,开发者可以自行检查转换的结果,并进行必要的手动修复。 总之,"Convert to Objective-C ARC"是Xcode强大的编辑工具之一,它能够帮助开发者轻松地将使用手动引用计数的代码转换为使用自动引用计数的代码,提高开发效率和代码质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值