Memory leaks destroying my application?

半世苍凉 提交于 2019-12-24 18:16:13

问题


I'm in big trouble now, my iPhone application keep terminating again and again due to memory leaks and I am unable to judge that where are those leaks. Tell me what step can I take in order to resolve this problem.

Thanks


回答1:


I think you're confusing terminology here. A memory leak is when you don't release an object after you're done with it.

A leak won't directly cause a crash. Leaks can indirectly cause crashes if you run out of memory as a result of not releasing lots of objects. However, if your crashes are happening in the simulator as well this is almost certainly not the case (the simulator having far more available memory than the device). As Daniel says, you can use the instruments tool to find out what's leaking.

Another good method is to use the XCode static analyzer - you can have it analyze your code and detect most common leaks.

Now, if your app is crashing and it's not because of a memory leak (you'll be able to tell this because you'll have memory warnings outputting to the console) then chances are your problem isn't a leak. Perhaps it's a bad access (you're over releasing), in which case your instruments leaks tool isn't going to help you.




回答2:


You can use Instruments to see where potential leaks are.

Here is a good tutorial. http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/




回答3:


If you need to find out leaks and source of crash. you can try these

  1. try to find as many leaks as possible from Instruments and fix
  2. set NSZombieEnabled for your executable parameters and debug application crash
  3. change all your autorelease objects to use alloc init as much as possible.

-- worst resort is if it keeps crashing even after trying all the above three. you write memory intensive module of your code in a thread and clean up that thread. I've noticed usually when threading is done the memory footprints are much cleaner. This is just my opinion, but the above 3 you should do.




回答4:


You could also Build + Analyze your application in XCode itself (Cmd+Shft+A or Product > Analyze). It'll show you the more obvious leaks.



来源:https://stackoverflow.com/questions/5744587/memory-leaks-destroying-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!