NSAutoreleasePool
自動(dòng)釋放池
An object that supports Cocoa’s reference-counted memory management system.
自動(dòng)釋放池是一個(gè)支持cocoa的自動(dòng)引用技術(shù)內(nèi)存管理系統(tǒng)的對(duì)象测萎。
Overview
概述
An autorelease pool stores objects that are sent a release message when the pool itself is drained.
當(dāng)池子釋放的時(shí)候帘皿,池子中存儲(chǔ)的對(duì)象會(huì)被發(fā)送一條release消息辣卒。
Important
重點(diǎn)
If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use@autoreleasepool blocks. For example, in place of:
如果你使用自動(dòng)引用計(jì)數(shù)的時(shí)候,你不能直接使用自動(dòng)釋放池伟叛。相反,你應(yīng)該使用@autoreleasepool block。例如取代下邊的代碼:
NSAutoreleasePool*pool = [[NSAutoreleasePoolalloc] init];// Code benefitting from a local autorelease pool.
[pool release];
你將會(huì)寫(xiě):
@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.
在自動(dòng)引用計(jì)數(shù)的環(huán)境中(和使用垃圾回收的相反)怖亭,一個(gè)自動(dòng)釋放池會(huì)包含那些收到一條autorelease消息的對(duì)象。當(dāng)釋放池子的時(shí)候坤检,會(huì)發(fā)送給這個(gè)對(duì)象一條release消息兴猩。因此發(fā)送autorelease消息取代了release消息對(duì)于每個(gè)對(duì)象。因此早歇,發(fā)送autorelease消息取代release消息延長(zhǎng)了那個(gè)對(duì)象的生命周期倾芝,直到這個(gè)池子釋放的時(shí)候。一個(gè)對(duì)象可以被放入同一個(gè)釋放池多次箭跳,在這種情況下晨另,每次把它放入自動(dòng)釋放池,它都會(huì)收到一條消息谱姓。
注意:上邊的意思就是借尿,一般我們不要進(jìn)行多次autorelease消息的發(fā)送。因?yàn)槭哦危@樣池子釋放的時(shí)候會(huì)發(fā)送多次的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.
子自動(dòng)釋放池的環(huán)境中割捅,cocoa希望總是有一個(gè)自動(dòng)釋放池可以使用。如果一個(gè)池子沒(méi)有用的話(huà)帚桩,自動(dòng)釋放池的對(duì)象就不能釋放亿驾,你的內(nèi)存就泄露了。在這種情況下账嚎,你的程序一般會(huì)有相應(yīng)的警告信息莫瞬。
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 “l(fā)ocal” autorelease pools to help to minimize the peak memory footprint.
在程序每次循環(huán)事件開(kāi)始的時(shí)候,框架都會(huì)在主線(xiàn)程創(chuàng)建一個(gè)自動(dòng)釋放池郭蕉,在最后釋放疼邀,因此會(huì)釋放在處理事件過(guò)程中生成的自動(dòng)釋放對(duì)象。如果你使用程序框架召锈,你一般來(lái)說(shuō)沒(méi)有必要?jiǎng)?chuàng)建你的自動(dòng)釋放池旁振。但是,創(chuàng)建本地的自動(dòng)釋放池可以幫助來(lái)減少內(nèi)存的峰值占用涨岁。
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.
你創(chuàng)建用allocation和init發(fā)送消息來(lái)創(chuàng)建一個(gè)自動(dòng)釋放池拐袜,用drain來(lái)釋放自動(dòng)釋放池。由于你不能retain自動(dòng)釋放池梢薪,釋放自動(dòng)釋放池知道要銷(xiāo)毀它蹬铺。你應(yīng)該總是在同一個(gè)上下文中創(chuàng)建和釋放自動(dòng)釋放池。
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.
每個(gè)線(xiàn)程(包括主線(xiàn)程)都維護(hù)它自己的自動(dòng)釋放池的堆棧結(jié)構(gòu)秉撇。新池子被創(chuàng)建的時(shí)候甜攀,他們會(huì)被添加到棧的頂部。當(dāng)池子銷(xiāo)毀的時(shí)候琐馆,會(huì)從棧移除规阀。對(duì)于當(dāng)前線(xiàn)程來(lái)說(shuō),Autoreleased對(duì)象會(huì)被放到自動(dòng)釋放池的頂部啡捶。當(dāng)一個(gè)線(xiàn)程停止的時(shí)候姥敛,它會(huì)自動(dòng)釋放掉和它結(jié)合的所有自動(dòng)釋放池奸焙。
Threads
線(xiàn)程
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的主線(xiàn)程外調(diào)用瞎暑。例如你要?jiǎng)?chuàng)建一個(gè)只有基礎(chǔ)框架的程序或者創(chuàng)建一個(gè)子線(xiàn)程,你需要?jiǎng)?chuàng)建你自己的自動(dòng)釋放池与帆。
記住Application Kit的應(yīng)用對(duì)象為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.
如果你的程序或者線(xiàn)程是長(zhǎng)久存在的玄糟,潛在的會(huì)生成大量的autoreleased對(duì)象勿她,你應(yīng)該每隔一段時(shí)間釋放和創(chuàng)建自動(dòng)釋放池,像在程序在主線(xiàn)程一樣阵翎。否則逢并,autoreleased會(huì)累積之剧,內(nèi)存會(huì)增長(zhǎng)。然而砍聊,如果你二級(jí)線(xiàn)程不會(huì)使cocoa調(diào)用背稼,你沒(méi)有必要?jiǎng)?chuàng)建自動(dòng)釋放池。
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對(duì)象來(lái)創(chuàng)建二級(jí)線(xiàn)程玻蝌,你不能使用cocoa,包括NSAutoreleasePool蟹肘,除非cocoa是多線(xiàn)程模式。cocoa會(huì)進(jìn)入二級(jí)線(xiàn)程俯树,只有在分離第一個(gè)線(xiàn)程之后帘腹。為了使用secondary POSIX threads,你必須分離出至少一個(gè)線(xiàn)程许饿,這個(gè)線(xiàn)程可以立即停止阳欲。你可以進(jìn)行測(cè)試cocoa中的多線(xiàn)程模式用NSThread類(lèi)的isMultiThreaded方法。
總結(jié):
1.池子會(huì)在自動(dòng)釋放池滿(mǎn)了之后進(jìn)行一次釋放陋率。
2.每處理一次用戶(hù)交互事件其實(shí)都會(huì)創(chuàng)建一個(gè)西東釋放池胸完,處理完成之后,把這個(gè)自動(dòng)釋放池釋放掉翘贮。