NSAutoreleasePool的使用

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.每处理一次用户交互事件其实都会创建一个西东释放池,处理完成之后,把这个自动释放池释放掉。


事件处理池子释放
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容