你需要在 GCD 的块内创建一个 NSAutoreleasePool 吗? [英] Do you need to create an NSAutoreleasePool within a block in GCD?

查看:31
本文介绍了你需要在 GCD 的块内创建一个 NSAutoreleasePool 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果您在 NSOperationQueue 上生成后台线程或运行 NSOperation,您需要为该线程或操作创建一个 NSAutoreleasePool,因为默认情况下不存在.

相同的规则是否适用于放置在 Grand Central Dispatch 队列中并将在非主线程上运行的块?也就是说,您是否需要在每个分派到主队列以外的任何块中创建一个 NSAutoreleasePool?

在我有限的测试中,我没有看到您通常在后台线程或 NSOperations 中看到的自动释放对象的控制台警告.但是,我似乎找不到这方面的权威文档,所以我想知道是否有人可以指出这一点.

解决方案

同样的规则是否适用于块被放置在一个大中央调度队列,将在一个非主线程?也就是说,你需要在其中创建一个 NSAutoreleasePool你发送给任何东西的每个块除了主队列?

Grand Central dispatch 将自动管理每个队列的自动释放池.但是,无法保证池何时会被排空;可能是在处理一个块之后,也可能是在数百个之后(但可能不会).

因此,如果您只分配几个对象,请不要担心.但是,如果您要分配大量对象(并且由于您的目标是内存受限环境),那么您应该创建和排空池.


文档已更新.

https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1><块引用>

如果你的代码块创建了多个 Objective-C 对象,你可能会想要将块代码的一部分包含在 @autorelease 块中处理这些对象的内存管理.虽然 GCD 派遣队列有自己的自动释放池,他们不保证当这些水池被排干时.如果您的应用程序是内存受限,创建自己的自动释放池可以让你释放以更规律的时间间隔自动释放对象的内存.

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.

解决方案

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?

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

If your block creates more than a few Objective-C objects, you might want to enclose parts of your block’s code in an @autorelease block to handle the memory management for those objects. Although GCD dispatch queues have their own autorelease pools, they make no guarantees as to when those pools are drained. If your application is memory constrained, creating your own autorelease pool allows you to free up the memory for autoreleased objects at more regular intervals.

这篇关于你需要在 GCD 的块内创建一个 NSAutoreleasePool 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆