Improving iOS performance with Xcode and Instruments for dummiesEstimated reading time: 7 mins
Hi friend, is your app running a bit slow? Are you, perhaps, unsure what to do about it? Where do you start to investigate? Perhaps, like me, you started searching 🕵️️ in your code for something evident? (Only to realize that you are going down a never ending rabbit hole?)
Well, stress not my friend because Xcode got you covered ✅ and it will help you find your issues (or put you on the right track at least).
This is a second article that I am writing about an Integrated Development Environment (IDE). The more that I use Xcode, the more I like it 😎 Xcode is really, really awesome. Just give it a try and use it.
Side note: it only took me 182 hours to get to the Instruments panel.
I’m actually really surprised at the amount of tooling that is given to you in this wonderful IDE. My software engineering training gave me my introduction to IDEs via Eclipse and NetBeans. I’ve spent years using those tools (and don’t get me wrong, they are powerful). But to be completely honest, to this day I will pick anything else but these two. They are ugly, clunky, and not intuitive 🌶
Why did I type that paragraph above?
Because if you have an aversion to IDEs and think that Xcode looks just like the other IDEs, then I am here to tell you that it really is not. Xcode IS a pleasure to use and I use it every day. (PS: If you are getting angry while reading this and saying !?Xcode sucks!? Don’t get so upset. I will admit, it is not perfect.)
It is this powerful thingy, built on top of DTrace, that comes with Xcode. No need to install extra software. Just download Xcode once and you have everything you need. Don’t have internet connection or you don’t want to be connected? No worries, you can still DO your work.
The following text is basically my brief overview of Instruments.
When you open Instruments, it looks like this:
You can launch Instruments like this:
Or like this:
(I didn’t want to leave you waiting while my slow computer takes its time to compile and open up… but you get the idea)
What you do next is kind of interesting. All of the icons that you see above represent the different kinds of tool that you can run on your application. (I think of this screen as a collection of apps for your app). Hopefully you will understand why I say on your application.
Out of all of the available options, I’ve only gotten familiar with Activity Monitor, Allocations, Leaks, and Time Profiler. Oh yes, not only are these tools wonderful but the naming conventions are intuitive and grouped by colour. Amazing.
Activity Monitor and Time Profiler are in blue. These blue thingys are used to monitor CPU stuffs.
Allocations and Leaks are in orange. These orange thingys are used to monitor memory stuff. (Guess what this other orange Zombies thingy looks at? Did you guess RAM stuffs? Cool right!!!)
You might be wondering to yourself… all of these thingsys are cool sounding, so what? What do I do with them? Well the cool thing about these thingys is that you can just JUMP in and you will figure things out as you go. It is that easy.
I suggest that you play with the other tools available. I am going to focus on the Time Profiler for the rest of this article. At this point in the learning, I found it to be the most useful for me.
Time Profiler - I’ve been using this for…profiling… time? 🤷 Basically, I open it up and it looks like this:
You might be asking, now what? How about we hit that big Red Record button ⏺ ? Yup, and it will look like this:
Now what? 🤔
Now you use your app! What Instruments, or should I say Time Profiler, does in the background is it samples your environment… Time Profiler records what it sees your app is doing during a specific time interval. Makes sense?
And if you have ever used software that has some kind of manipulation of a timeline, then you will be just fine.
So this is a snapshot of how your app behaves, or its environment:
time profiler records many such samples at a regular interval over time:
until you press the stop button ofcourse:
Now what? 🤔
Now you get to take off your developer hat 🎩 and play a detective 🕵️ … for real… how cool is that?
Since we are exploring the Timing Profiler, we are interesting in timing. You can open up (expand) the tree, that Xcode conveniently made for you, and go exploring 🗺. See which areas or which methods in your code take a long time. It very simple. Just click expand ⏬ and dive 🏊 in:
Don’t know where to start? Try this:
- Click record
- Play with you app as regular user (try various uses case)
- Stop the recording
- And then… you can filter specific time intervals!! How cool is that?!?!?!
Here is an example of me recording as I create a new to-do in dailyvibes and then filtering various time intervals. You can really isolate the various time intervals, like so:
As you can see, to filter you simple Click and Drag on the timeline… just like any other application with a timeline. It is just that simple. And, when you filter, if you notice, the tree changes. Thus you can really get into the nitty gritty details and find out your problems 🚧.
Oh, and… and… and you can SAVE this sampling and come back to it later:
Here is an actual example of me diving deep into the tree 🌲. When you find something juicy, like this:
You can… double click on the method, and it will show you EXACTLY which lines 👀 of code that may be troubling:
There you have it friends. I’d like you to take away three things from this:
- Xcode is pretty awesome 🔥
- Be a digital detective 🕵️ and give Instruments a try
- Be careful ❌ of premature optimization
If you liked this, please let me know. I like hearing feedback so I can improve. You can find me on twitters.
Until next time,
PS: I hope I did not offend you with the *for dummies tagline. I love technical documents but I feel like they need a bit of flavour sometimes. I simply want this technical mumbo-jumbo to feel relatable and friendly.
Update: This article was also posted on medium