When we are taking a shot at iOS application, we are thinking of new features for making our app great. Apart from improving / enhancing our app, we ought to instrument its code also. The Instrument allows you to check your code for performance issues, memory issues, reference cycles, and other problems. With the help of these instruments, we can bug-free potential applications as best as possible.
Xcode incorporates a performance tuning application named Instruments that monitor your app’s network and file I/O activity.
Let’s investigate it one-by-one and its usage:
1. Activity Monitor
Activity Monitor is an awesome utility to monitor what type of applications and services are chewing up CPU cycles, energy hogs, and other things that might slow your Mac down. Using activity monitor one can figure out why iPhone was chewing the battery when comes in sleeping mode. When Instruments comes in a sleeping mode, One can easily monitor devices and its processes.
The Allocations instrument captures information about memory allocation for an app. You can inspect that everything is in sequence or not. If you happen to be working on Swift, all of your Swift objects will get prefixed with the application name. If you are working on Objective-C, it’s a little bit difficult to find, essentially have to know the names of the things you are looking for. If you happen to have prefixed your files you can search on that prefix and see all your objects that way, or you can seek on some naming scheme you might have used.
If you click on the little arrow next to the object, it’ll show you all the allocations of that object, and who is responsible for creating it. Then, if you click on the little arrow next to one of those allocations, you can see the detailed information about its retain and release count. If that number doesn’t reach 0, it won’t get deallocated.
It includes Allocation and Leak templates that will help you see all the memory allocations and potential leaks that are happening.
You cannot get an excessive amount of detail on the Leaks panel, but it will take snapshots for you, at specified intervals, to see if you have any memory that has been allocated yet not able to deallocate. This often happens in the case of Objective-C and using some of the C libraries where you are responsible for releasing any allocated memory. If you are working on Swift, these leaks are less common as it takes care of some of the things you used to do with Objective-C.
4. Timer Profiler
At the time of running your application, sometimes you can be delayed in performing some task. If you want to examine that, the Timer Profiler instrument is very beneficial. It will be more effective, how to make your apps faster, more efficient and can be used to identify problems and verify your fixes. It is easy to improve your app’s power usage and performance.
5. Energy Log
It monitors factors that influence energy usage on an iOS device, including CPU activity, network activity, screen brightness, and more. Keep note that this tool doesn’t supports in simulator.
It gathers data from your running app and presents it in a graphical timeline.
Identify the areas where usage is highest, and see if you can reduce impact in those areas.The Energy Usage instrument indicates a level from 0 to 20, indicating how much energy your app is using at any given time.
6. Network Connection
The Connection instrument captures information about TCP/IP and UDP/IP networks activating over time. Like Energy Log, this tool also doesn’t support in simulator. The timeline pane displays graphs that represent the amount of network activity in the following categories:
7. System Trace
This template provides comprehensive information about system behaviour. It shows when threads are scheduled, and shows thread transitions from user to system code via system calls and memory operations. This template may be used with OS X or iOS.
8. OpenGL ES Analyzer
The OpenGL ES Analyzer instrument captures information about OpenGL ES activity. It detects problems and offer solutions based on best practices and knowledge of Apple’s hardware and software platforms. Again, this tool also doesn’t supports in simulator.
It displays following trace details:
9. System Usage
This template monitors an app and records system I/O activity related to files, sockets, and shared memory. This includes inputs, outputs, duration, backtrace, call tree, and more for each call. This template may be used with iOS only.
The I/O Activity instrument captures information about I/O events.
10. GPU Driver
Monitors GPU hardware activity. This Instrument monitors GPU-side events. Keep note that this tool doesn’t support in simulator.
The following GPU activity columns are available in the detail pane.
This template measures general memory usage of an app, focusing on the detection of over-released objects, known as zombies. It provides statistics on object allocations by class, as well as memory address histories for active allocations. This template may be used with OS X or iOS.
12. Core Animation
This template uses time profiling to measure graphics performance and CPU usage in an app. This template may be used with iOS only. This tool doesn’t support in simulator.
The Core Animation instrument captures information on selected animation statistics. You may see spikes in the timeline pane where the frame rate of the app becomes significantly better. In some cases, however, spikes can be caused by changing the device between landscape and normal orientation—and if you don’t know this, you might spend time trying to find what caused the performance increase.
It is a powerful tool you can use to collect data about the performance and behaviour of one or more processes on the system.
14. Core Data
The Core Data Fetches instrument that captures information about data store fetch operations performed by Core Data applications. It can record information from a single process or all processes. The all-processes option records information only from running Core Data applications. You can export recorded information to a DTrace script. This tool doesn’t support in simulator.
The timeline pane displays the following information, based on the instrument’s configuration:
It also records the information regarding:
15. File Activity
In file activity, It captures information about file open, close, and start operations.
In read/ write, it captures information from reads and writes to files. This includes the read, write, pread, and pwrite functions. This instrument uses DTrace in its implementation and can be exported to a DTrace script. Keep note that this tool doesn’t support in simulator.
In File Attributes, it captures information about changes to ownership and access permissions for files. You can export recorded information to a DTrace script.
In Directory I/O, it captures information about directory operations, specifically the delete, link, mkdir, mount, rename, rmdir, symlink, unlink, and unmount functions.
The Counters instrument records information from user-managed PM (Performance Monitor) events. Keep note that this tool doesn’t support in simulator.
17. Cocoa Layout
The Cocoa Layout instrument captures information on changes to NSLayoutConstraint objects. Keep note that this tool doesn’t support in simulator.
18. Metal Application
Records Metal application events. It displays a graph indicating Metal application events.
This article demonstrated the instrument tools in Xcode. Although it’s embedded within and may be used with Xcode, Instrument is a separate app, which may be used independently as needed.
Incorporating Instruments into your workflow from the beginning of the app development process can save you time later by helping you find issues early in the development cycle.
Once you become more comfortable with the tooling, you’ll end up using it more, and the better your code will become. You’ll become more proactive about checking on things that you know might cause an issue and it’ll become part of your standard workflow.
Hopefully, all of this will help you track down and fix those memory leaks.
At 9series, our smart developers are using these tools to check application usage, performance of app, memory leak, etc… based on application usage, requirement and use-cases.
Happy Healthy Coding !!!