ARTICLE

iPhone Memory Leaks Tracking and Use of Instruments

Posted by Monish Bansal Articles | iPhone/iPad February 14, 2013
This article shows the use of Instruments for tracking application memory leaks.
Reader Level:

iPhone Memory Leaks

I have used Instruments and NSZombie for tracking application memory leaks. In this article I will use Instruments for the tracking. Instruments has the following features:

  1. Memory allocation (virtual memory and real memory allocation)
  2. Leaks in memory
  3. Activity Monitor
  4. System Trace
  5. Core animation
  6. Blank

What memory leaks are and the causes of memory leaks

A memory leak is when your program loses track of a piece of memory that was allocated. The consequence is that the "leaked" memory will never be freed by the program. This usually happens when a piece of code does a "new" or a "malloc" (or an "alloc" in Objective-C) but never does a corresponding "delete", "free" or "release", respectively.

When you new, malloc, or alloc, what the OS is doing is giving your program a chunk of memory on the heap.  It expects you to hold a reference to that memory address (usually in the form of a pointer) and it's relying on you to tell the OS when you're done with it (by calling free, delete, or release).

Memory leaks happen when you throw away your pointer to that memory. If your program no longer knows where in the heap your memory is allocated, how can you ever free it?

In simpler language, memory leaks mean you already freed the pointer and now the OS doesn't have a reference of that pointer.


Example

I am creating an example application that leaks memory in two places. 

Blank


And there's a LeakedObject class.


Instruments

Launch Instruments. Here you will see many tools in Instruments for various tasks. On the left-hand side, choose iPhone. On the right-hand menu, double-click the "Leaks" tool:

Instruments_01.jpeg


Once you've clicked that, you should see the following screen:


Instruments_02.jpeg


Please connect the device with the system. In the top-left corner of the window you'll see a drop-down menu that says "Launch Executable". Click on that and ensure that your iPhone (not your computer) is selected as the active device. Then scroll down to "Launch Executable" and you should see a list of all the apps that are installed on your iPhone. Find your app and select it.


Instruments_03.jpeg

After the app/game has run for a few seconds, the auto leak check will run and lo and behold, it will find 2 memory leaks.

Instruments_05b.jpeg


Click on one memory leak. The Detail View will show you a complete stack trace to the point where your leaked memory was allocated. In our example above, clicking on the first leak reveals that a leak occurred inside [NSString initWithUTF8String]. If you look one step higher in the stack trace, you'll see the last call inside my app was to [InstrumentsTestViewController viewDidLoad].

Instruments_06b.jpeg


Double-click on that line in the selected Detail View and it opens an XCode window right to the culprit!

Login to add your contents and source code to this article
post comment
     

Yes we check memory leaks in every app. Initial load time is default and we cant change it. And in some cases app can crash due to high memory use. an example if you check Image album in facebook app and change image frequently then app will crash because it saves images in cache and app will crash. This is the limitation of smart phone devices.

Posted by Monish Bansal Feb 19, 2013

Great informative article. Now question is, can you build app smart enough not to crash and know there are memory leaks and close the leaks or that function. As a user, there are two things you do not want. Wait for app to load (initial load, or any screen or any data) and second crash. I am aware that iOS will close your app if it does not respond within certain time (seconds, not sure actual time).

Posted by Mahesh Chand Feb 15, 2013

Nice article it is really helpful for beginner in ios technology...Thanks Monish sir!!

Posted by Sachin Bhardwaj Feb 15, 2013

In This condition app will not have free memory and it will show memory warning. and sometime app can be crash or app performance will very low.

Posted by Monish Bansal Feb 14, 2013

There are actually many possible causes of memory leaks. The most common probably is when the programmer simply forgets to do the delete/free. Sometimes it is conditional in the sense that a function might be returned from prior to the end of the function and the memory is not freed whereas it is freed at the end of the function.

Posted by Sam Hobbs Feb 14, 2013
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter