Why is autorelease especially dangerous/expensive for iPhone applications?

e.James picture e.James · Mar 5, 2009 · Viewed 11.5k times · Source

I'm looking for a primary source (or a really good explanation) to back up the claim that the use of autorelease is dangerous or overly expensive when writing software for the iPhone.

Several developers make this claim, and I have even heard that Apple does not recommend it, but I have not been able to turn up any concrete sources to back it up.

SO references:
autorelease-iphone
Why does this create a memory leak (iPhone)?

Note: I can see, from a conceptual point of view, that autorelease is slightly more expensive than a simple call to release, but I don't think that small penalty is enough to make Apple recommend against it.

What's the real story?

Answer

TofuBeer picture TofuBeer · Mar 5, 2009

(cannot accept your own answer?)

Well, after all that, I did manage to find a reference from Apple Developer, added as a side-note near the bottom of the page:

iPhone OS Note: Because on iPhone OS an application executes in a more memory-constrained environment, the use of autorelease pools is discouraged in methods or blocks of code (for example, loops) where an application creates many objects. Instead, you should explicitly release objects whenever possible.

Still, this suggests using autorelease carefully, not avoiding it altogether.

(and now for my comment)

It sounds like there is a certain amount of overhead in maintaining the pool. I read this article which would lead me to probably avoid autorelease as much as possible because I prefer things to be consistent. If you have some memory under autorelease and other memory being totally manually managed it can be a bit more confusing.