24

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?

4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • 1
    Are you looking at the actual "leaks" data or are you looking at the Object Allocations data? If you're looking at the latter, then yes, it's normal... those aren't leaks, just allocations. It gives you an idea of your memory footprint. – Jason Coco Jan 30 '09 at 03:41
  • I'm looking at leaks. The only thing there of mine is main. The number of leaks and bytes continue to increase as I use the app. I don't understand how main could be leaking this much. – 4thSpace Jan 30 '09 at 07:10
  • make sure all alloc are being released. If not you are creating memory leaks. – Niels Hansen Jan 31 '09 at 23:18

13 Answers13

29

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
  • 414
  • 3
  • 4
  • Nice tutorial. Just remember you can *skip a lot of steps there* if you just use Xcode to load Instruments by going on **Help and typing "Leaks"**. It will show you the menu option that will load it up with all set. – cregox Jan 12 '11 at 12:52
  • This link is not working anymore, can you please update. – Noitidart Aug 05 '19 at 19:10
5

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
  • 25,525
  • 19
  • 65
  • 75
  • @Ben Thanks. I'll try it. @lajos Please see the part where I say, " I have Extended Detail pane displayed". – 4thSpace Jan 30 '09 at 03:25
3

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

Xorsat
  • 2,388
  • 21
  • 21
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.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
  • 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 Aug 30 '10 at 03:52
  • True. A year and a half ago when I posted this, it wasn't included in Xcode. – Sophie Alpert Aug 31 '10 at 07:45
2

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

Genericrich
  • 4,611
  • 5
  • 36
  • 55
2

Use LLVM/Clang Static Analyzer.

1

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
  • 74,769
  • 26
  • 128
  • 150
  • Great point! To find those I sometimes add NSLog calls to dealloc to print retain count. – lajos Jan 30 '09 at 15:08
1
Run -> Start with Performance Tool -> Leaks
Vikdor
  • 23,934
  • 10
  • 61
  • 84
Shawn
  • 21
  • 1
  • 3
0

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.

user2998756
  • 71
  • 3
  • 5
0

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
  • 5,709
  • 9
  • 49
  • 75
0

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

0

The memory debugger (button just over the console, next to the view debugger) is quite useful too. It will show you leaks, and you can check /filter easily if objects are still in memory when they shouldn't.

Frederic Adda
  • 5,905
  • 4
  • 56
  • 71
0

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.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Shawn
  • 21
  • 1
  • 3