How do you detect memory leaks on iPhone?

2019-01-16 07:10发布

问题:

I'm using the Leaks Instruments feature through Xcode to (try and) find memory leaks. I still haven't figured out how to use this program. I click Leaks in the program and see memory increasing as I do various things in the simulator. I have Extended Detail pane displayed. The only thing in Extended Detail pane that references my app is main. As in the main method produced by Xcode. Everything else is UIKit, Foundations, and other SDK classes I didn't write. What am I doing wrong that nothing is showing up from my app?

Before I hit 3 minutes, there are over 100 leaks totaling 2.5k. Is this common?

回答1:

I've written up a Tutorial on using Instruments to track iPhone memory leaks. I'm not sure if it will help you with what you're dealing with or not...couldn't hurt, though. :-)

http://www.streamingcolour.com/blog/tutorials/tracking-iphone-memory-leaks/



回答2:

Change the view to "Extended Detail" on the instruments panel. This will show you the stack trace of each leaked object after you stop recording and select the leaked object.

You do see calls into the API, but what you are interested in is finding the last method of your application before the API calls, that is where the leak is.

A tip: turn on "gather memory contents" in the leaks view. Seeing the object values should also help finding where the problem is.

You don't want any leaks. 100 leaks is not typical (at least in my apps ;) Typical should be 0.



回答3:

I'm not familiar with how to use Leaks, but you can always try running the Clang analyzer on your code to see if that'll turn anything up: http://clang.llvm.org/StaticAnalysis.html. It can often find many bugs that might lead to memory leaks.



回答4:

Xcode: run -> Start with Performance Tool -> Leaks



回答5:

Keep in mind that the Simulator may leak when the device will not. Ran into that once already with UITableViewController class.



回答6:

Use LLVM/Clang Static Analyzer.



回答7:

Note also that the leak tool is not going to show you instances where objects are over-retained and still held on to. Leaks are cases where objects that should have been let go are just hanging around with no-one to clean them up. Over retained objects are validly held onto even though you'd think they should be gone - thus the leak tool cannot point them out, since they are still referred to and there's no way to tell them apart from objects that should still be retained.

To find those, use the memory reporting tool and make sure that memory use goes down fully after you free an object. If you notice something isn't freeing memory, you can start by putting breakpoints in dealloc to see if what you expect to see released is actually getting released.

You need to look for both cases to keep a clean memory footprint.



回答8:

Run -> Start with Performance Tool -> Leaks


回答9:

To detect memory leaks you can use the "build and analyze" function of Xcode.

Simply select Build -> Build and Analyze in the Xcode menu.



回答10:

Made a sum up of the main memory leak tools: http://bcaccinolo.wordpress.com/2010/09/15/iphone-essential-performance-tools-list/



回答11:

Leaks application that can be found in Xcode: run -> Start with Performance Tool -> Leaks.

Apple’s Instruments utility that can be found in /Developer/Applications/Performance Tools.



回答12:

One of the best way to find the memory leaks is Select Product-> Analyze. In the left Xcode shows in which file you have memory leaks. What are the variable causing memory leaks. This is one of the best way to find memory leaks.