SlideShare a Scribd company logo
1 of 20
Download to read offline
Instruments.
wednesday.is
wednesday.is
1. Make it work
2. Make it right
3. Make it fast
- Brian W. Kernighan
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
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
wednesday.is
Letโ€™s get started with a
sample app
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
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
wednesday.is
it spends a lot of time invoking the method that creates the CGImage output after applying
the image filter.
Problem
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.
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.
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.
wednesday.is
Allocations
You have likely heard about memory leaks!
One of the type is Unbounded memory
growth
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.
wednesday.is
So, How can we prevent
memory leak using
instruments?
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
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
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
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
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
Thank you
very much!!!
wednesday.is

More Related Content

Similar to Master iOS Performance Optimization with Instruments

La1 information and communication technology and society
La1   information and communication technology and societyLa1   information and communication technology and society
La1 information and communication technology and society
Azmiah Mahmud
ย 
Learning area 1_-_information_and_communication_technology_and_society
Learning area 1_-_information_and_communication_technology_and_societyLearning area 1_-_information_and_communication_technology_and_society
Learning area 1_-_information_and_communication_technology_and_society
Saktis Kesavan
ย 
Learning area 1 information and communication technology and society
Learning area 1   information and communication technology and societyLearning area 1   information and communication technology and society
Learning area 1 information and communication technology and society
Shuren Lew
ย 

Similar to Master iOS Performance Optimization with Instruments (20)

La1 information and communication technology and society
La1   information and communication technology and societyLa1   information and communication technology and society
La1 information and communication technology and society
ย 
Learning area 1_-_information_and_communication_technology_and_society
Learning area 1_-_information_and_communication_technology_and_societyLearning area 1_-_information_and_communication_technology_and_society
Learning area 1_-_information_and_communication_technology_and_society
ย 
Learning area 1 information and communication technology and society
Learning area 1   information and communication technology and societyLearning area 1   information and communication technology and society
Learning area 1 information and communication technology and society
ย 
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
ย 
iOS Scroll Performance
iOS Scroll PerformanceiOS Scroll Performance
iOS Scroll Performance
ย 
React Native Android. It's easy.
React Native Android. It's easy.React Native Android. It's easy.
React Native Android. It's easy.
ย 
Best Practice iPhone SDK App Design
Best Practice iPhone SDK App DesignBest Practice iPhone SDK App Design
Best Practice iPhone SDK App Design
ย 
Pixlr and small apps in the classroom
Pixlr and small apps in the classroomPixlr and small apps in the classroom
Pixlr and small apps in the classroom
ย 
Wids datathon slides_vanim (updated)
Wids datathon slides_vanim (updated)Wids datathon slides_vanim (updated)
Wids datathon slides_vanim (updated)
ย 
PVS-Studio Now Supports Any Build System under Windows and Any Compiler. Easy...
PVS-Studio Now Supports Any Build System under Windows and Any Compiler. Easy...PVS-Studio Now Supports Any Build System under Windows and Any Compiler. Easy...
PVS-Studio Now Supports Any Build System under Windows and Any Compiler. Easy...
ย 
TechMentor Fall, 2011 - How to Resolve (Nearly) Every Windows 7 Application I...
TechMentor Fall, 2011 - How to Resolve (Nearly) Every Windows 7 Application I...TechMentor Fall, 2011 - How to Resolve (Nearly) Every Windows 7 Application I...
TechMentor Fall, 2011 - How to Resolve (Nearly) Every Windows 7 Application I...
ย 
Practical guide to optimization in Unity
Practical guide to optimization in UnityPractical guide to optimization in Unity
Practical guide to optimization in Unity
ย 
Smalltalk in the pocket - Building applications for the iPhone
Smalltalk in the pocket - Building applications for the iPhoneSmalltalk in the pocket - Building applications for the iPhone
Smalltalk in the pocket - Building applications for the iPhone
ย 
3 - Thermometer.pptx thermometer thermometer thermometer
3 - Thermometer.pptx thermometer thermometer thermometer3 - Thermometer.pptx thermometer thermometer thermometer
3 - Thermometer.pptx thermometer thermometer thermometer
ย 
Android programming-basics
Android programming-basicsAndroid programming-basics
Android programming-basics
ย 
So. many. vulnerabilities. Why are containers such a mess and what to do abou...
So. many. vulnerabilities. Why are containers such a mess and what to do abou...So. many. vulnerabilities. Why are containers such a mess and what to do abou...
So. many. vulnerabilities. Why are containers such a mess and what to do abou...
ย 
Performence #2 gpu
Performence #2  gpuPerformence #2  gpu
Performence #2 gpu
ย 
0-Day Up Your Sleeve - Attacking macOS Environments
0-Day Up Your Sleeve - Attacking macOS Environments0-Day Up Your Sleeve - Attacking macOS Environments
0-Day Up Your Sleeve - Attacking macOS Environments
ย 
Agility Requires Safety
Agility Requires SafetyAgility Requires Safety
Agility Requires Safety
ย 
Ml goes fruitful
Ml goes fruitfulMl goes fruitful
Ml goes fruitful
ย 

More from Wednesday Solutions

Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
Wednesday Solutions
ย 

More from Wednesday Solutions (8)

Error Handling in Express
Error Handling in ExpressError Handling in Express
Error Handling in Express
ย 
What is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A WalkthroughWhat is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A Walkthrough
ย 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
ย 
Create Basic 3D Scenes Using Three.js
Create Basic 3D Scenes Using Three.jsCreate Basic 3D Scenes Using Three.js
Create Basic 3D Scenes Using Three.js
ย 
A Guide to Figma for Developers
A Guide to Figma for DevelopersA Guide to Figma for Developers
A Guide to Figma for Developers
ย 
What is Temporal: Workflow & Cluster
What is Temporal: Workflow & ClusterWhat is Temporal: Workflow & Cluster
What is Temporal: Workflow & Cluster
ย 
Vector Embedding using AI
Vector Embedding using AIVector Embedding using AI
Vector Embedding using AI
ย 
What is GraphQL: Best Practices
What is GraphQL: Best PracticesWhat is GraphQL: Best Practices
What is GraphQL: Best Practices
ย 

Recently uploaded

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
ย 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
ย 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
ย 

Recently uploaded (20)

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
ย 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
ย 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
ย 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
ย 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
ย 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
ย 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
ย 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
ย 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ย 

Master iOS Performance Optimization with Instruments

  • 2. wednesday.is 1. Make it work 2. Make it right 3. Make it fast - Brian W. Kernighan
  • 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.
  • 12. wednesday.is Allocations You have likely heard about memory leaks! One of the type is Unbounded memory growth
  • 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.
  • 14. wednesday.is So, How can we prevent memory leak using instruments?
  • 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