Why are certain lines in my Cocoa application taking forever to run?
I'm having a very strange problem. I'm running a loop in a detached thread, and some lines of code in my loop (different lines in each iteration of the loop) are taking a long long time (~45 seconds) to execute, even if they are simple commands like initializing an NSXMLDocument with data. This problem is开发者_运维技巧 also very inconsistent, and it doesn't occur in the same place or each time. Any ideas why this is happening?
PS. Since my code is so long and complex, and the problem isn't consistent, I can't post any sample code :/
Don't guess about performance issues. There are plenty of tools to help you determine what is going wrong, including the CPU Sampler instrument in the Instruments app, and Shark. Both of these will let you analyse exactly where the CPU time is going, so you can do something about it.
Use Instruments to find out why.
What others said; you should use Instruments to identify where the time is going. In particular, you'll want to use the CPU sampler instrument and the virtual memory activity instrument.
From the sounds of it, I would bet that your application is causing the system to run out of RAM and start paging to disk. This will flat out kill performance in exactly the way you describe; at some indeterminate time during the run, it seems like the app is either super sluggish or just pauses for a while.
You should probably also use the Object Alloc instrument to figure out if you are using memory as expected and, if using quite a lot, where you might optimize your memory use.
精彩评论