✍️ Note

Some codes and contents are sourced from Apple’s official documentation. This post is for personal notes where I summarize the original contents to grasp the key concepts

What is Autorelease Pool?

An object that supports Cocoa’s reference-counted memory management system.

An autorelease pool stores objects that are sent a release message when the pool itself is drained.

If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool blocks. 

@autoreleasepool blocks are more efficient than using an instance of NSAutoreleasePool directly; you can also use them even if you do not use ARC.

https://developer.apple.com/documentation/foundation/nsautoreleasepool

AutoRelease Pool

You can send multiple autorelease message to an object. When autorelease pool released, object will receive multiple release message.

Common Scenario when Is it useful for? -> Created an object in a Loop!

for (i=0; i < MANY_TIMES; i++)
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//Create an object and send autorelease message

MyObject *object = [[MyObject alloc] init];
[object autorelease];

//Release all objects that created in this loop.
[pool release];

}

Leave a comment

Quote of the week

"People ask me what I do in the winter when there's no baseball. I'll tell you what I do. I stare out the window and wait for spring."

~ Rogers Hornsby