问题
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?
回答1:
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.)
Also, as you noted, NSOperationQueue
allows you to specify a maximum number of concurrent tasks, which is another approach.
来源:https://stackoverflow.com/questions/19132333/memory-usage-for-global-gcd-queue