Why use the Autorelease pool?

I know that in the main method an autoresource pool has been created, and all objects that receive an autostart message are stored in this pool and freed when the pool is depleted.

But they always say to avoid auto-implementing objects, to avoid memory leaks, and, in turn, application crashes.

Then why and under what conditions should we use autoreleasepool?

Apple docs offers us to use them when we use streams, so at the beginning of the stream we need to create an autorun pool and merge it at the end of the stream, but what if we do not create an autorun object in the full stream, then in this condition it is also necessary to create autoreleasepool in the beginning of the flow.

Please clear my confusion. Thanx.

+28
memory-management objective-c nsautoreleasepool
Jun 18 '11 at 8:11
source share
1 answer

Your guess is correct. When you can make sure that a particular thread never uses autorealized objects, that thread does not need an autoresist pool.

Avoiding an abstract is bad advice, a coin has two sides. Using autorelease'd objects carries a certain amount of overhead (albeit insignificant in most scenarios), which should be avoided when possible. Especially in cases where there are several outputs to the method or an exception can be detected, auto-implementation helps to avoid memory leaks and makes the code cleaner.

Remember that this means that nothing in this thread can use autorelease, including any frameworks you can name. There are situations when this occurs, for example, in the classic scenario of a producer / consumer. You have a manufacturer who creates objects, sends them to the main runloop threads, and can register them in the main autoreleasepool threads.

In general, I would not recommend creating a thread where significant work is done (besides a simple, long calculation) without autoreleasepool.

+19
Jun 18 '11 at 8:17
source share



All Articles