Why is @autoreleasepool still needed with ARC?
People often misunderstand ARC for some kind of garbage collection or the like. The truth is that, after some time people at Apple (thanks to llvm and clang projects) realized that Objective-C's memory administration (all the retains
and releases
, etc.) can be fully automatized at compile time. This is, just by reading the code, even before it is run! :)
In order to do so there is only one condition: We MUST follow the rules, otherwise the compiler would not be able to automate the process at compile time. So, to ensure that we never break the rules, we are not allowed to explicitly write release
, retain
, etc. Those calls are Automatically injected into our code by the compiler. Hence internally we still have autorelease
s, retain
, release
, etc. It is just we don't need to write them anymore.
The A of ARC is automatic at compile time, which is much better than at run time like garbage collection.
We still have @autoreleasepool{...}
because having it does not break any of the rules, we are free create/drain our pool anytime we need it :).
@autoreleasepool
doesn't autorelease anything. It creates an autorelease pool, so that when the end of block is reached, any objects that were autoreleased by ARC while the block was active will be sent release messages. Apple's Advanced Memory Management Programming Guide explains it thus:
At the end of the autorelease pool block, objects that received an autorelease message within the block are sent a release message—an object receives a release message for each time it was sent an autorelease message within the block.
ARC doesn't get rid of retains, releases and autoreleases, it just adds in the required ones for you. So there are still calls to retain, there are still calls to release, there are still calls to autorelease and there are still auto release pools.
One of the other changes they made with the new Clang 3.0 compiler and ARC is that they replaced NSAutoReleasePool
with the @autoreleasepool
compiler directive. NSAutoReleasePool
was always a bit of a special "object" anyway and they made it so that the syntax of using one is not confused with an object so that it's generally a bit more simple.
So basically, you need @autoreleasepool
because there are still auto release pools to worry about. You just don't need to worry about adding in autorelease
calls.
An example of using an auto release pool:
- (void)useALoadOfNumbers {
for (int j = 0; j < 10000; ++j) {
@autoreleasepool {
for (int i = 0; i < 10000; ++i) {
NSNumber *number = [NSNumber numberWithInt:(i+j)];
NSLog(@"number = %p", number);
}
}
}
}
A hugely contrived example, sure, but if you didn't have the @autoreleasepool
inside the outer for
-loop then you'd be releasing 100000000 objects later on rather than 10000 each time round the outer for
-loop.
Update:
Also see this answer - https://stackoverflow.com/a/7950636/1068248 - for why @autoreleasepool
is nothing to do with ARC.
Update:
I took a look into the internals of what's going on here and wrote it up on my blog. If you take a look there then you will see exactly what ARC is doing and how the new style @autoreleasepool
and how it introduces a scope is used by the compiler to infer information about what retains, releases & autoreleases are required.