NSAutoreleasePool的使用

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)釋放池釋放掉翘贮。


事件處理池子釋放
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末赊窥,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子狸页,更是在濱河造成了極大的恐慌锨能,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件芍耘,死亡現(xiàn)場(chǎng)離奇詭異址遇,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)斋竞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門(mén)倔约,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人坝初,你說(shuō)我怎么就攤上這事浸剩。” “怎么了鳄袍?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵绢要,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我拗小,道長(zhǎng)重罪,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮剿配,結(jié)果婚禮上搅幅,老公的妹妹穿的比我還像新娘。我一直安慰自己呼胚,他們只是感情好盏筐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著砸讳,像睡著了一般琢融。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上簿寂,一...
    開(kāi)封第一講書(shū)人閱讀 51,763評(píng)論 1 307
  • 那天漾抬,我揣著相機(jī)與錄音,去河邊找鬼常遂。 笑死纳令,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的克胳。 我是一名探鬼主播平绩,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼漠另!你這毒婦竟也來(lái)了捏雌?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤笆搓,失蹤者是張志新(化名)和其女友劉穎性湿,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體满败,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡肤频,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了算墨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宵荒。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖净嘀,靈堂內(nèi)的尸體忽然破棺而出报咳,到底是詐尸還是另有隱情,我是刑警寧澤面粮,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布少孝,位于F島的核電站继低,受9級(jí)特大地震影響熬苍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一柴底、第九天 我趴在偏房一處隱蔽的房頂上張望婿脸。 院中可真熱鬧,春花似錦柄驻、人聲如沸狐树。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)抑钟。三九已至,卻和暖如春野哭,著一層夾襖步出監(jiān)牢的瞬間在塔,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工拨黔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蛔溃,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓篱蝇,卻偏偏與公主長(zhǎng)得像贺待,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子零截,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容