Allocations Instrument: Generations

February 16th, 2016

Filed under: Instruments | Be the first to comment!

I couldn’t cover everything about Instruments in my Getting Started with Instruments article. Having gotten the basics out of the way, I can now write shorter, more focused articles on Instruments. This is the first of those articles, showing how to measure heap memory growth using the Allocations instrument.

Measuring Heap Memory Growth

Measuring heap memory growth with Instruments entails the following high-level steps:

  1. Start recording with the Allocations instrument.
  2. Pause recording.
  3. Take a snapshot.
  4. Resume recording.
  5. Perform the activity in your application whose memory usage you want to measure.
  6. Go to Step 2.

Translating these steps to Instruments is mostly straightforward. Click the Record button to start recording. Click the Pause button to pause and resume recording. The only step that could be tough is figuring out how to take a snapshot. To take a snapshot, click the Mark Generation button, which is in the Display Settings section.

MarkGenerationButton

When you click the Mark Generation button, the detail view switches to the Generations section. The Generations section has a listing for each snapshot you’ve taken. When you take a snapshot, Instruments gives it a generic name like Generation A. You can give a snapshot a more meaningful name by selecting it and pressing the Return key.

Examining the Results

The detail view shows the following information for each snapshot:

  • A timestamp of when you took the snapshot.
  • Growth, which is the amount of memory allocated since the previous snapshot. For the first snapshot, the previous snapshot is when you started recording.
  • # Persistent, which is the number of memory allocations since the previous snapshot.

The Growth column is the most important column. It tells you the amount of heap growth.

Showing the Call Tree

If you move the mouse cursor over the name of a snapshot, a small button with an arrow facing right appears. Clicking that button shows the call tree of memory allocations that occurred during the snapshot period. Use the call tree view to find the areas of your code that allocated the memory.

Showing Memory Categories

Next to each snapshot is a disclosure triangle. Clicking the disclosure triangle shows the memory categories in that snapshot. Instruments shows the number of memory allocations and amount of allocated memory for each category.

Most memory categories are the names of Apple’s classes and data structures. The prefixes CF, NS, and UI indicate Apple data structures. Virtual memory allocations have the category VM:.

Each memory category has a disclosure triangle next to it. Clicking the disclosure triangle shows each memory allocation in that category. You can see the size of each memory allocation. If you open the extended detail view, you can see the call stack for each memory allocation.

Accessing More Data on a Memory Category’s Allocations

Moving the mouse cursor over a memory category makes the small right arrow button appear. Clicking that button shows the following information for each memory allocation in that category:

  • The memory address.
  • The category, which will be identical for each listing.
  • The timestamp.
  • Live. If there’s a dot in this column, this memory allocation has not been freed, meaning the memory allocation is still alive.
  • The size of the allocation.
  • The library that made the allocation. For memory allocations your application directly makes, the name of your application will be in the Responsible Library column.
  • The function that made the allocation.

Showing Reference Count Histories

Moving the mouse cursor over a memory address makes the small right arrow button appear. Clicking that button shows the reference count history for that memory address.

One Final Note

What I talked about in the previous two sections doesn’t just apply to the Generations section. If you click in the jump bar and choose Statistics, you can also use the techniques I described in the previous two sections. Click the small right arrow button next to a memory category to get information on every memory allocation in the given category. Click the small right arrow button next to a memory address to examine its reference count history.


Leave a Reply

Your email address will not be published. Required fields are marked *