ARC时代的内存管理
什么是ARC
Automatic Reference Counting (ARC) is a compiler feature that provides automatic memory management of Objective-C objects. Rather than having to think about retain and release operations [^1]
[^1]: Transitioning to ARC Release Notes
ARC提供是一个编译器的特性,帮助我们在编译的时候自动插入管理引用计数的代码。
最重要的是我们要认识到ARC的本质仍然是通过引用计数来管理内存。因此有时候如果我们操作不当,仍然会有内存泄露的危险。下面就总结一下ARC时代可能出现内存泄露的场景。
内存泄露类型
1. 循环引用
基于引用计数的内存管理机制无法绕过的一个问题便是循环引用(retain cycle)
(Python同样也采用了基于引用计数的内存管理,但是它采用了另外的机制来清除引用循环导致的内存泄露,而OC和Swift需要我们自己来处理这样的问题[^2])
对象之间的循环引用:使用弱引用避免
block与对象之间的循环引用:
会导致Block与对象之间的循环引用的情况有:
1
|
self.myBlock = ^{ self.someProperty = XXX; }; |
对于这种Block与Self直接循环引用的情况,编译器会给出提示。
但是对于有多个对象参与的情况,编译器便无能为力了,因此涉及到block内使用到self的情况,我们需要非常谨慎。(推荐涉及到self的情况,如果自己不是非常清楚对象引用关系,统一使用解决方案处理)
1
2
|
someObject.someBlock = ^{ self.someProperty = XXX; }; //还没有循环引用 self.someObjectWithBlock = someObject; // 导致循环引用,且编译器不会提醒 |
解决方案:
1
2
3
4
5
6
7
8
9
|
__weak SomeObjectClass *weakSelf = self; SomeBlockType someBlock = ^{ SomeObjectClass *strongSelf = weakSelf; if (strongSelf == nil) { // The original self doesn't exist anymore. // Ignore, notify or otherwise handle this case. } [strongSelf someMethod]; }; |
我们还有一种更简便的方法来进行处理,实际原理与上面是一样的,但简化后的指令更易用。
1
2
3
4
5
6
7
8
9
|
@weakify(self) [self.context performBlock:^{ // Analog to strongSelf in previous code snippet. @strongify(self) // You can just reference self as you normally would. Hurray. NSError *error; [self.context save:&error]; // Do something }]; |
你可以在这里找到@weakify,@strongify工具:MyTools_iOS
[^2]: How does Python deal with retain cycles?
1. NSTimer
一般情况下在Action/Target模式里 target一般都是被weak引用,除了NSTimer。
1
2
3
4
5
|
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats |
NSTimer Class Reference指出NSTimer会强引用target。
target
The object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to this object until it (the timer) is invalidated.
然后官方的Timer Programming Topics指出: 我们不应该在dealloc中invalidate timer。
A timer maintains a strong reference to its target. This means that as long as a timer remains valid, its target will not be deallocated. As a corollary, this means that it does not make sense for a timer’s target to try to invalidate the timer in its dealloc
method—the dealloc method will not be invoked as long as the timer is valid.
举一个例子,我们让timer在我们的ViewController中不断调用handleTimer方法.
1
2
3
4
5
6
7
8
9
|
- (void)viewDidLoad { [ super viewDidload]; self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]; } |
这个时候,timer和我们的ViewController就是循环引用的。即使我们在dealloc方法中invalidate timer也是没用的。因为timer强引用着VC。而dealloc是在对象销毁的时候才会被调用。
可能有人会有疑惑,如果VC不强引用timer。会发生什么呢?
NSTimer Class Reference指出: Runloop会强引用tiemr。这是理所当然的,因为如果一个timer是循环的,如果没被强引用,那么在函数返回后(比如上面的viewDidLoad函数),则会被销毁。自然就不能不断循环地通知持有的target。
Note in particular that run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.
这个时候,Runloop, Timer和ViewController的关系是这样的。
因为main runloop 的生命周期跟应用的生命周期是一致的,所以如果我们不主动invalidate timer,runloop就会一直持有timer,而timer也一直持有ViewController。同样也造成了内存泄露。
因此在使用NSTimer时,特别是循环的NSTimer时。我们需要注意在什么地方invalidate计时器,在上面这个例子,我们可以在viewWillDisappear里面做这样的工作。
Swift's ARC
在Swift中,ARC的机制与Objective-C基本是一致的。
相对应的解决方案:
对象之间的循环引用:使用弱引用避免
1
2
3
4
|
protocol aProtocol:class{} class aClass{ weak var delegate:aProtocol? } |
注意到这里,aProtocol通过在继承列表中添加关键词class来限制协议只能被class类型所遵循。这也是为什么我们能够声明delegate为weak的原因,weak仅适用于引用类型。而在Swift,enum与struct这些值类型中也是可以遵循协议的。
闭包引起的循环引用:
Swift提供了一个叫closure capture list的解决方案。
语法很简单,就是在闭包的前面用[]声明一个捕获列表。
1
2
3
|
let closure = { [weak self] in self?.doSomething() //Remember, all weak variables are Optionals! } |
我们用一个实际的例子来介绍一下,比如我们常用的NotificationCenter:
1
2
3
4
5
6
7
8
9
10
11
|
class aClass{ var name:String init(name:String){ self.name = name NSNotificationCenter.defaultCenter().addObserverForName( "print" , object: self, queue: nil) { [weak self] notification in print( "hello \(self?.name)" )} } deinit{ NSNotificationCenter.defaultCenter().removeObserver(self) } } |
Swift的新东西
swift为我们引入了一个新的关键词unowned。这个关键词同样用来管理内存和避免引用循环,和weak一样,unowned不会导致引用计数+1。
1. 那么几时用weak,几时用unowned呢?
举上面Notification的例子来说:
如果Self在闭包被调用的时候有可能是Nil。则使用weak
如果Self在闭包被调用的时候永远不会是Nil。则使用unowned
2. 那么使用unowned有什么坏处呢?
如果我们没有确定好Self在闭包里调用的时候不会是Nil就使用了unowned。当闭包调用的时候,访问到声明为unowned的Self时。程序就会奔溃。这类似于访问了悬挂指针(进一步了解,请阅读Crash
in Cocoa)
对于熟悉Objective-C的大家来说,unowned在这里就类似于OC的unsafe_unretained。在对象被清除后,声明为weak的对象会置为nil,而声明为unowned的对象则不会。
3. 那么既然unowned可能会导致崩溃,为什么我们不全部都用weak来声明呢?
原因是使用unowned声明,我们能直接访问。而用weak声明的,我们需要unwarp后才能使用。并且直接访问在速度上也更快。(这位国外的猿说:Unowned is faster and allows for immutability and nonoptionality. If you don't need weak,
don't use it.)
其实说到底,unowned的引入是因为Swift的Optional机制。
因此我们可以根据实际情况来选择使用weak还是unowned。个人建议,如果无法确定声明对象在闭包调用的时候永远不会是nil,还是使用weak来声明。安全更重要。
延伸阅读:从Objective-C到Swift
参考链接:
shall-we-always-use-unowned-self-inside-closure-in-swif
what-is-the-difference-between-a-weak-reference-and-an-unowned-reference
ARC时代的内存管理的更多相关文章
- 【转】iOS夯实:ARC时代的内存管理
iOS夯实:ARC时代的内存管理 什么是ARC Automatic Reference Counting (ARC) is a compiler feature that provides autom ...
- iOS夯实:ARC时代的内存管理
iOS夯实:ARC时代的内存管理 文章转自 ARC时代的内存管理 什么是ARC Automatic Reference Counting (ARC) is a compiler feature tha ...
- ARC下的内存管理
1.ARC下单对象内存管理 局部变量释放对象随之被释放 int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = ...
- ARC机制集合内存管理
// // main.m // 13-ARC机制集合内存管理 // // Created by apple on 14-3-21. // Copyright (c) 2014年 apple. ...
- objective-c启用ARC时的内存管理 (循环引用)
PDF版下载:http://download.csdn.net/detail/cuibo1123/7443125 在Objective-C中,内存的引用计数一直是一个让人比较头疼的问 ...
- iOS- 非ARC的项目内存管理细节详解(实战)
1.前言 接上文:iOS- 如何将非ARC的项目转换成ARC项目(实战) 2.内存管理时相关的配置 当我们把将非ARC的内存管理都管理好后,发现在做有些操作的时候内存还是在一直的缓慢增加 比如做一个最 ...
- objective-c启用ARC时的内存管理
PDF版下载:http://download.csdn.net/detail/cuibo1123/7443125 在objective-c中,内存的引用计数一直是一个让人比較头疼的问题.尤其 ...
- iOS夯实:内存管理
iOS夯实:内存管理 文章转自 内存管理 最近的学习计划是将iOS的机制原理好好重新打磨学习一下,总结和加入自己的思考. 有不正确的地方,多多指正. 目录: 基本信息 旧时代的细节 新时代 基本信息 ...
- iOS ARC内存管理
iOS的内存管理机制,只要是iOS开发者,不管多长的时间经验,都能说出来一点,但是要深入的理解.还是不简单的.随着ARC(自动管理内存)的流行.iOS开发者告别了手动管理内存的复杂工作.但是自动管理内 ...
随机推荐
- oracle11实战详解
因为最近可以学习oracle了,所以昨天把oracle装好了,装的时候因为种种不知名的原因,把我的mysql居然连连不上了.说实话如果自己看教程安装的话还是有一点小复杂的,特别是对于我这种学渣来说,我 ...
- Scala:集合类型Collection和迭代器
http://blog.csdn.net/pipisorry/article/details/52902549 Scala Collection Scala 集合分为可变的和不可变的集合. 可变集合可 ...
- Android 6.0出现的init: cannot execve(‘XXX’):Permission denied问题:禁止SELINUX的权限设置
最近在开发MTK的相关项目,需要将一些可执行文件添加到init.rc文件里去,但是开机后发现,这个bin文件没有权限不能执行,于是我就在init.rc中对相应的bin文件增加了权限.后来发现,改了也没 ...
- 长度为N的数组乱序存放着0带N-1.现在只能进行0与其他数的swap操作,请设计并实现排序,必须通过交换实现排序。
void sort(int* arr, int len) { if (!arr) { return; } for (int i = 1; i < len; ++i) { while (arr[0 ...
- For oracle databases, if the top showing the oracle database, then oracle process is using the top c
Note 805586.1 Troubleshooting Session Administration (Doc ID 805586.1)Note 822527.1 How To Find ...
- Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》
昨晚写博客太仓促,代码结构有问题,早上测试发现没法监听文本变化!今日更改一下.真心见谅啦,哈哈!主活动的代码已经改好了,看截图这次的确实现了文本监听变化情况. 监听文本输入情况,仅仅限于土司略显low ...
- GCD API 记录 (三)
本篇就不废话啦,接着上篇记录我见过或者使用过的与GCD相关的API.由于一些API使用的非常少,用过之后难免会忘记,还是记录一下比较好. 6.dispatch_group_wait 该API依然是与d ...
- 剑指Offer——网易笔试之不要二——欧式距离的典型应用
剑指Offer--网易笔试之不要二--欧式距离的典型应用 前言 欧几里得度量(euclidean metric)(也称欧氏距离)是一个通常采用的距离定义,指在m维空间中两个点之间的真实距离,或者向量的 ...
- Java基础---Java---IO流-----LineNumberReader方法及原理、自定义一个LineNumberReader、字节流、图片复制、mp3复制、
LineNumberReader 跟综行号的缓冲字符输入流,些类定义了setLineNumber(int)和getLineNumber(int),它们可分别用于设置和获取当前行号 import jav ...
- CONFIGURE ADFS 3.0 WITH SHAREPOINT 2013
http://blogit.create.pt/miguelmoreno/2014/11/14/configure-adfs-3-0-with-sharepoint-2013/