SlideShare a Scribd company logo
1 of 46
Optimizing Apps for Better
Performance
Elif Boncuk
Software Specialist @ Garanti Teknoloji
#dfist
Who am I?
➢Hacettepe - Computer Engineering (2006 - 2011)
➢Garanti Technology - Mobile Applications Team (2011- )
➢MBA Student @BAU (2014 - )
➢Blogger (2010 - )
➢Photographer
@elifbon_
+ElifBoncuk
www.elifboncuk.wordpress.com
#dfist
Be Faster!
➢Gather Information
➢Gain Insight
➢Take Action
#dfist
4 Major Steps
➢Rendering
➢Compute
➢Memory
➢Battery
#dfist
Rendering
Udacity: Android Performance
#dfist
Rendering...
Udacity: Android Performance
#dfist
Rendering...
MEASURE
EXECUTE
RECORD
LAYOUT
CPU
RASTERIZATION GPU
F
L
O
W
PROBLEM
OVERDRAW
LAYOUTS &
INVALIDATIONS
PROBLEM
#dfist
Rasterization
Udacity: Android Performance
#dfist
Overdraw
➢Overdraw is a term used to describe how many times a pixel on the screen has been redrawn in
a single frame.
Udacity: Android Performance
#dfist
Overdraw...
1. On your mobile device, go to Settings and tapDeveloper Options.
2. In the Hardware accelerated rendering section, select Debug GPU
Overdraw.
3. In the Debug GPU overdraw popup, select Show overdraw areas.
4. Don't panic as your screen turns into a delirium of colors. The coloring
is provided to help you diagnose your app's display behavior.
http://developer.android.com/
#dfist
Overdraw…
5. The colors are hinting at the amount of overdraw on your screen
for each pixel, as follows:
True color: No overdraw
Blue: Overdrawn once
Green: Overdrawn twice
Pink: Overdrawn three times
Red: Overdrawn four or more times
http://developer.android.com/
#dfist
Overdraw…
How?
➢Eliminate unneeded backgrounds and
drawables
➢Define areas will hide portions of view
Best Practices:
➢getWindow().setBackgroundDrawable(null
➢android:background:”@null”
http://developer.android.com/
#dfist
Clipping
➢Clipping is an optimisation which can be defined as Android
framework knows overdraw is a problem and will go out of its
way to avoid drawing UI widgets that may be invisible in the final
image.
➢Canvas.quickReject()
➢Canvas.clipRect()
DRAWABLE
ZONE
Canvas.clipRect()
20dp
20dp 20dp
20dp
Udacity: Android Performance
#dfist
CPU Optimizations
Udacity: Android Performance
#dfist
Hierarchy Viewer
http://developer.android.com/
#dfist
Hierarchy Viewer
Bird's-eye view
Tree Overview
Tree View
View Properties
Nodes
http://developer.android.com/
#dfist
Hierarchy Viewer
Each view in your subtree gets three dots, which
can be green, yellow, or red.
The left dot represents the Draw Process of the
rendering pipeline.
The middle dot represents the Layout Phase.
The right dot represents the Execute Phase.
http://developer.android.com/
#dfist
Hierarchy Viewer
The color of the dots indicates the relative performance of this node
in respect to all other profiled nodes.
Green means the view renders faster than at least half of the
other views.
Yellow means the view renders faster than the bottom half of the
other views.
Red means the view is among the slowest half of views.
http://developer.android.com/
#dfist
Compute
➢Slow Function Performance
Udacity: Android Performance
#dfist
Profiling with Traceview
➢Traceview is a graphical viewer for execution logs that you create by using the Debug class to log
tracing information in your code. Traceview can help you debug your application and profile its
performance.
#dfist
Profiling with Traceview
http://developer.android.com/
#dfist
Batching and Caching
TARGET ARRAY TARGET VALUES
Look at all
that
overhead!
Udacity: Android Performance
#dfist
Blocking the UI Thread
Udacity: Android Performance
#dfist
How to Solve?
Android's single thread model:
1. Do not block the UI thread
2. Do not access the Android UI toolkit
from outside the UI thread
Udacity: Android Performance
#dfist
How to Solve?
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
final Bitmap bitmap =
loadImageFromNetwork("http://example.com/image.png")
;
mImageView.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
}
}).start();
}
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
Bitmap b =
loadImageFromNetwork("http://example.com/image.p
ng");
mImageView.setImageBitmap(b);
}
}).start();
}
#dfist
How to Solve?
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
#dfist
Analyzing UI Performance with Systrace
➢The Systrace tool allows you to collect and inspect timing information across an entire Android
device, which is called a trace.
Trace.beginSection("Data Structures");
// TODO:
Trace.endSection();
#dfist
Analyzing UI Performance with Systrace
Bad Performance Example
http://developer.android.com/
#dfist
Analyzing UI Performance with Systrace
http://developer.android.com/
#dfist
Memory
Basic Principles of Garbage Collection:
➢Find data objects in a program that cannot be accesed in the future.
➢Reclaim the resources used by those objects.
Udacity: Android Performance
#dfist
Memory Monitor
Memory Monitor reports in real-time how your app allocates memory.
➢ Showing available and used memory in a graph, and garbage collection events over time.
➢ Quickly testing whether app slowness might be related to excessive garbage collection events.
➢ Quickly testing whether app crashes may be related to running out of memory.
Dark blue: Amount of memory that your app is currently using.
Light blue: Available, unallocated memory.
#dfist
Memory Monitor
#dfist
Heap Viewer
Heap Viewer reports in real-time what types of objects your application has allocated, how many, and their sizes
on the heap.
➢ Getting a sense of how your app allocates and frees memory.
➢ Identifying memory leaks.
➢ 5.0+
http://developer.android.com/
#dfist
Heap Viewer
http://developer.android.com/
#dfist
Allocation Tracker
Allocation Tracker records an app's memory allocations and lists all allocated objects for the profiling cycle with their call
stack, size, and allocating code.
➢Identifying where many similar object types, from roughly the same call stack, are allocated and deallocated over a very
short period of time.
➢Finding the places in your code that may contribute to inefficient memory use.
http://developer.android.com/
#dfist
Battery
Udacity: Android Performance
#dfist
Batterystats & Battery Historian
Batterystats collects battery data from your device, and Battery Historian converts that data into an HTML
visualization that you can view in your Browser.
➢Showing you where and how processes are drawing current from the battery.
➢Identifying tasks in your app that could be deferred or even removed to improve battery life.
➢5.0+
#dfist
Batterystats & Battery Historian
http://developer.android.com/
#dfist
Batterystats & Battery Historian
battery_level: When the battery level was recorded and logged.
top: The application running at the top.
wifi_running: Shows that the Wi-Fi network connection was active.
screen: Screen is turned on.
phone_in_call: Recorded when the phone is in a call.
wake_lock: App wakes up, grabs a lock, does small work, then goes back to sleep. running: Shows when the CPU is awake. Check whether it is
awake and asleep when you expect it to be.
wake_reason: The last thing that caused the kernel to wake up. If it's your app, determine whether it was necessary.
mobile_radio: Shows when the radio was on. Starting the radio is battery expensive. Many narrow bars close to each other can indicate opportunities
for batching and other optimizations.
gps: Indicates when the GPS was on. Make sure this is what you expect.
sync: Shows when an app was syncing with a backend.
#dfist
Batterystats & Battery Historian
Battery History: A time series of power-relevant events, such as screen, Wi-Fi, and app launch.
These are also visible through Battery Historian.
Per-PID Stats: How long each process ran.
Statistics since last charge: System-wide statistics, such as cell signal levels and screen
brightness. Provides an overall picture of what's happening with the device. This information is
especially useful to make sure no external events are affecting your experiment.
Estimated power use (mAh) by UID and peripheral: This is currently an extremely rough estimate
and should not be considered experiment data.
Per-app mobile ms per packet: Radio-awake-time divided by packets sent. An efficient app will
transfer all its traffic in batches, so the lower this number the better.
All partial wake locks: All app-held wakelocks, by aggregate duration and count.
#dfist
Batterystats & Battery Historian
Udacity: Android Performance
#dfist
JobScheduler
Google IO 2014: Project Volta
#dfist
JobScheduler
private void pollServer() {
mWakeLockMsg.setText("Polling the server! This day sure went by fast.");
for (int i=0; i<10; i++) {
mWakeLock.acquire();
mWakeLockMsg.append("Connection attempt, take " + i + ":n");
mWakeLockMsg.append(getString(R.string.wakelock_acquired));
// Always check that the network is available before trying to connect. You don't want
// to break things and embarrass yourself.
if (isNetworkConnected()) {
new SimpleDownloadTask().execute();
} else {
mWakeLockMsg.append("No connection on job " + i + "; SAD FACE");
}
}
}
@Override
protected void onPostExecute(String result) {
mWakeLockMsg.append("n" + result + "n");
releaseWakeLock();
}
#dfist
JobScheduler
mServiceComponent = new ComponentName(this,
MyJobService.class);
@Override
public boolean onStartJob(JobParameters params) {
Log.i(LOG_TAG, "Totally and completely working
on job " + params.getJobId());
// First, check the network, and then attempt to
connect.
if (isNetworkConnected()) {
new SimpleDownloadTask() .execute(params);
return true;
} else {
Log.i(LOG_TAG, "No connection on job " +
params.getJobId() + "; sad face");
}
return false;
}
public void pollServer() {
JobScheduler scheduler = (JobScheduler)
getSystemService(Context.JOB_SCHEDULER_SERVICE);
for (int i=0; i<10; i++) {
JobInfo jobInfo = new JobInfo.Builder(i,
mServiceComponent)
.setMinimumLatency(5000) // 5
seconds
.setOverrideDeadline(60000) // 60
seconds (for brevity in the sample)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
// WiFi or data connections
.build();
mWakeLockMsg.append("Scheduling job " + i +
"!n");
scheduler.schedule(jobInfo);
}
}
#dfist
Hunter S. Thompson
“Life should not be a journey to the
grave with the intention of arriving
safely in a pretty and well preserved
body, but rather to skid in broadside in
a cloud of smoke, throughly used up,
totally worn out, and loudly
proclaiming “Wow! What a ride!””
#dfist
Questions?
@elifbon_
+ElifBoncuk
www.elifboncuk.wordpress.com
Thank you!

More Related Content

What's hot

Visual Testing: The Missing Piece of the Puzzle -- presentation by Gil Tayar
Visual Testing: The Missing Piece of the Puzzle -- presentation by Gil TayarVisual Testing: The Missing Piece of the Puzzle -- presentation by Gil Tayar
Visual Testing: The Missing Piece of the Puzzle -- presentation by Gil Tayar
Applitools
 
DEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android ApplicationsDEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android Applications
Kyungmin Lee
 

What's hot (20)

Automated Historical Performance Analysis with kmemtracer
Automated Historical Performance Analysis with kmemtracerAutomated Historical Performance Analysis with kmemtracer
Automated Historical Performance Analysis with kmemtracer
 
Google Glasses Integration with SAP
Google Glasses Integration with SAPGoogle Glasses Integration with SAP
Google Glasses Integration with SAP
 
Google Glass, the GDK, and HTML5
Google Glass, the GDK, and HTML5Google Glass, the GDK, and HTML5
Google Glass, the GDK, and HTML5
 
Design Patterns every Android developer should know
Design Patterns every Android developer should knowDesign Patterns every Android developer should know
Design Patterns every Android developer should know
 
Lecture #1 Creating your first android project
Lecture #1  Creating your first android projectLecture #1  Creating your first android project
Lecture #1 Creating your first android project
 
Visual Testing: The Missing Piece of the Puzzle -- presentation by Gil Tayar
Visual Testing: The Missing Piece of the Puzzle -- presentation by Gil TayarVisual Testing: The Missing Piece of the Puzzle -- presentation by Gil Tayar
Visual Testing: The Missing Piece of the Puzzle -- presentation by Gil Tayar
 
DEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android ApplicationsDEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android Applications
 
Android Programming made easy
Android Programming made easyAndroid Programming made easy
Android Programming made easy
 
Developer Student Clubs NUK - Flutter for Beginners
Developer Student Clubs NUK - Flutter for BeginnersDeveloper Student Clubs NUK - Flutter for Beginners
Developer Student Clubs NUK - Flutter for Beginners
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
 
What's new in android 4.4 - Romain Guy & Chet Haase
What's new in android 4.4 - Romain Guy & Chet HaaseWhat's new in android 4.4 - Romain Guy & Chet Haase
What's new in android 4.4 - Romain Guy & Chet Haase
 
Recap of Android Dev Summit 2018
Recap of Android Dev Summit 2018Recap of Android Dev Summit 2018
Recap of Android Dev Summit 2018
 
Introduction to html5 game programming with ImpactJs
Introduction to html5 game programming with ImpactJsIntroduction to html5 game programming with ImpactJs
Introduction to html5 game programming with ImpactJs
 
Android Fundamentals
Android FundamentalsAndroid Fundamentals
Android Fundamentals
 
Android study jams 2021 [collab] [master]
Android study jams 2021 [collab] [master]Android study jams 2021 [collab] [master]
Android study jams 2021 [collab] [master]
 
Android tools
Android toolsAndroid tools
Android tools
 
Visual Component Testing -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
Visual Component Testing  -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...Visual Component Testing  -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
Visual Component Testing -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
 
Devraj_Nataraj_CV_PDF
Devraj_Nataraj_CV_PDFDevraj_Nataraj_CV_PDF
Devraj_Nataraj_CV_PDF
 
Android studio
Android studioAndroid studio
Android studio
 
Introduction to android studio 2.0 and data binding library
Introduction to android studio 2.0 and data binding libraryIntroduction to android studio 2.0 and data binding library
Introduction to android studio 2.0 and data binding library
 

Viewers also liked

Viewers also liked (20)

App indexing api
App indexing apiApp indexing api
App indexing api
 
Workshop on Search Engine Optimization
Workshop on Search Engine OptimizationWorkshop on Search Engine Optimization
Workshop on Search Engine Optimization
 
Project Analysis - How to Start Project Develoment
Project Analysis - How to Start Project DevelomentProject Analysis - How to Start Project Develoment
Project Analysis - How to Start Project Develoment
 
Workhsop on Logic Building for Programming
Workhsop on Logic Building for ProgrammingWorkhsop on Logic Building for Programming
Workhsop on Logic Building for Programming
 
Lecture 04. Mobile App Design
Lecture 04. Mobile App DesignLecture 04. Mobile App Design
Lecture 04. Mobile App Design
 
Android development session 3 - layout
Android development   session 3 - layoutAndroid development   session 3 - layout
Android development session 3 - layout
 
Android Udacity Study group 1
Android Udacity Study group 1Android Udacity Study group 1
Android Udacity Study group 1
 
Android development session 4 - Fragments
Android development   session 4 - FragmentsAndroid development   session 4 - Fragments
Android development session 4 - Fragments
 
Fundamental of android
Fundamental of androidFundamental of android
Fundamental of android
 
Working better together designers &amp; developers
Working better together   designers &amp; developersWorking better together   designers &amp; developers
Working better together designers &amp; developers
 
Session #8 adding magic to your app
Session #8  adding magic to your appSession #8  adding magic to your app
Session #8 adding magic to your app
 
Management Innovation
Management Innovation Management Innovation
Management Innovation
 
Workshop Android for Java Developers
Workshop Android for Java DevelopersWorkshop Android for Java Developers
Workshop Android for Java Developers
 
Lecture 06. iOS Programming. Основи Objective-C
Lecture 06. iOS Programming. Основи Objective-CLecture 06. iOS Programming. Основи Objective-C
Lecture 06. iOS Programming. Основи Objective-C
 
Android Development Workshop
Android Development WorkshopAndroid Development Workshop
Android Development Workshop
 
Workshop on How to crack interview
Workshop on How to crack interviewWorkshop on How to crack interview
Workshop on How to crack interview
 
Lecture 08 Xamarin
Lecture 08 XamarinLecture 08 Xamarin
Lecture 08 Xamarin
 
Android design lecture #1
Android design   lecture #1Android design   lecture #1
Android design lecture #1
 
Android development session 6 - Google Maps v2
Android development   session 6 - Google Maps v2Android development   session 6 - Google Maps v2
Android development session 6 - Google Maps v2
 
Workshop on android ui
Workshop on android uiWorkshop on android ui
Workshop on android ui
 

Similar to Optimizing Apps for Better Performance

Manish Chasta - Securing Android Applications
Manish Chasta - Securing Android ApplicationsManish Chasta - Securing Android Applications
Manish Chasta - Securing Android Applications
Positive Hack Days
 
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NCAndroid Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Jim Tochterman
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the Trenches
Anuradha Weeraman
 

Similar to Optimizing Apps for Better Performance (20)

Manish Chasta - Securing Android Applications
Manish Chasta - Securing Android ApplicationsManish Chasta - Securing Android Applications
Manish Chasta - Securing Android Applications
 
Mobile optimization
Mobile optimizationMobile optimization
Mobile optimization
 
GDG Devfest 2016 session on Android N
GDG Devfest 2016 session on Android NGDG Devfest 2016 session on Android N
GDG Devfest 2016 session on Android N
 
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NCAndroid Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
 
Getting Started with Android - OSSPAC 2009
Getting Started with Android - OSSPAC 2009Getting Started with Android - OSSPAC 2009
Getting Started with Android - OSSPAC 2009
 
MobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android AppsMobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android Apps
 
Android 3.0 Portland Java User Group 2011-03-15
Android 3.0 Portland Java User Group 2011-03-15Android 3.0 Portland Java User Group 2011-03-15
Android 3.0 Portland Java User Group 2011-03-15
 
Mobile is slow - Over the Air 2013
Mobile is slow - Over the Air 2013Mobile is slow - Over the Air 2013
Mobile is slow - Over the Air 2013
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the Trenches
 
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
 
An end-to-end experience of Windows Phone 7 development (Part 1)
An end-to-end experience of Windows Phone 7 development (Part 1)An end-to-end experience of Windows Phone 7 development (Part 1)
An end-to-end experience of Windows Phone 7 development (Part 1)
 
Introduction to Android - Mobile Portland
Introduction to Android - Mobile PortlandIntroduction to Android - Mobile Portland
Introduction to Android - Mobile Portland
 
Getting started with android dev and test perspective
Getting started with android   dev and test perspectiveGetting started with android   dev and test perspective
Getting started with android dev and test perspective
 
Android 3.1 - Portland Code Camp 2011
Android 3.1 - Portland Code Camp 2011Android 3.1 - Portland Code Camp 2011
Android 3.1 - Portland Code Camp 2011
 
移动端Web app开发
移动端Web app开发移动端Web app开发
移动端Web app开发
 
An Introduction To Android
An Introduction To AndroidAn Introduction To Android
An Introduction To Android
 
Intro to Android Programming
Intro to Android ProgrammingIntro to Android Programming
Intro to Android Programming
 
[Wroclaw #1] Android Security Workshop
[Wroclaw #1] Android Security Workshop[Wroclaw #1] Android Security Workshop
[Wroclaw #1] Android Security Workshop
 
Android 101 Session @thejunction32
Android 101 Session @thejunction32Android 101 Session @thejunction32
Android 101 Session @thejunction32
 
Android N Highligts
Android N HighligtsAndroid N Highligts
Android N Highligts
 

Recently uploaded

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (8)

9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 
Thane 💋 Call Girls 7738631006 💋 Call Girls in Thane Escort service book now. ...
Thane 💋 Call Girls 7738631006 💋 Call Girls in Thane Escort service book now. ...Thane 💋 Call Girls 7738631006 💋 Call Girls in Thane Escort service book now. ...
Thane 💋 Call Girls 7738631006 💋 Call Girls in Thane Escort service book now. ...
 
Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 

Optimizing Apps for Better Performance

  • 1. Optimizing Apps for Better Performance Elif Boncuk Software Specialist @ Garanti Teknoloji
  • 2. #dfist Who am I? ➢Hacettepe - Computer Engineering (2006 - 2011) ➢Garanti Technology - Mobile Applications Team (2011- ) ➢MBA Student @BAU (2014 - ) ➢Blogger (2010 - ) ➢Photographer @elifbon_ +ElifBoncuk www.elifboncuk.wordpress.com
  • 9. #dfist Overdraw ➢Overdraw is a term used to describe how many times a pixel on the screen has been redrawn in a single frame. Udacity: Android Performance
  • 10. #dfist Overdraw... 1. On your mobile device, go to Settings and tapDeveloper Options. 2. In the Hardware accelerated rendering section, select Debug GPU Overdraw. 3. In the Debug GPU overdraw popup, select Show overdraw areas. 4. Don't panic as your screen turns into a delirium of colors. The coloring is provided to help you diagnose your app's display behavior. http://developer.android.com/
  • 11. #dfist Overdraw… 5. The colors are hinting at the amount of overdraw on your screen for each pixel, as follows: True color: No overdraw Blue: Overdrawn once Green: Overdrawn twice Pink: Overdrawn three times Red: Overdrawn four or more times http://developer.android.com/
  • 12. #dfist Overdraw… How? ➢Eliminate unneeded backgrounds and drawables ➢Define areas will hide portions of view Best Practices: ➢getWindow().setBackgroundDrawable(null ➢android:background:”@null” http://developer.android.com/
  • 13. #dfist Clipping ➢Clipping is an optimisation which can be defined as Android framework knows overdraw is a problem and will go out of its way to avoid drawing UI widgets that may be invisible in the final image. ➢Canvas.quickReject() ➢Canvas.clipRect() DRAWABLE ZONE Canvas.clipRect() 20dp 20dp 20dp 20dp Udacity: Android Performance
  • 16. #dfist Hierarchy Viewer Bird's-eye view Tree Overview Tree View View Properties Nodes http://developer.android.com/
  • 17. #dfist Hierarchy Viewer Each view in your subtree gets three dots, which can be green, yellow, or red. The left dot represents the Draw Process of the rendering pipeline. The middle dot represents the Layout Phase. The right dot represents the Execute Phase. http://developer.android.com/
  • 18. #dfist Hierarchy Viewer The color of the dots indicates the relative performance of this node in respect to all other profiled nodes. Green means the view renders faster than at least half of the other views. Yellow means the view renders faster than the bottom half of the other views. Red means the view is among the slowest half of views. http://developer.android.com/
  • 20. #dfist Profiling with Traceview ➢Traceview is a graphical viewer for execution logs that you create by using the Debug class to log tracing information in your code. Traceview can help you debug your application and profile its performance.
  • 22. #dfist Batching and Caching TARGET ARRAY TARGET VALUES Look at all that overhead! Udacity: Android Performance
  • 23. #dfist Blocking the UI Thread Udacity: Android Performance
  • 24. #dfist How to Solve? Android's single thread model: 1. Do not block the UI thread 2. Do not access the Android UI toolkit from outside the UI thread Udacity: Android Performance
  • 25. #dfist How to Solve? public void onClick(View v) { new Thread(new Runnable() { public void run() { final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png") ; mImageView.post(new Runnable() { public void run() { mImageView.setImageBitmap(bitmap); } }); } }).start(); } public void onClick(View v) { new Thread(new Runnable() { public void run() { Bitmap b = loadImageFromNetwork("http://example.com/image.p ng"); mImageView.setImageBitmap(b); } }).start(); }
  • 26. #dfist How to Solve? public void onClick(View v) { new DownloadImageTask().execute("http://example.com/image.png"); } private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protected void onPostExecute(Bitmap result) { mImageView.setImageBitmap(result); } }
  • 27. #dfist Analyzing UI Performance with Systrace ➢The Systrace tool allows you to collect and inspect timing information across an entire Android device, which is called a trace. Trace.beginSection("Data Structures"); // TODO: Trace.endSection();
  • 28. #dfist Analyzing UI Performance with Systrace Bad Performance Example http://developer.android.com/
  • 29. #dfist Analyzing UI Performance with Systrace http://developer.android.com/
  • 30. #dfist Memory Basic Principles of Garbage Collection: ➢Find data objects in a program that cannot be accesed in the future. ➢Reclaim the resources used by those objects. Udacity: Android Performance
  • 31. #dfist Memory Monitor Memory Monitor reports in real-time how your app allocates memory. ➢ Showing available and used memory in a graph, and garbage collection events over time. ➢ Quickly testing whether app slowness might be related to excessive garbage collection events. ➢ Quickly testing whether app crashes may be related to running out of memory. Dark blue: Amount of memory that your app is currently using. Light blue: Available, unallocated memory.
  • 33. #dfist Heap Viewer Heap Viewer reports in real-time what types of objects your application has allocated, how many, and their sizes on the heap. ➢ Getting a sense of how your app allocates and frees memory. ➢ Identifying memory leaks. ➢ 5.0+ http://developer.android.com/
  • 35. #dfist Allocation Tracker Allocation Tracker records an app's memory allocations and lists all allocated objects for the profiling cycle with their call stack, size, and allocating code. ➢Identifying where many similar object types, from roughly the same call stack, are allocated and deallocated over a very short period of time. ➢Finding the places in your code that may contribute to inefficient memory use. http://developer.android.com/
  • 37. #dfist Batterystats & Battery Historian Batterystats collects battery data from your device, and Battery Historian converts that data into an HTML visualization that you can view in your Browser. ➢Showing you where and how processes are drawing current from the battery. ➢Identifying tasks in your app that could be deferred or even removed to improve battery life. ➢5.0+
  • 38. #dfist Batterystats & Battery Historian http://developer.android.com/
  • 39. #dfist Batterystats & Battery Historian battery_level: When the battery level was recorded and logged. top: The application running at the top. wifi_running: Shows that the Wi-Fi network connection was active. screen: Screen is turned on. phone_in_call: Recorded when the phone is in a call. wake_lock: App wakes up, grabs a lock, does small work, then goes back to sleep. running: Shows when the CPU is awake. Check whether it is awake and asleep when you expect it to be. wake_reason: The last thing that caused the kernel to wake up. If it's your app, determine whether it was necessary. mobile_radio: Shows when the radio was on. Starting the radio is battery expensive. Many narrow bars close to each other can indicate opportunities for batching and other optimizations. gps: Indicates when the GPS was on. Make sure this is what you expect. sync: Shows when an app was syncing with a backend.
  • 40. #dfist Batterystats & Battery Historian Battery History: A time series of power-relevant events, such as screen, Wi-Fi, and app launch. These are also visible through Battery Historian. Per-PID Stats: How long each process ran. Statistics since last charge: System-wide statistics, such as cell signal levels and screen brightness. Provides an overall picture of what's happening with the device. This information is especially useful to make sure no external events are affecting your experiment. Estimated power use (mAh) by UID and peripheral: This is currently an extremely rough estimate and should not be considered experiment data. Per-app mobile ms per packet: Radio-awake-time divided by packets sent. An efficient app will transfer all its traffic in batches, so the lower this number the better. All partial wake locks: All app-held wakelocks, by aggregate duration and count.
  • 41. #dfist Batterystats & Battery Historian Udacity: Android Performance
  • 43. #dfist JobScheduler private void pollServer() { mWakeLockMsg.setText("Polling the server! This day sure went by fast."); for (int i=0; i<10; i++) { mWakeLock.acquire(); mWakeLockMsg.append("Connection attempt, take " + i + ":n"); mWakeLockMsg.append(getString(R.string.wakelock_acquired)); // Always check that the network is available before trying to connect. You don't want // to break things and embarrass yourself. if (isNetworkConnected()) { new SimpleDownloadTask().execute(); } else { mWakeLockMsg.append("No connection on job " + i + "; SAD FACE"); } } } @Override protected void onPostExecute(String result) { mWakeLockMsg.append("n" + result + "n"); releaseWakeLock(); }
  • 44. #dfist JobScheduler mServiceComponent = new ComponentName(this, MyJobService.class); @Override public boolean onStartJob(JobParameters params) { Log.i(LOG_TAG, "Totally and completely working on job " + params.getJobId()); // First, check the network, and then attempt to connect. if (isNetworkConnected()) { new SimpleDownloadTask() .execute(params); return true; } else { Log.i(LOG_TAG, "No connection on job " + params.getJobId() + "; sad face"); } return false; } public void pollServer() { JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); for (int i=0; i<10; i++) { JobInfo jobInfo = new JobInfo.Builder(i, mServiceComponent) .setMinimumLatency(5000) // 5 seconds .setOverrideDeadline(60000) // 60 seconds (for brevity in the sample) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) // WiFi or data connections .build(); mWakeLockMsg.append("Scheduling job " + i + "!n"); scheduler.schedule(jobInfo); } }
  • 45. #dfist Hunter S. Thompson “Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, throughly used up, totally worn out, and loudly proclaiming “Wow! What a ride!””