views:

8136

answers:

12

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?

+2  A: 

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.

Ben Alpert
I downloaded that tool, only to find out in its docs that it seems to be the same one already included in XCode (executed using "command-shift-a"). True?
Jonny
True. A year and a half ago when I posted this, it wasn't included in Xcode.
Ben Alpert
+3  A: 

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.

lajos
@BenThanks. I'll try it.@lajosPlease see the part where I say, " I have Extended Detail pane displayed".
4thSpace
+1  A: 

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.

Kendall Helmstetter Gelner
Great point! To find those I sometimes add NSLog calls to dealloc to print retain count.
lajos
A: 

I just want to find leaks. If the Leaks tool is telling me I have 100 leaks and everything is pointing to SDK classes, there isn't anything I can do about it. But how are others able to get 0 leaks if the SDK is so leaky?

4thSpace
voted down because it's not an answer
prendio2
A: 

Here are some examples of what I see:

1 608 bytes +[NSIndexPath indexPathWithIndexes:length] 0 128 bytes -[NSIndexPath initWithindexes:length]

That's the first two items in the stack along with the memory they're leaking. Both come from Foundations. My stack has a number of these types of calls (UIKit, QuartzCore, CoreFoundations, GraphicsServices). Leaky SDK? I'm using 2.2.

4thSpace
Hmmm... I have not seen any SKD leaks and I've used pretty much all the APIs you are listing. Can you post a screenshot?
lajos
Yeah the SDK I have yet to see any leaks from. More than likely you have places where you used the alloc but then did not release the code. Also make sure that if you are using @property properties that you call them properly.
Niels Hansen
+17  A: 

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/

OwenGoss
+1  A: 

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

Genericrich
A: 

please tell me i am not the one to responsible for this leaking.. +[NSIndexPath indexPathWithIndexes:length:]

+1  A: 

Use LLVM/Clang Static Analyzer.

+2  A: 

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

Khawar
A: 

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

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

Abramodj
A: 

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

Benoit Caccinolo