19

Let's say that I have a loop that returns a bunch of autoreleased NSData objects...

NSData* bigData = ...
while(some condition) {
    NSData* smallData = [bigData subdataWithRange:...];
    //process smallData
}

Under ARC, should I still wrap an @autoreleasepool around the while condition?

NSData* bigData = ...
@autoreleasepool {
    while(some condition) {
        NSData* smallData = [bigData subdataWithRange:...];
        //process smallData
    }
}

The reason why I'm asking is I see the living allocation count in instruments going through the roof for my NSData objects that invoke a dataWith... method as opposed to an initWith... method. When I use initWith..., the living allocation count is much, much less.

Is it better to prefer the initWith... methods whenever possible?

3 Answers 3

13

Yes you should still use autorelease pools when using convenience methods in a tight loop. All the old memory management rules still apply under ARC, the compiler is merely injecting RRs for you. Checkout the great post by the awesome Mike Ash!

Link

2
  • 10
    Per @Chuck's answer: the @autoreleasepool needs to go inside the loop for it to do any good. The idiom is while (x) @autoreleasepool { ... }, not @autoreleasepool { while (x) { ... } }. Oct 4, 2012 at 19:18
  • This is oversimplified and incorrect. ARC can often optimize away use of the autorelease pool for methods that return a +0 object (which is most use of the use of autorelease). Jun 12, 2015 at 20:35
13

I think your issue is that the autorelease pool is supposed to go inside the loop. With the loop inside the autorelease block rather than vice-versa, the accumulated objects won't be released until after the loop finishes.

2
  • I haven't thought about putting the @autoreleasepool inside the loop...thanks for the tip.
    – Tim Reddy
    Oct 19, 2011 at 19:53
  • if not using the @autorelease block, and considiering that smallData is a local variable, that local variable and the object is pointing to is not deallocated after each iteration in the while loop? why do we need @autoreleaseblock? this is confusing me? Jul 7 at 10:25
7

Under ARC, should I still wrap an @autoreleasepool around the while condition?

Yes. Autorelease Pools are still in place, and grow and pop as before. The compiler just adds and coalesces the necessary retains and releases operations when ARC is enabled (echoing Logan), based on the methods that are visible to the TU and default naming conventions.

Execution in ARC is nearly identical the manual reference counting: Autorelease pool stacks still exist. One difference is that the compiler may order the reference counting operations slightly different from the way you wrote it (not in an incorrect way), and may omit unnecessary retain cycles.

Is it better to prefer the initWith... methods whenever possible?

WRT minimizing heap growth compared to the autoreleased counterparts: Yes. That's always been the case. It's especially important on iOS devices, where memory is quite limited.

The exception to this is when the object may avoid an allocation. Example:

NSString * copy = [NSString stringWithString:arg];

in this case, copy may be [[arg retain] autorelease]. Note that in this case, copy is still autoreleased, but you should not usually go to great lengths to test the presence of such optimizations. Note: It's also better to use copy = [arg copy]...[arg release] here.

The other bonus is that your ref count imbalances are often caught earlier when the object is never autoreleased, and closer to the call site (rather than when the Autorelease Pool is finally popped).

Performance with large autorelease pools is actually much worse than most people would suppose. If you can avoid depending on them heavily (e.g. using alloc+init...+release), you can make your program noticeably faster. Explicitly creating autorelease pools is cheap, and can help minimize this problem. When allocations are large and/or numerous, avoid using autorelease on them where possible, and do wrap these sections in explicit autorelease pools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.