This Wednesday Wisdom deck goes into the details of iOS App Performance Optimization with Xcode Instruments. Uncover the secrets to faster, more efficient apps by troubleshooting CPU usage and memory issues. This deck simplifies the process, making it accessible to all. Discover how to utilize Instruments effectively and prevent memory leaks, with practical insights and tips.
Wednesday is a leading Product Engineering Agency. We have worked with over 50 Global Brands.
Due to our product thinking expertise, Wednesday has been the agency of choice for fast-growing startups. Over 10% of India's Unicorns are our customers.
We are known for our expertise in:
1. Data Engineering: Using DataOps principles, we build data pipelines that are cost-effective and performant and allow you to make strategic decisions.
2. Applied AI: We use large language models and your proprietary data to build data-centric intelligent apps for your customers.
3. App Development & Modernization: We use our expertise in strategy, product development & design to build web, mobile, TV & IoT Apps.
We offer our expertise via the following services:
- Launch: Idea from napkin sketch to product market fit.
- Amplify: Engineering & design for companies with product market fit.
- Control: Fast-paced, tight-deadline projects suited for enterprises.
- Catalyse: Staff Augmentation
If you want to explore an opportunity to work with Wednesday, send your resume to careers@wednesday.is.
3. 3
Today’s Plan
What we’ll be covering
What Instruments is and why do we use it
How to check your code for performance
issues and memory issues
The best way to debug these
issues
4. wednesday.is
iOS is so fast, why
bother
What is instruments
Instruments is a powerful performance
analysis and testing tool that's part of
Xcode toolset. Instruments can help you
profile your apps on all platforms - iOS,
macOS, watchOS, tvOS - in order to
better understand and optimize their
behavior and performance.
Why do we need to use it ?
• Track down problems in your source code
• Analyze the performance of your app
• Find memory problems
6. You can access the instruments by using
Product ➔ Profile from the Xcode
menu for which the shortcut handle is ⌘ + I.
This will rebuild your product and opens the
Xcode Instruments overview.
wednesday.is
How can I access Xcode
Instruments?
An overview which shows 17 di
ff
erent instruments
An overview which shows 17 di
ff
erent instruments
7. wednesday.is
Time Profiler
1- Recording controls:The record button stops and starts the app
currently under test.
2 - Run timer:The timer counts how long the profiled app has been
running and how many times it has run.
3 - Instrument track:This is theTime Profiler track.You’ll learn more
about the specifics of the graph later.
4 - Detail panel:This shows the main information about the particular
instrument you’re using. In this case, it shows the ones using the most
CPU time.
5 - Inspectors panel:There are two inspectors — Extended Detail
and Run Info
8. wednesday.is
it spends a lot of time invoking the method that creates the CGImage output after applying
the image filter.
Problem
9. wednesday.is
To Solve this we can do two things
1- First, offload the image filtering onto a background thread
with DispatchQueue.global().async
2 - cache each image after it’s generated.
10. wednesday.is
1- Check to see if a filtered image for this photo exists
in the image cache. If yes, display that image.
2 - If not, dispatch the call to create an image with the
tonal filter onto a background queue.This allows the
UI to remain responsive while the filter runs. When
the filter completes, save the image in the cache and
update the image view on the main queue.
11. wednesday.is
We can do the same for
original Flickr thumbnails.
If an image already exists in
the cache, call the completion
closure straight away with the
cached image. Otherwise,
load the image from Flickr and
store it in the cache.
13. wednesday.is
Unbounded memory growth
This occurs when memory is allocated continuously and never given a chance to be deallocated.
If this continues unchecked, you’ll run out of memory. On iOS, this means the system will
terminate your app.
15. wednesday.is
To start fresh with a new instruments profile, quit the
Instruments app
Press Command-I in Xcode, select Allocations from
the list and press Choose.
WithAllocations running, perform five different searches
in the app.
Make sure you have some results and let the app settle
a bit by waiting for a few minutes
16. wednesday.is
- This adds an image to a dictionary, keyed on the
photo ID of the Flickr photo.
- But you’ll notice the image is never cleared from
that dictionary!
- That’s where your unbounded memory growth is
coming from. Everything is working as it should,
but the app never removes things from the cache
it only ever adds them!
Example of Unbounded memory growth
17. wednesday.is
- To fix the problem, have ImageCache listen for the
memory warning notification UIApplication fires.
- When ImageCache receives this, it should clear its
cache.This registers an observer for
UIApplication.didReceiveMemoryWarningNotification to
execute a closure that clears images.
- All the code needs to do is remove all objects in the
cache.This ensures nothing is holding onto the images
anymore and they’ll be deallocated.
Solution
18. wednesday.is
Bonus tip!
Always profile your app on an actual device instead of the simulator.The iOS
simulator has all the horsepower of your Mac behind it, whereas a device has all the
limitations of mobile hardware.That said, your app may seem to run fine in the
simulator, but you might discover a performance issue once it’s running on a real
device.
19. 19
Key Takeaways
What we learned
What is instruments? Why is it needed!
How to check CPU usage usingTime
profiler
Types of memory leaks and how to detect
them usingAllocation
Some optimizations to improve
app performance