ARC中的@autoreleasepool还有作用吗?
ARC中的@autoreleasepool还有作用吗?

QUESTION
For the most part with ARC (Automatic Reference Counting), we don't need to think about memory management at all with Objective-C objects. It is not permitted to create NSAutoreleasePools anymore, however there is a new syntax:
在ARC中,我们不再需要手动管理内存.ARC已经不允许你创建NSAutoreleasePools了,然而有一种新的语法格式:
@autoreleasepool{…}
My question is, why would I ever need this when I'm not supposed to be manually releasing/autoreleasing ?
我的问题是,为什么@autoreleasepool 可以在ARC中使用而不出错呢?
ANSWER
There seems to be a lot of confusion on this topic (and at least 80 people who probably are now confused about this and think they need to sprinkle @autoreleasepool around their code).
这个主题看起来相当迷惑人(至少80人搞不清楚状况,或许他们会将@autoreleasepool从他们的代码中移除掉)
If a project (including its dependencies) exclusively uses ARC, then @autoreleasepool never needs to be used and will do nothing useful. ARC will handle releasing objects at the correct time. For example:
如果一个工程完全由ARC编译(包括它依赖的工程项目),那么@autoreleasepool 完全没有用处,ARC会自己管理好对象的内存释放问题,例如:
@interfaceTesting:NSObject+(void) test;
@end
@implementation Testing
-(void) dealloc
{
NSLog(@"dealloc");
}
+(void) test
{
while(true)
NSLog(@"p = %p",[Testing new]);
}
@end
displays:
p =0x17696f80
dealloc
p =0x17570a90
dealloc
Each Testing object is deallocated as soon as the value goes out of scope, without waiting for an autorelease pool to be exited. (The same thing happens with the NSNumber example; this just lets us observe the dealloc.) ARC does not use autorelease.
每一个Testing对象,在出了值的作用域之后都会被立马释放掉了,完全不需要有一个 autorelease 存在,ARC中使用autorelease完全没有用处.
The reason @autoreleasepool is still allowed is for mixed ARC and non-ARC projects, which haven't yet completely transitioned to ARC.
你之所以可以继续使用@autoreleasepool 的原因是因为如果你的工程是ARC与MRC混合的,工程中并没有完全的转换成ARC而已.
If you call into non-ARC code, it may return an autoreleased object. In that case, the above loop would leak, since the current autorelease pool will never be exited. That's where you'd want to put an @autoreleasepool around the code block.
如果你在MRC代码中调用了,它会给你返回一个autoreleased 的对象.在那种情形中,上面的循环就会泄露,所以,为了兼容MRC,你需要给你的相关代码添加@autoreleasepool.
But if you've completely made the ARC transition, then forget about autoreleasepool.
如果你的项目完全由ARC编译的,那么请忘掉这个autoreleasepool :).
ARC中的@autoreleasepool还有作用吗?的更多相关文章
- @autoreleasepool在MRC和ARC中的区别
对于@autoreleasepool {} (1)在ARC中会销毁所有在里面创建的对象,即使你用外面的Strong指针指向他 (2)在MRC中如果有外部的强指针指向,不会销毁对象,retainCoun ...
- 如何实现ARC中weak功能?
原文链接 我们都知道ARC中weak与assign或者说unsafe_unretained最大的不同就是设置weak属性后,系统会在对象被释放后自动将指向对象的指针置为nil,而assign则会产生一 ...
- block使用小结、在arc中使用block、如何防止循环引用
引言 使用block已经有一段时间了,感觉自己了解的还行,但是几天前看到CocoaChina上一个关于block的小测试主题: [小测试]你真的知道blocks在Objective-C中是怎么工作的吗 ...
- SQLSERVER中NULL位图的作用
SQLSERVER中NULL位图的作用 首先感谢宋沄剑提供的文章和sqlskill网站:www.sqlskills.com,看下面文章之前请先看一下下面两篇文章 SQL Server误区30日谈-Da ...
- PHP中的header()函数作用
PHP 中 header()函数的作用是给客户端发送头信息. 什么是头信息?这里只作简单解释,详细的自己看http协议.在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(h ...
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- ARC中KVO开发注意
1 在ARC 中 KVO开发 添加监听和去掉监听必需 一一匹配,不要有过的去掉监听否则会有可能导致对象无法释放. 例如,在一个viewcontroller中添加webview 并监听webview的c ...
- log4net日志在app.config中assembly不起作用
log4net 1.2.15.0日志在app.config中assembly不起作用,必须 1.手动调用方法log4net.Config.XmlConfigurator.Configure()来初始化 ...
- URL中“#” “?” &“”号的作用
URL中"#" "?" &""号的作用 阅读目录 1. # 2. ? 3. & 回到顶部 1. # 10年9月,twit ...
随机推荐
- 720P、1080P、4K是什么意思?
什么是像素? 中文全称为图像元素.像素仅仅只是分辨率的尺寸单位,而不是画质. 从定义上来看,像素是指基本原色素及其灰度的基本编码. 像素是构成数码影像的基本单元,通常以像素每英寸PPI(pixels ...
- hdu 5894(组合数取模)
hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- wordpress后台加载速度异常缓慢排查记录(原创)
原因在于在function.php函数中加入了下面的代码导致了缓慢: //停用版本更新通知remove_action('load-update-core.php', 'wp_update_themes ...
- Android Webview中解决H5的音视频不能自动播放的问题
在开发webview的时候,当加载有声音的网页的时候,声音不会自动播放, 解决方法:在webview中调用js方法.这个方法需要在webview的setWebViewClient方法之后在onPage ...
- Hadoop案例(六)小文件处理(自定义InputFormat)
小文件处理(自定义InputFormat) 1.需求分析 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案.将多个小文件合并 ...
- Linux下Diff命令
一般正常比较两个文件用vimdiff,算是直接进入vim界面,如果比较两个文件夹下面的文件,可以用diff,注意,这里只会比较文件夹下面的同名文件,他会列出不一样的点. 参考Linux下Diff命令使 ...
- lr如何屏蔽全局变量的影响
首先要熟悉C语言的全局变量和局部变量的含义: C语言中的变量详解 先说说变量的作用域,比如,在函数中,形参变量只是在被调用期间才分配内存单元,调用结束立即释放.这就说明形参变量只有在函授内才是有效的, ...
- 阿里云ecs 服务器配置
阿里云ecs 7.0+安装mysql 5.6 http://jingyan.baidu.com/article/454316ab67bd02f7a7c03af4.html 安装jdk yum -y i ...
- HTTP协议的重新学习
思论:做互联网一年多了,想了想对http协议的认识还处于很笼统的阶段,抽休息时间,重新梳理一下自己的网络知识. 1.什么叫HTTP协议? HTTP协议是Hyper TEXT Transfer Prot ...
- angular4 动态Form中获取表单字段并在页面中使用的方法
主要有两种方式 第一种 使用get属性 页面中使用如下: 第二种 使用普通方法事件 页面中使用如下 *转载请附出处