您需要在 GCD 的块内创建 NSAutoreleasePool 吗?
通常,如果您生成后台线程或在 NSOperationQueue 上运行 NSOperation,则需要为该线程或操作创建 NSAutoreleasePool,因为默认情况下不存在。
同样的规则是否适用于放置在 Grand Central Dispatch 队列中并将在非主线程上运行的块?也就是说,您是否需要在分派到主队列以外的任何块的每个块中创建一个 NSAutoreleasePool ?
在我有限的测试中,我没有看到您通常在后台线程或 NSOperations 中看到的自动释放对象的控制台警告。但是,我似乎找不到这方面的明确文档,所以我想知道是否有人可以指出这一点。
Normally, if you spawn a background thread or run an NSOperation on an NSOperationQueue you need to create an NSAutoreleasePool for that thread or operation because none exists by default.
Does the same rule apply to a block that is placed within a Grand Central Dispatch queue and will be run on a non-main thread? That is, do you need to create an NSAutoreleasePool within each block you dispatch to anything other than the main queue?
In my limited testing, I don't see the console warnings for autoreleased objects that you normally see with background threads or NSOperations. However, I can't seem to find definitive documentation on this, so I was wondering if someone could point out where this is stated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大中央调度将自动管理每个队列的自动释放池。然而,无法保证泳池何时会排空;它可能是在处理一个块之后,也可能是在数百个块之后(但可能不会)。
因此,如果您只分配几个对象,请不要担心。但是,如果您要分配大量对象(并且由于您的目标是内存受限的环境),那么您应该创建并排空池。
文档已更新。
看
https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1
Grand central dispatch will manage an autorelease pool per queue automatically. However, there are no guarantees as to when the pool will be drained; it may be after one block is processed, it may be after hundreds (but probably won't be).
So, if you are only allocating a few objects, don't worry about it. However, if you are allocating any significant number of objects (and since you are targeting a memory constrained environment), then you should be creating and draining pools.
The documentation has been updated.
See
https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1