不要担心`retainCount`?真? [英] Don't worry about `retainCount`? Really?

查看:134
本文介绍了不要担心`retainCount`?真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知不要担心保留计数。我理解我不应该使用基于 retainCount的条件逻辑决定 release retain ,但我不应该担心吗?我认为这些在某种程度上与内存使用相对应。

I've been told to not worry about retain counts. I understand that I shouldn't decide to release or retain using conditional logic based on retainCount, but should I not worry about it? I thought these correspond to memory usage in some way.

例如,如果我有一堆 UIView 我还放入 NSArray 以便能够遍历它们,不是加倍保留计数因此记忆使用该应用程序?如果是这样,如果子视图是500 UIControl 实例,这是代价高昂还是微不足道的? 这假设我当然需要500个实例。

For instance, if I have a bunch of subviews of UIView that I've also put into an NSArray to be able to iterate through them, doesn't that double the retain count and therefore the memory use of the application? If so, is this costly or trivial, if the subviews are, say, 500 UIControl instances? This is assuming that I need the 500 instances, of course.

推荐答案

你不应该担心 retainCount 因为它通常是引用计数系统的误导性实现细节。您应该关注的是遵循适当的对象所有权政策。

You're not supposed to be worrying about retainCount because it's often a misleading implementation detail of the reference counting system. What you're supposed to concern yourself with is following proper object ownership policy.

我在Apple的文档中偶尔发布一次:

I post this once in a while from Apple's documentation:


重要:此方法在调试内存管理问题时通常没有价值。因为任何数量的框架对象可能保留了一个对象以保存对它的引用,而同时自动释放池可能在对象上保留任意数量的延迟版本,所以您不太可能从此获取有用信息方法。

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

至于你的上一个问题,从一个数组添加对象到另一个数组,你的内存使用率不会翻倍。保留计数只是对象中的无符号整数,当某些东西声称拥有它时,它会递增1。但同样,不要担心自己。

As for your last question, your memory usage is not going to double from adding objects from one array to another array. The retain count is just an unsigned integer in the object that gets incremented by 1 when something claims ownership of it. But again, don't concern yourself with that.

这篇关于不要担心`retainCount`?真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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