Guides and Sample Code


Instruments User Guide

Find Abandoned Memory

The Allocations profiling template uses the Allocations and VM Tracker instruments to measure general and virtual memory usage in your app. However, to track down abandoned memory that’s been allocated but isn’t needed again, focus strictly on the Allocations instrument. This instrument measures heap memory usage and tracks allocations, including specific object allocations by class.

Because abandoned memory is still referenced by your app, Instruments can’t determine its importance. To find abandoned memory, use generational analysis to ensure that memory does not continue to grow while repeatedly performing the same set of operations. For example, ending and starting a new game, opening and closing a window, creating and deleting a contact, or setting and unsetting a preference are all operations that should conceptually return your app to a previous and stable memory state. Extensive cycling through such operations many times should not result in unexpected or unrestrained memory growth. Instruments helps you correlate periods of memory growth with specific object allocations, so you can release them and reduce your app’s memory footprint.

To look for abandoned memory in your app
  1. Launch Instruments.

  2. In the profiling template selection dialog that appears, click Allocations.

  3. Choose your device and app from the target device and process lists.

  4. Click Choose to create a trace document.

  5. Click the Allocations instrument in the timeline pane.

  6. Press Command-2 to show the display settings area of the inspector pane.

    This gives you quick access to the Mark Generation button, which you use to flag different generations of actions as you profile your app.

  7. Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.

  8. Perform a short sequence of repeatable actions in your app.

    In order to accurately generate trends, this should be a set of actions that starts and finishes with the app in the same state.

  9. Click the Mark Generation button in the inspector pane.

    A flag appears in the track pane to identify the generation.

    A list of generations you’ve marked is shown in the detail pane. Each generation includes a list of allocations that has occurred since the previous generation.

    You can also mark generations after you’re done recording by dragging the inspection head in the track pane’s timeline to the desired location and clicking Mark Generation.

  10. Perform steps 8 and 9 several times while monitoring the detail pane until you see whether memory is growing without limit.

  11. Click the Stop button (image: ../Art/inline_stop_button_2x.png) in the toolbar (or press Command-R again) when you’re ready to stop recording.

  12. Scan through the generations in the detail pane and find one that seems representative of repeated memory growth.

    The Growth and # Persistent columns tell you how much additional memory and how many allocations have occurred since the previous generation. If your app returns to its original state after an operation, you shouldn’t expect there to be growth from generation to generation.

  13. Click the disclosure triangle (image: ../Art/inline_disclosure_triangle_2x.png) of a generation to display new objects that have been allocated since the prior generation.

  14. Look for objects that are persisting. If you identify one, click the disclosure triangle (image: ../Art/inline_disclosure_triangle_2x.png) to display its instances.

  15. Select an object instance.

  16. Press Command-3 to display a stack trace for the selected instance in the extended detail area of the inspector.

    This stack trace provides the complete list of method calls responsible allocating the instance.

  17. Click the Collapse button (image: ../Art/inline_hidesystemcalls_button_2x.png) in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.

    Calls made by your app are colored black and preceded by a user code icon (image: ../Art/inline_usercode_icon_2x.png).

  18. Double-click a method in the stack trace to display its code in Instruments.

  19. Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for editing.

  20. Determine whether the allocation is useful. If it’s not, it’s abandoned memory that needs to be resolved.