NSAutoreleasePool
自动释放池
An object that supports Cocoa’s reference-counted memory management system.
自动释放池是一个支持cocoa的自动引用技术内存管理系统的对象。
Overview
概述
An autorelease pool stores objects that are sent a release message when the pool itself is drained.
当池子释放的时候,池子中存储的对象会被发送一条release消息。
Important
重点
If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use@autoreleasepool blocks. For example, in place of:
如果你使用自动引用计数的时候,你不能直接使用自动释放池。相反,你应该使用@autoreleasepool block。例如取代下边的代码:
NSAutoreleasePool*pool = [[NSAutoreleasePoolalloc] init];// Code benefitting from a local autorelease pool.
[pool release];
你将会写:
@autoreleasepool{
// Code benefitting from a local autorelease pool.
}
@autoreleasepoolblocks are more efficient than using an instance of NSAutoreleasePool directly; you can also use them even if you do not use ARC.
@autoreleasepool blocks比直接使用NSAutoreleasePool更加高效,即使不在ARC下,你也可以使用它们。
In a reference-counted environment (as opposed to one which uses garbage collection), an NSAutoreleasePool object contains objects that have received an autorelease message and when drained it sends a release message to each of those objects. Thus, sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is drained (it may be longer if the object is subsequently retained). An object can be put into the same pool several times, in which case it receives a release message for each time it was put into the pool.
在自动引用计数的环境中(和使用垃圾回收的相反),一个自动释放池会包含那些收到一条autorelease消息的对象。当释放池子的时候,会发送给这个对象一条release消息。因此发送autorelease消息取代了release消息对于每个对象。因此,发送autorelease消息取代release消息延长了那个对象的生命周期,直到这个池子释放的时候。一个对象可以被放入同一个释放池多次,在这种情况下,每次把它放入自动释放池,它都会收到一条消息。
注意:上边的意思就是,一般我们不要进行多次autorelease消息的发送。因为,这样池子释放的时候会发送多次的release消息。
In a reference counted environment, Cocoa expects there to be an autorelease pool always available. If a pool is not available, autoreleased objects do not get released and you leak memory. In this situation, your program will typically log suitable warning messages.
子自动释放池的环境中,cocoa希望总是有一个自动释放池可以使用。如果一个池子没有用的话,自动释放池的对象就不能释放,你的内存就泄露了。在这种情况下,你的程序一般会有相应的警告信息。
The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop, and drains it at the end, thereby releasing any autoreleased objects generated while processing an event. If you use the Application Kit, you therefore typically don’t have to create your own pools. If your application creates a lot of temporary autoreleased objects within the event loop, however, it may be beneficial to create “local” autorelease pools to help to minimize the peak memory footprint.
在程序每次循环事件开始的时候,框架都会在主线程创建一个自动释放池,在最后释放,因此会释放在处理事件过程中生成的自动释放对象。如果你使用程序框架,你一般来说没有必要创建你的自动释放池。但是,创建本地的自动释放池可以帮助来减少内存的峰值占用。
You create an NSAutoreleasePool object with the usual alloc and init messages and dispose of it with drain (or release—to understand the difference, seeGarbage Collection). Since you cannot retain an autorelease pool (or autorelease it—see retain and autorelease), draining a pool ultimately has the effect of deallocating it. You should always drain an autorelease pool in the same context (invocation of a method or function, or body of a loop) that it was created. SeeUsing Autorelease Pool Blocksfor more details.
你创建用allocation和init发送消息来创建一个自动释放池,用drain来释放自动释放池。由于你不能retain自动释放池,释放自动释放池知道要销毁它。你应该总是在同一个上下文中创建和释放自动释放池。
Each thread (including the main thread) maintains its own stack of NSAutoreleasePool objects (seeThreads). As new pools are created, they get added to the top of the stack. When pools are deallocated, they are removed from the stack. Autoreleased objects are placed into the top autorelease pool for the current thread. When a thread terminates, it automatically drains all of the autorelease pools associated with itself.
每个线程(包括主线程)都维护它自己的自动释放池的堆栈结构。新池子被创建的时候,他们会被添加到栈的顶部。当池子销毁的时候,会从栈移除。对于当前线程来说,Autoreleased对象会被放到自动释放池的顶部。当一个线程停止的时候,它会自动释放掉和它结合的所有自动释放池。
Threads
线程
If you are making Cocoa calls outside of the Application Kit’s main thread—for example if you create a Foundation-only application or if you detach a thread—you need to create your own autorelease pool.
如果你要在Application Kit的主线程外调用。例如你要创建一个只有基础框架的程序或者创建一个子线程,你需要创建你自己的自动释放池。
记住Application Kit的应用对象为Mac OS。在iOS下不需要注意上边这一条。
If your application or thread is long-lived and potentially generates a lot of autoreleased objects, you should periodically drain and create autorelease pools (like the Application Kit does on the main thread); otherwise, autoreleased objects accumulate and your memory footprint grows. If, however, your detached thread does not make Cocoa calls, you do not need to create an autorelease pool.
如果你的程序或者线程是长久存在的,潜在的会生成大量的autoreleased对象,你应该每隔一段时间释放和创建自动释放池,像在程序在主线程一样。否则,autoreleased会累积,内存会增长。然而,如果你二级线程不会使cocoa调用,你没有必要创建自动释放池。
Note
注意:
If you are creating secondary threads using the POSIX thread APIs instead of NSThread objects, you cannot use Cocoa, including NSAutoreleasePool, unless Cocoa is in multithreading mode. Cocoa enters multithreading mode only after detaching its firstNSThread object. To use Cocoa on secondary POSIX threads, your application must first detach at least one NSThread object, which can immediately exit. You can test whether Cocoa is in multithreading mode with the NSThread class method isMultiThreaded.
如果你用POSIX thread APIs取代NSThread对象来创建二级线程,你不能使用cocoa,包括NSAutoreleasePool,除非cocoa是多线程模式。cocoa会进入二级线程,只有在分离第一个线程之后。为了使用secondary POSIX threads,你必须分离出至少一个线程,这个线程可以立即停止。你可以进行测试cocoa中的多线程模式用NSThread类的isMultiThreaded方法。
总结:
1.池子会在自动释放池满了之后进行一次释放。
2.每处理一次用户交互事件其实都会创建一个西东释放池,处理完成之后,把这个自动释放池释放掉。