Autorelease pool blocks provide a mechanism whereby you can relinquish ownership of an object, but avoid the possibility of it being deallocated immediately (such as when you return an object from a method). Typically, you don’t need to create your own autorelease pool blocks, but there are some situations in which either you must or it is beneficial to do so
The AppKit and UIKit frameworks process each event-loop iteration (such as a mouse down event or a tap) within an autorelease pool block. Therefore you typically do not have to create an autorelease pool block yourself, or even see the code that is used to create one
① AppKit与UIKit在每次Runloop迭代时会默认在autorelease pool下执行
If you are writing a program that is not based on a UI framework, such as a command-line tool.
② 如果你是基于非UI framework的命令行编程,你必须自己创建autorelease pool,否则可能造成autoreleased对象的内存泄漏
If you spawn a secondary thread.You must create your own autorelease pool block as soon as the thread begins executing; otherwise, your application will leak objects. (See Autorelease Pool Blocks and Threads for details.)
② 如果你自己创建了子线程,你必须创建autorelease pool,否则可能造成autoreleased对象的内存泄漏
If you write a loop that creates many temporary objects.You may use an autorelease pool block inside the loop to dispose of those objects before the next iteration. Using an autorelease pool block in the loop helps to reduce the maximum memory footprint of the application.
③ 如果你在循环中创建了大量的临时对象(autoreleased对象),你应该在每次循环迭代间使用autorelease pool,这样能在下次循环前对对象进行销毁从而有效地降低系统内存使用峰值
官方文档: Using Autorelease Pool Blocks
官方文档: NSAutoreleasePool