全局GCD队列的内存使用情况 [英] Memory usage for global GCD queue

查看:91
本文介绍了全局GCD队列的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要过滤的图像很多,当我在创建的串行队列中执行此操作时,每个块完成后就会释放内存.当我将这项工作分派到全局GCD队列时,内存不会释放并且无法控制.

I have a number of images I am filtering and when I do this in a serial queue that I create ,the memory is released after each block is complete. When I dispatch this work to the global GCD queue the memory doesnt release and gets out of control.

我将这些语句包装在一个自动释放块中,但这似乎没有什么不同.

I wrapped the statements in an autorelease block but that doesnt seem to make a difference.

是因为线程池以某种方式保留了对块的引用吗?一段时间后,内存被释放,但是在此之前,该应用将崩溃,其中包含大量图像.

Is it due to the fact that the thread pool keeps references to the blocks somehow? After a while the memory gets released, but the app will crash before this with a large number of images.

为什么内存由全局队列保留,并且如何解决此问题,以便在每个块之后释放它?

Why is the memory being kept by the global queue and how could this be fixed so it releases after every block?

推荐答案

您在评论中提到GCD似乎创建了太多线程,导致太多图像一次存储在内存中.如果GCD产生了线程负载",其中负载>内核数的3倍,则可能意味着您要提交给它的块最终被阻塞了(也许在I/O上).一种性能更高的方法是对串行队列上的I/O进行序列化,并且仅并行执行内存中处理.从这方面获得GCD帮助的一种方法是使用dispatch_apply,它将限制并发操作的数量. IME dispatch_apply将创建的线程数不超过2 *. (尽管这并不考虑可能创建线程的其他并发操作.)

You mentioned in your comments that GCD seems to be creating too many threads causing too many images to be in memory at once. If GCD is spawning "loads of threads" where loads is > 3x number of cores, it probably means the blocks you're submitting to it are ending up blocked (perhaps on I/O). A more performant approach would be to serialize the I/O on a serial queue, and do only the in-memory processing in parallel. One way to get help from GCD in this regard is to use dispatch_apply which will limit the number of concurrent operations. IME dispatch_apply will not create more than 2 * number-of-cores threads. (Although that doesn't account for other, concurrent operations that may create threads.)

此外,如您所述,NSOperationQueue允许您指定最大并发任务数,这是另一种方法.

Also, as you noted, NSOperationQueue allows you to specify a maximum number of concurrent tasks, which is another approach.

这篇关于全局GCD队列的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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