0

I suspect my app is creating lots of threads using dispatch_async() calls. I've seen crash reports with north of 50 and 80 threads. It's a large code base I didn't write and haven't fully dissected. What I would like to do is get a profile of our thread usage; how many threads we're creating, when we're creating them, etc.

My goal is to figure out of we are spending all of our time swapping threads and if using an NSOperationQueue would be better so we have more control than we do by just dispatch_async'ing blocks all over willy-nilly.

Any ideas / techniques for investigating this are welcome.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • Yuck. async_dispatch is fine, but you really want to look at the number of GCD QUEUE's being defined. Make use of a small number of named serial queues so you have fewer threads. While Instruments will let you see threads, you might have a hard time correlating these threads to the async_dispatch() calls. Good ol' NSLog might be your best friend on this one, you can at least log all async_dispatch calls. – CSmith Aug 16 '12 at 17:22
  • 2
    I highly recommend reading Mike Ash's ["GCD Practicum"](http://www.mikeash.com/pyblog/friday-qa-2009-09-25-gcd-practicum.html), because he describes in detail the situations that can cause the behavior you're seeing, and ways to address them. That doesn't answer your question about tools, however. – Brad Larson Aug 16 '12 at 17:24
  • No, but thats good information. I'll take a look at the book. I'm a Windows guy still trying to get bad-ass proficient in this Apple world, so any ends to that means is appreciated. – i_am_jorf Aug 16 '12 at 17:25
  • @jeffamaphone - It's actually a blog post as part of his Friday series (which are always great resources). Go to the link in my comment and read it now, because it's fairly short and his suggestions of resource-specific serial queues and dispatch semaphores are how I solve almost all of these kinds of block buildup problems. – Brad Larson Aug 16 '12 at 17:48
  • Yep, I've been reading it. :) Looks very relevant. – i_am_jorf Aug 16 '12 at 17:48
  • 1
    @jeffamaphone - Also, Apple had some great demonstrations about how to measure and avoid these kinds of GCD problems in several of the WWDC session videos on GCD. There are a lot of great tips in the GCD videos from 2009-2012 (usually only one or two per conference), and I've watched all of those a couple of times now. – Brad Larson Aug 16 '12 at 17:51
  • Yes, I'm going to spend some time profiling with Instruments (I've bene reading the Apple docs and it seems straight forward), but I have some good ideas about areas to investigate. Mike Ash's demo is almost exactly the situation I have, but with network IO instead of disk. – i_am_jorf Aug 16 '12 at 17:55

2 Answers2

1

Looks like you need to take a look at Instruments. Learn about it from Apple docs or WWDC sessions or wherever you want. There are many resources

Generally NSOperationQueues are definitely better if you need to implement some dependicies.


As Brad Llarson pointed there are a few WWDC sessions which are helpful in many cases. However besides optimizing your calls you should consider making your code more human readable and simply better. I haven't ever seen source code with as many as 80 queues on iOS. There must be something wrong with the architecture of app.

Let me know anyone if I am wrong.

wczekalski
  • 720
  • 4
  • 21
  • Instruments helps a little, at least I can get a good idea of the number of threads being created. Recording more than 20 seconds (like 100s) seems to cause it to fall over and become unusable. Like most profilers I've used, it seems to require a lot of investment and dedication. – i_am_jorf Aug 16 '12 at 19:36
1

If you are spinning that many threads, you are most likely I/O bound. Also, Mike's article is great, but it's quite old (though still relevant wrt regular queues).

Instead of using dispatch_async, you should be using dispatch_io and friends for your I/O requirements. They handle all the asynchronous monitoring and callbacks for you... and will not overrun your process with extraneous processing threads.

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • Good to note. After Mike's article I had a good read of all the GCD docs and discovered the dispatch_io family of functions. – i_am_jorf Aug 18 '12 at 15:54