为什么我的 Cocoa 应用程序中的某些行需要永远运行?
我有一个非常奇怪的问题。我正在一个分离的线程中运行一个循环,循环中的某些代码行(循环每次迭代中的不同行)需要很长时间(约 45 秒)才能执行,即使它们是简单的命令,例如使用数据初始化 NSXMLDocument。这个问题也很不一致,不会在同一个地方或者每次都出现。有什么想法为什么会发生这种情况吗?
附言。由于我的代码又长又复杂,而且问题不一致,所以我无法发布任何示例代码:/
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 :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要猜测性能问题。有很多工具可以帮助您确定问题所在,包括 Instruments 应用程序和 Shark< /a>.这两者都可以让您准确分析 CPU 时间的去向,以便您可以采取一些措施。
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.
别人怎么说;您应该使用 Instruments 来确定时间都去哪儿了。特别是,您需要使用 CPU 采样器工具和虚拟内存活动工具。
从声音来看,我敢打赌您的应用程序正在导致系统耗尽 RAM 并开始分页到磁盘。这将按照您所描述的方式消除杀伤性能;在运行过程中的某个不确定的时间,应用程序似乎要么超级缓慢,要么只是暂停了一段时间。
您可能还应该使用对象分配工具来确定您是否按预期使用内存,如果使用相当多,您可以在哪里优化内存使用。
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.