您需要在 GCD 的块内创建 NSAutoreleasePool 吗?

发布于 2024-10-01 03:21:44 字数 310 浏览 3 评论 0原文

通常,如果您生成后台线程或在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2024-10-08 03:21:44

相同的规则是否适用于块
位于中央车站内
调度队列并将在
非主线程?也就是说,你需要
在其中创建一个 NSAutoreleasePool
你分配给任何东西的每个块
除了主队列之外?

大中央调度将自动管理每个队列的自动释放池。然而,无法保证泳池何时会排空;它可能是在处理一个块之后,也可能是在数百个块之后(但可能不会)。

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


文档已更新。


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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文