Debugging memory leaks with Instruments and Heapshots

I was first introduced to Instruments in the CS193P course that I took over the past few months.   I found them to be invaluable for debugging memory leaks as I work on my Address Book iPhone app.

iOS Memory Instruments

There are some good references on the Apple web site on using Instruments:

Instruments User Guide

Instruments User Reference

Debugging a real leak

One of the things you can look at in the Leaks Instrument is your Allocations.  The instrument tracks how many bytes are living, allocated then released, etc.

Leaks Instrument - Allocations

There is documentation in the section on the Allocations Instrument for Displayed Information to tell you what the different columns mean.

I noticed that over time, the number of Live Bytes was increasing as I added test data to the Address Book.  Suspecting a leak, I looked at the Leaks tab:

Leaks Instrument 1

Sure enough, I had a leak that was occurring with multiple instances of a string.

By diving down into the leaked object, I was able to find the offending code fairly easily:

Leaks Instrument 2

It turns out that for some reason adding information to the Organization property (kABPersonOrganizationProperty) in a new Address Book record is causing my problem.  Knowing this I can look closer at this section of code to identify the problem.

Using Heapshots

I was first introduced to Heapshots after reading the excellent article:

When is a Leak not a Leak? Using Heapshot Analysis to Find Undesirable Memory Growth

A Heapshot is basically a snapshot of the state of your heap that shows growth since the previous Heapshot.    I discovered that some increases in the heap in my application were not showing up as true leaks, yet they definitely represented memory that was being unexpectedly held onto.

I like to take a Heapshot when the Instrument is first fired up.  I then take Heapshots periodically after some event occurs in my application.

Debugging with Heapshots

I noticed that I was seeing my heap unexpectedly grow at certain times.  Ideally the Heap Growth should be 0 bytes after your application has reached a steady state.

Heapshot - snapshots

With Heapshots you are able to dive into a specific Heapshot and look at the function call tree to help isolate the problem.   In order to see the call tree, you must click on the right arrow to the immediate right of “Heapshot X”.  If you click on a right arrow on one of the sub views, you will not see this call tree.

Heapshot - call tree

Once you’ve identified a good starting point in the function call tree, you can look at the associated code by double clicking the function name:

Heapshot - code view

UIImagePNGRepresentation, UIImageJPEGRepresentation

In one case I was able to trace issues to calls to UIImagePNGRepresentation() (UIImageJPEGRepresentation() had the same issue).

A search showed that others have had the same issue with these functions, though I haven’t seen a satisfactory solution yet:

Objective C UIImagePNGRepresentation memory issue (using ARC)

memory problem when saving images with UIImagePNGRepresentation

Memory issue in using UIImagePNGRepresentation

Advertisements

4 responses to “Debugging memory leaks with Instruments and Heapshots

  1. I did some more digging and did find a bug related to setting a value for the Organization property (kABPersonOrganizationProperty) when also setting the kABPersonKindProperty value to kABPersonKindOrganization.

    I created a project to illustrate the problem and uploaded it to git@github.com:scottcarter/AddressBookOrganizationBug.git

    A bug report was also filed with Apple.

    Like

  2. I sent mail to Michael Jury @ AAPL about the final issue; did you ever discover how to address the UIImagePNGRepresentation problem?

    Like

  3. Hi Chris,

    Unfortunately I did not come up with a way to address the problem with UIImagePNGRepresentation.

    Like

  4. Pingback: The development of Contacts2Web | Finalize.com: My journey with iOS and other code adventures

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s