Instruments_iOS (1)

Performance Analysis through Instruments tool for iOS App

 

Instruments Tool

Instruments is a powerful and flexible performance-analysis and testing tool. It is a  part of the Xcode tool set. Instruments tool is designed to help you profile your iOS applications in order to increase their performance and better utilization of resources.

We will see

  1. How to  find hot-spots in code using Time profiler instrument.
  2. How to find and fix the memory problems like strong reference cycle using Allocations instrument.
  3. How to reduce the memory waste by finding the abandoned memory in application using Allocations instrument.
  4. Finding memory allocations which are deallocated and still referenced by our application using Zombies instrument.

Launch Instruments from Xcode

To launch Instruments from the Xcode menu

  1. Open Xcode.
  2. Choose Xcode > Open Developer Tool > Instruments.

1

To profile an opened Xcode project

  1. Launch Xcode and open your project.
  2. Do one of the following:
    • Choose Product > Profile.

Instruments-2

    • Click and hold the Run button in the Xcode toolbar and choose Profile.

Instruments-3

  • Press Command+I.

Xcode builds your project, Instruments launches, and you’re prompted to select a profiling template.

Instruments Tool sections

instruments-1

  • Recording control: Start/Stop and Pause the recording.
  • Timer: Running time and number of times application tested.
  • Track: Graphical Representation for selected tool w.r.t. time.
  • Details Panel: Detail output of the particular instrument.
  • Inspectors Panel: Record Setting, Display Setting and Extended Details.

CPU Uses

In Instruments tool, we can conduct testing under variety of conditions. We can also identify that how well the application uses the threads, cores and other resources. We can balance the work with multiple threads and offload work from main thread making our app more responsive. The Time Profiler instrument shows how well an application utilizes CPU cores and threads.

Steps to find CPU Use

  1. Launch Instruments tool using any one of the methods described above.
  2. When it asks to select a template, click Time Profiler.
  3. Choose your device and app from the target device and process lists if they are not selected.
  4. Click the Red Record button in the toolbar or press Command+R to begin recording.
  5. Use your app or the system normally.
  6. Click the Black Stop button in toolbar or press Command+R again when complete.
  7. Click the CPU or threads strategy button in the trace document toolbar.
  8. Examine the collected data.

In Detail panel, you can see the time take by each method in your application. if you want to open code then you can double click on any method and if you want to edit the code you can click small xcode icon and modify the code where needed.

Memory Leak

When we allocate some memory in our application but we never release them and the memory no longer being referenced by your app then this is called memory leak. Since we do not have any reference to that allocated memory, we cannot release it and the memory cannot be used again by application. In Instruments tool, we have Leaks template to find the memory leaks in an application.

Steps for finding memory leaks

  1. Launch Instruments tool using any one of the methods described above.
  2. When it asks to select a template, click Leaks.
  3. Choose your device and app from the target device and process lists if they are not selected.
  4. Click the Red Record button in the toolbar or press Command+R to begin recording.
  5. Use your app normally.
  6. Watch the Leaks instrument in the timeline pane for leaks. A leak appears as a red bar.
  7. Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.
  8. Choose Call Tree from the detail type list in the navigation bar of the detail pane. This displays a list of method calls related to any detected leaks.
  9. Press Command+2 to show the display settings area of the inspector pane.
  10. Under the Call Tree display settings, select Invert Call Tree and Hide System Libraries.
  11. In the call tree, select a method call you want to investigate.
  12. Press Command+3 to display a stack trace for the selected method call in the extended detail area of the inspector.
  13. Double-click the method call in the stack trace to display its code in Instruments.
  14. Click the small Xcode button at the top of the detail pane to open the code in Xcode for review and editing.

Abandoned Memory

Abandoned Memory is the memory that is allocated by your application, but it no longer needed and also won’t be referenced. Instruments cannot find abandoned memory because it is still referenced by your application. So for finding abandoned memory, we can use generational analysis. if memory continuously grows while performing some set of actions repeatedly.

Steps for finding Abandoned Memory

  1. Launch Instruments tool using any one of the methods described above.
  2. When it asks to select a template, click Allocations.
  3. Choose your device and app from the target device and process lists.
  4. Press Command+2 to show the display settings area of the inspector pane.
  5. Click the Red Record button in the toolbar or press Command+R to begin recording.
  6. 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.
  7. 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.
  8. Perform steps 6 and 7 several times while monitoring the detail pane until you see whether memory is growing without limit..
  9. Click the Black Stop button in the toolbar or press Command+R again when you’re ready to stop recording.
  10. 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.
  11. Look for objects that are persisting. If you identify one, click the disclosure triangle to display its instances.
  12. Select an object instance.
  13. Press Command+3 to display a stack trace for the selected instance in the extended detail area of the inspector.
  14. Click the Collapse button 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.
  15. Double-click a method in the stack trace to display its code in Instruments.
  16. Click the small Xcode button at the top of the detail pane to open the code in Xcode for editing.
  17. Determine whether the allocation is useful. If it’s not, it’s abandoned memory and you should resolve it.

Note:  During the first few iterations, extra allocations may occur due to caching. Therefore, it is important to create a few initial generations for the purpose of establishing a baseline. Then, create additional generations for true analysis.

Zombies

Zombie is the memory that has been released by your application but still referenced somewhere. Using that reference in your application will cause crash. In Instruments tool we can use Zombies profiling template to detect zombies in the application. It will enable NSZombie and convert each released object to type of NSZombie, so whenever your application try to call a released memory the NSZombie object will be called and Instrument will notify us.

Steps for finding Zombies

  1. Launch Instruments tool using any one of the methods described above.
  2. When it asks to select a template, click Zombies.
  3. Choose your device and app from the target device and process lists.
  4. Click the Red Record button in the toolbar or press Command+R to begin recording.
  5. Use your app normally.
  6. If a call is made to a deallocated object, a flag is inserted in the timeline pane and a Zombie Messaged dialog appears, indicating that a message was sent to a deallocated object at a specific memory address.
  7. If you close the Zombie Messaged dialog, you can reopen it by clicking the flag.
  8. Click the focus arrow next to the zombie’s memory address to display the memory history of the zombie object in the detail pane, along with corresponding reference counts and method calls.
  9. Select the Zombie event in the detail pane.
  10. Press Command+3 to display a stack trace for the selected event in the extended detail area of the inspector.
  11. Double-click a method in the stack trace to display its code in Instruments.
  12. Click the Xcode button at the top of the detail pane to open the code in Xcode for editing.

Leave a Reply