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

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

问题描述

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

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.

相同的规则是否适用于放置在Grand Central Dispatch队列中的块,并且将在非主线程上运行?也就是说,你需要在每个块中创建一个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?

在我有限的测试中,我看不到控制台您通常在后台线程或NSOperations中看到的自动释放对象的警告。但是,我似乎找不到关于这个的明确文件,所以我想知道是否有人可以指出这是什么说明。

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.

推荐答案


同样的规则适用于放置在Grand Central
调度队列中的块
,并且将在
非主线程上运行吗?也就是说,你需要

内创建一个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将自动为每个队列管理一个自动释放池。然而,没有保证何时池将被耗尽;它可能在一个块被处理之后,可能在数百(但可能不会)之后。

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).

因此,如果你只分配几个对象,don不要担心。但是,如果你分配了大量的对象(因为你是针对一个内存受限的环境),那么你应该创建和删除池。

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.

文档已更新。

请参阅
https://developer.apple.com/library/ios/DOCUMENTATION/General /Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1


如果你的块创建了多个Objective-C对象,你可能
想把块的代码在@autorelease块中包含到
中,处理这些对象的内存管理。虽然GCD dispatch
队列有自己的自动释放池,但是当这些池耗尽时,它们不能保证
。如果你的应用程序是内存
约束,创建自己的autorelease池允许你以更定期的间隔释放
的内存用于自动释放的对象。

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天全站免登陆