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还有作用吗?的更多相关文章

  1. @autoreleasepool在MRC和ARC中的区别

    对于@autoreleasepool {} (1)在ARC中会销毁所有在里面创建的对象,即使你用外面的Strong指针指向他 (2)在MRC中如果有外部的强指针指向,不会销毁对象,retainCoun ...

  2. 如何实现ARC中weak功能?

    原文链接 我们都知道ARC中weak与assign或者说unsafe_unretained最大的不同就是设置weak属性后,系统会在对象被释放后自动将指向对象的指针置为nil,而assign则会产生一 ...

  3. block使用小结、在arc中使用block、如何防止循环引用

    引言 使用block已经有一段时间了,感觉自己了解的还行,但是几天前看到CocoaChina上一个关于block的小测试主题: [小测试]你真的知道blocks在Objective-C中是怎么工作的吗 ...

  4. SQLSERVER中NULL位图的作用

    SQLSERVER中NULL位图的作用 首先感谢宋沄剑提供的文章和sqlskill网站:www.sqlskills.com,看下面文章之前请先看一下下面两篇文章 SQL Server误区30日谈-Da ...

  5. PHP中的header()函数作用

    PHP 中 header()函数的作用是给客户端发送头信息. 什么是头信息?这里只作简单解释,详细的自己看http协议.在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(h ...

  6. 浅析python 中__name__ = '__main__' 的作用

    引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...

  7. ARC中KVO开发注意

    1 在ARC 中 KVO开发 添加监听和去掉监听必需 一一匹配,不要有过的去掉监听否则会有可能导致对象无法释放. 例如,在一个viewcontroller中添加webview 并监听webview的c ...

  8. log4net日志在app.config中assembly不起作用

    log4net 1.2.15.0日志在app.config中assembly不起作用,必须 1.手动调用方法log4net.Config.XmlConfigurator.Configure()来初始化 ...

  9. URL中“#” “?” &“”号的作用

    URL中"#" "?" &""号的作用   阅读目录 1. # 2. ? 3. & 回到顶部 1. # 10年9月,twit ...

随机推荐

  1. springboot 集合 meshsite3

    工程目录 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  2. [实战]MVC5+EF6+MySql企业网盘实战(18)——文件上传,下载,修改

    写在前面 经过一段时间的秀秀改改,终于把文件上传下载,修改文件夹文件名称的功能实现了. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企 ...

  3. chrome浏览器插件开发经验(一)

    最近在进行chrome浏览器插件的开发,一些小的经验总结随笔. 1.首先,推荐360的chrome插件开发文档:http://open.chrome.360.cn/extension_dev/over ...

  4. windows下怎样使用md命令一次建立多级子目录

    在Windows系统中一次只能够创建一个子目录,在命令提示符窗口则可以一次性创建多个子目录,例如如果想在f盘创建多级子目录,则md 23\13\65\45,后面的数字随便都可以.如果想一次性删除多级目 ...

  5. Markdown 格式如何转换成 Word?

    参考资料:https://www.zhihu.com/question/22972843/answer/136008865 markdown语法简洁,写作效率极高,非常适合网络博客.邮件.笔记等非正式 ...

  6. 使用Jedis

    前言 借助Jedis可以在Java上操作Redis. Jedis 到https://mvnrepository.com/去找jar包下载即可. 如果是maven项目: <!-- https:// ...

  7. UVA11107 Life Forms --- 后缀数组

    UVA11107 Life Forms 题目描述: 求出出现在一半以上的字符串内的最长字符串. 数据范围: \(\sum len(string) <= 10^{5}\) 非常坑的题目. 思路非常 ...

  8. 【20181023T3】“新”的家园【虚图】

    打死也不告诉你这个名字是我编的 题面 [错解] 哎最短路欸 哎floyd+dijkstra有30分 骗分骗分 [正解] 我们发现n和m(不是E)不是一个数量级的 也就是说,在做传统最短路的时候,很多时 ...

  9. vmware10上三台虚拟机的Hadoop2.5.1集群搭建

    由于官方版本的Hadoop是32位,若在64位Linux上安装,则必须先重新在64位环境下编译Hadoop源代码.本环境采用编译后的hadoop2.5.1 . 安装参考博客: 1 http://www ...

  10. Tagging Problems & Hidden Markov Models---NLP学习笔记(原创)

    本栏目来源于对Coursera 在线课程 NLP(by Michael Collins)的理解.课程链接为:https://class.coursera.org/nlangp-001 1. Taggi ...