Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does every custom GCD queue need an autorelease pool under ARC?

For example if you create your own GCD queue:

self.renderQueue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_SERIAL);

Do you have to create an autorelease pool in every block submitted to that queue, using:

@autoreleasepool {

}

?

Or does ARC create it for you? What happens if you don't specify autoreleasepool in your custom queues?

like image 349
iamjustaprogrammer Avatar asked Mar 21 '23 04:03

iamjustaprogrammer


1 Answers

It depends on your usage of autoreleased objects.

Every GCD thread has an outermost autorelease pool, but that pool is drained at a time you cannot control directly (currently the drain occurs when the thread becomes idle, just before it parks itself in the kernel awaiting reuse or reaping).

If your process keeps GCD threads active for long periods of time and/or if you create a large number of autoreleased objects (or very large autoreleased objects) in your blocks, you may wish to create a pool in your blocks to ensure the resources occupied by those objects are freed up earlier.

like image 63
das Avatar answered Apr 26 '23 03:04

das