macos 你需要在 GCD 的块内创建一个 NSAutoreleasePool 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4141123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do you need to create an NSAutoreleasePool within a block in GCD?
提问by Brad Larson
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.
通常,如果您在 NSOperationQueue 上生成后台线程或运行 NSOperation,您需要为该线程或操作创建一个 NSAutoreleasePool,因为默认情况下不存在。
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 队列中并将在非主线程上运行的块?也就是说,您是否需要在分派给主队列以外的任何对象的每个块中创建一个 NSAutoreleasePool?
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.
在我有限的测试中,我没有看到您通常在后台线程或 NSOperations 中看到的自动释放对象的控制台警告。但是,我似乎无法找到关于此的明确文档,所以我想知道是否有人可以指出这一点。
回答by bbum
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 队列中并将在非主线程上运行的块?也就是说,您是否需要在分派给主队列以外的任何对象的每个块中创建一个 NSAutoreleasePool?
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).
Grand Central dispatch 将自动管理每个队列的自动释放池。但是,无法保证池何时会被排空;它可能在一个块被处理之后,也可能在数百个之后(但可能不会)。
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.
文档已更新。
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.
如果您的块创建了多个 Objective-C 对象,您可能希望将块代码的一部分包含在 @autorelease 块中,以处理这些对象的内存管理。尽管 GCD 调度队列有自己的自动释放池,但它们不能保证这些池何时被排空。如果您的应用程序内存受限,创建您自己的自动释放池可以让您以更规律的时间间隔为自动释放对象释放内存。