在Cocoa中使用autorelease的成本是多少? [英] What is the cost of using autorelease in Cocoa?

查看:133
本文介绍了在Cocoa中使用autorelease的成本是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数Apples文档似乎避免使用自动释放对象,特别是在创建gui视图时,但我想知道使用自动释放对象的成本是什么?

Most of Apples documentation seems to avoid using autoreleased objects especially when creating gui views, but I want to know what the cost of using autoreleased objects is?

UIScrollView *timeline = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 34)];
[self addSubview:timeline];
[timeline release];

最终我应该使用一个策略,其中一切都被自动释放,并使用retain / release应该是规则的具体情况?或者我应该一般使用retain / release与autorelease是从方便的方法如[NSString stringWithEtc ...]的返回对象的异常?

Ultimately should I use a strategy where everything is autoreleased and using retain/release should be the exception to the rule for specific cases? Or should I generally be using retain/release with autorelease being the exception for returned objects from convenience methods like [NSString stringWithEtc...] ?

推荐答案

有两种费用:


  1. (假设您有一个选项可以避免自动释放的对象)你的对象的生命周期。这可能意味着您的内存占用增加 - 不必要。在受限平台上,这意味着如果应用程序超出限制,则会终止该应用程序。

  1. (Assuming you have an option to avoid autoreleased objects.) You effectively unnecessarily extend the lifetime of your objects. This can mean that your memory footprint grows -- unnecessarily. On a constrained platform, this can mean that your application is terminated if it exceeds a limit. Even if you don't exceed a limit, it may cause your system to start swapping, which is very inefficient.

找到当前自动发布的额外开销(即使不超过限制),可能会导致系统开始交换,这是非常低效的。池,将自动释放的对象添加到它,然后释放对象(一个额外的方法调用)。

The additional overhead of finding the current autorelease pool, adding the autoreleased object to it, and then releasing the object at the end (an extra method call). This may not be a large overhead, but it can add up.

在任何平台上的最佳做法是尝试避免自动释放。

Best practice on any platform is to try to avoid autorelease if you can.

要回答问题:


我使用一个策略,其中一切都是自动释放和使用保留/释放应该是特定情况下的规则的例外?

Ultimately should I use a strategy where everything is autoreleased and using retain/release should be the exception to the rule for specific cases?


或者我应该一般使用retain / release与autorelease作为返回的对象从方便的方法例如[NSString stringWithEtc ... ]?

Or should I generally be using retain/release with autorelease being the exception for returned objects from convenience methods like [NSString stringWithEtc...] ?

您应该总是使用retain / release c $ c> NSString 通常不需要使用 stringWithEtc 方法,因为有 initWithEtc 等效。

You should always use retain/release if you can -- in the case of NSString there is typically no need to use stringWithEtc methods as there are initWithEtc equivalents.

另请参阅这个问题

这篇关于在Cocoa中使用autorelease的成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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