自动发布:NARC使用后始终使用吗? [英] Autorelease: Use always when you've NARC'ed?

查看:67
本文介绍了自动发布:NARC使用后始终使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题看起来很愚蠢:我检查了但不是

在谈论NARC时,此博客的作者说, 就我个人而言,我希望立即在同一行自动释放由NARC编辑的所有内容."这与我在Apple网站和书籍中看到的所有示例完全相反,在这些示例中,仅当必须返回对象且不能立即释放对象时才使用自动释放.通常,我们的想法是autorelease占用大量内存,并且可能会使您的程序陷入困境(尽管它使代码更清晰).从 Appress开始iPhone 3开发:

In talking about NARC, the author of this blog says, "Personally, I like to immediately autorelease anything I NARC-ed, on the same line." This goes completely counter to all the examples I've seen on the Apple site and in books, where autorelease is only used when the object must be returned and cannot be released immediately. In general, the idea is that autorelease is memory intensive and can gum up your program (though it makes code cleaner). From Appress Beginning iPhone 3 Development:

这些对象可能有害 对应用程序内存的影响 脚印.可以使用 自动发布,但尝试仅使用它 当您真正需要时,而不仅仅是 保存键入一两行代码.

these objects can have a detrimental effect on your application’s memory footprint. It is OK to use autorelease, but try to use it only when you really need to, not just to save typing a line or two of code.

我不是在问自动发布是否比显式调用发布更糟糕,而是:

I am not asking if autorelease is worse than explicitly calling release (it is), but rather:


我的猜测是,与使用垃圾收集器(如MonoTouch应用程序已成功完成)相比,自动释放几乎不会减少您的内存占用,文森特的建议也很正确,并且可以使代码更简洁,减少意外的内存泄漏


My guess is that, compared to using a garbage collector (as MonoTouch apps do sucessfully), autorelease will hardly make a dent in your memory footprint, and that Vincent's advice it right on, and can make for cleaner code and less accidental memory-leaks.

推荐答案

您应该知道自动发布的工作原理.应用程序中的每个线程通常都有一个自动释放池.可以在池中注册对象.在注册它们时,池将确定它们所属的堆栈帧,并且只要剩下该堆栈帧,就会自动从池中弹出该堆栈帧.

You should be aware how autorelease works. Each thread in your application normally has a single autorelease pool. Objects can be registered in the pool. At the time they are registered, the pool determines the stackframe they belong to and will automatically pop the from the pool whenever that stackframe is left.

虽然这看起来可能很昂贵(并且可以肯定与直接保留/释放相比),但我认为它甚至没有接近世代标记和清扫垃圾收集器所能拥有的成本.

While this may seem costly (and it certainly is compared to direct retain/release), I don't think it even close to the cost a generation mark and sweep garbage collector can have.

在所有可能引发异常且没有尝试/追赶的情况下,自动释放真正发挥作用的地方.在这种情况下,自动发布绝对比直接发布更可取.

Where autorelease really shines is in all situations where exceptions may be raised and there's no try/catch around. Autorelease is definitely preferable to a direct release in such cases.

但是,在某些情况下,您应该避免自动释放(对于垃圾收集环境,您也应该尝试避免自动释放情况也是如此).在运行大量次的循环中创建临时的,自动释放的对象就是这种情况,这给垃圾收集器或自动释放池带来了巨大压力.

There are, however, situations where you should avoid autorelease (the same goes for garabge collected environments where you should try to avoid these situations too). Creating temporary, autoreleased objects in a loop which runs a huge number of times is such a scenario, which puts significant stress on a garbage collector or the autorelease pool.

在非常简单的工作线程中应该避免用自动释放替换释放,并且可以在没有自动释放池开销的情况下生存.因此,指导原则是:只要有可能避免使用它,就应该在不确定自动释放时使用.

Replacing release with autorelease should be avoided in worker threads that are very simple and can live without the overhead of an autorelease pool. So the guideline is: Whenever you can avoid it, you should, whenever you're unsure autorelease.

这篇关于自动发布:NARC使用后始终使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆