On my last co-op term this past summer, one of my assigned projects was to develop an iPhone application to serve the purpose of an education tool. After four months of programming in Objective-C under the iPhone SDK, I’ve come to realize that mobile development can have it’s benefits especially in an environment where smart phone usage is substantially increasing.
As many software developers are aware, the use of documentation is very important for others to understand, use and adapt the function they are using without reviewing all necessary source code. My documentation was one of the last assignments I was given to ensure all my work was comprehensive and transferable to the next person that will continue the project in my absence.Therefore, I’ve prepared a tutorial that will help improve the performance of an application.
Below is an introductory tutorial for finding memory leaks in iPhone applications using Instruments. This is a powerful tool to prevent your application from reaching its memory limitations:
Finding Leaks Using Instruments:
Bugs that should always be fixed are memory leaks, which are blocks of allocated memory that is of no use for the program. While debugging in an iPhone application, the Instruments application can be used to identify memory leaks. More specifically, Instruments is a powerful tool that allows the user to monitor the process of data collection as well as understanding both user program and operating system behavior.
The following readings from the iPhone Dev Center are recommended to have a more in depth understanding of using Instruments. If any of the links are broken, please proceed to the dev center to obtain the latest version.
- “Introduction to Instruments User Guide”
- “Instruments Quick Start” a brief introduction to using Instruments and its main window.
- “Adding and Configuring Instruments” how to add and configure Instruments to run.
- “Viewing and Analyzing Trace Data” tools used to view the returned data from Instruments
In addition to the iPhone Dev Center, the following sites are recommended as useful resources for tutorials and further investigation.
The following skills are recommended for using this tool:
- Debugging to reduce number of bugs, defects
- Apple software development tools (Xcode, Interface Builder, iPhone Simulator)
- Previous Obj-C experience
- Knowledge of garbage collection and memory leaks
- Previous experience with application performance analyzers
Basic Fundamentals of Instruments:
Launching the Application:
To launch the application, open the Finder and locate its Developer Folder. The Instruments application should be located in Developer/Applications.
An alternative method is to open the application within Xcode. In Xcode, select Run > Run with Performance Tool > Leaks. Instruments will then launch with the necessary templates to identify leaks for the current executed program in Xcode.
Trace Document Layout:
A trace document is a window to display the collection of data. The document is used to view data from both high and low frequencies.
The Trace Document:
The trace document features the following components to organize and configure the instruments needed to collect data:
- Instruments pane: holds the instruments required to run. You can configure the instruments in this pane from adding or deleting them.
- Track pane: displays the summary of data collected by the instruments in use. Information is read-only.
- Detail pane: displays the details of data collected by the instruments in use. Shows the ‘events’ used for the graphical view.
- Extended Detail pane: shows information on the currently selected item in the details pane.
The figure below represents some of the key features of the Instruments window while tracing:
The Instruments Toolbar:
The toolbar for the trace document is used to add and control instruments, open views, and configure the track pane. The figure below represents the different controls on the toolbar:
- Pause/Resume button: pause and resume data collection during recording.
- Record/Stop button: record and stop the data collection process.
- Loop button: sets a loop to repeat the recorded steps continuously.
- Default Target menu: selects the trace target for the document.
- Time/Run control: displays the completed time of the current trace.
- Inspection Range control: selects a time range for the data collection.
- Mini button: replaces the trace document with a smaller window.
- View button: controls to either show or hide the details and extended pane.
- Library button: hides or shows the library.
Saving and Importing Trace Data:
Using Instruments allows the user to save and import trace data. After recording the trace data you can save the document as well as the instrument configuration for future references. The following instructions will explain how save and export data for other applications to read.
Saving a Trace Document:
To save a set of instruments along with the data collected after a trace, select File > Save. Instruments should save the trace file with a .trace extension.
Exporting Track Data:
To save the track data to a CSV file, select the Instrument with the data you want to be exported and choose Instrument > Export Data for <Instrument Name>. The data of the most recent run of that instrument will be exported.
Importing Data from the Sample Tool:
To import data from another application, select File > Import Data. Instruments will demand a text file that contains the sample data to create a new trace document based on your selection.
Hope this tutorial contributes to a successful application! Feel free to ask any questions.