开发者

Don't worry about `retainCount`? Really?

开发者 https://www.devze.com 2023-01-09 00:58 出处:网络
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

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.

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.


The value returned by retainCount is the absolute number of times the object has been retained. A UIView comes from a framework whose implementation is opaque. The implementation details aren't something you should worry about save for the documented interface you interact with.

And within that implementation, instances of UIView may be retained any number of times as a part of the implementation. In terms of memory, the actual number of retains is meaningless; 1 is the same as 5.

The only thing you should ever be concerned with is how your code changes an objects retain count.

If your code increases the retain count, it must decrease it somewhere or the object will stick around forever. If you retain, you must release (or autorelease). If you copy, you must release or autorelease. If you new or alloc, you must release (or autorelease).

That is it.


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.

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.

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.


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...

Yes, it will.

... and therefore the memory use of the application?

No! The conclusion is wrong. 1000000 takes as much space as 0 when stored as a 32-bit integer.


The reason they say not to worry is that the retainCount field can often be very misleading. Aside from not knowing when the autorelease pool was last flushed or how many times an object has been autoreleased since, there are also some complicated internals, and system components could be temporarily holding references in ways that cannot be predicted. So if you start to study the retainCount you'll likely spend a lot time trying to figure out what other parts of the system are doing with various objects, which is not likely to help you get your application right.

You should design how your application works so that memory usage is not excessive.

You should worry about how many objects you have in memory, and how many times you have retained them (this is a number that will be less than the retainCount), and making sure you release them as many times as you retain them.

Calling retain on an object multiple times still only results in a single copy of the object being in memory.

To check for memory usage and/or leaks, you use the instruments leaks detector.


The retain count is just a number. An object whose retain count hits zero will get deallocated; aside from that, it doesn't matter if it's been retained once or fifty times.

0

精彩评论

暂无评论...
验证码 换一张
取 消