High-performance Android apps. Tim Bray



Similar documents
How to develop your own app

getsharedpreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

A Look through the Android Stack

Introduction to Android. CSG250 Wireless Networks Fall, 2008

Lecture 1 Introduction to Android

Android Geek Night. Application framework

Introduction to the BackgroundWorker Component in WPF

Android Fundamentals 1

Performance Testing. Based on slides created by Marty Stepp

RESCO MOBILE CRM QUICK GUIDE. for MS Dynamics CRM. ios (ipad & iphone) Android phones & tablets

Introduction to Android Programming (CS5248 Fall 2015)

ODROID Multithreading in Android

A Case Study of an Android* Client App Using Cloud-Based Alert Service

Cloud Storage Service

Optimizing Background Sync on Smartphones

Android app development course

MA-WA1920: Enterprise iphone and ipad Programming

Getting started with Android and App Engine

Android on Intel Course App Development - Advanced

Connecting Software Connect Bridge - Mobile CRM Android User Manual

Measuring Firebird Disc I/O

QUIRE: : Lightweight Provenance for Smart Phone Operating Systems

Programming with Android

Mobile Security - Tutorial 1. Beginning Advanced Android Development Brian Ricks Fall 2014

ANDROID PROGRAMMING - INTRODUCTION. Roberto Beraldi

Getting Started on the PC and MAC

Job Reference Guide. SLAMD Distributed Load Generation Engine. Version 1.8.2

slides at goo.gl/kifue

Step One Check for Internet Connection

Enterprise Java (BI-EJA) Technologie programování v jazyku Java (X36TJV)

Mini Project - Phase 3 Connexus Mobile App (Android)

Luxriot Broadcast Server Manual

Mobility Introduction Android. Duration 16 Working days Start Date 1 st Oct 2013

CSE 403. Performance Profiling Marty Stepp

Using your LED Plus keypad

ITG Software Engineering

How to Setup and Manage LAUSD in Outlook

AdFalcon Android SDK Developer's Guide. AdFalcon Mobile Ad Network Product of Noqoush Mobile Media Group

Operating Systems Concepts: Chapter 7: Scheduling Strategies

VMware vcenter Log Insight Getting Started Guide

Spring Design ScreenShare Service SDK Instructions

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Frameworks & Android. Programmeertechnieken, Tim Cocx

Why Alerts Suck and Monitoring Solutions need to become Smarter

DEVELOPING DATA PROVIDERS FOR NEEDFORTRADE STUDIO PLATFORM DATA PROVIDER TYPES

Getting Started with Zoom

1. Comments on reviews a. Need to avoid just summarizing web page asks you for:

UI Performance Monitoring

Finding Performance and Power Issues on Android Systems. By Eric W Moore

Tomcat Tuning. Mark Thomas April 2009

Chapter 2 Getting Started

Effective Java Programming. measurement as the basis

Zing Vision. Answering your toughest production Java performance questions

Programming Autodesk PLM 360 Using REST. Doug Redmond Software Engineer, Autodesk

Developing Algo Trading Applications with SmartQuant Framework The Getting Started Guide SmartQuant Ltd Dr. Anton B.

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

Google Drive: Access and organize your files

Specifications. What s Inside The box

Top Ten Qlik Performance Tips

Async startup WPF Application

VMware vcenter Log Insight Getting Started Guide

MS ACCESS DATABASE DATA TYPES

Android Developer Fundamental 1

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

PD 360 Training. Getting Started Series. PD 360 Essentials (Distance Learning) PD 360 Essentials. School Leadership and PD 360

Before reading this manual

Android Services. Android. Victor Matos

Configuring your client to connect to your Exchange mailbox

Sizmek Formats. IAB Mobile Pull. Build Guide

HTTPS hg clone plugin library SSH hg clone plugin library

Технологии Java. Android: Введение. Кузнецов Андрей Николаевич. Санкт-Петербургский Государственный Политехнический Университет

Understanding Files and Folders

The SwannCloud Mobile App

IOIO for Android Beginners Guide Introduction

User Guide for Android OS

Zoom Cloud Meetings: Leader Guide

Habanero Extreme Scale Software Research Project

User's Guide. [Home Network] app. Model No.

DETERMINATION OF THE PERFORMANCE

Android Framework. How to use and extend it

Part II. Managing Issues

Product Guide Nintex. All rights reserved. Errors and omissions excepted.

Reboot, reset, erase, power off, restore - what's the difference?

Mobile Conference Connection User Guide Android Mobile Device

Introducing Xcode Source Control

Accelerating Wordpress for Pagerank and Profit

Linux Scheduler. Linux Scheduler

Android Application Development Course Program

Scaling Database Performance in Azure

Affdex SDK for Android. Developer Guide For SDK version 1.0

MXwendler Javascript Interface Description Version 2.3

Event Log. Chapter 7 Event Log Event Log Management Excel Editing Create a New Event Log... 10

Transcription:

High-performance Android apps Tim Bray

Two Questions How do you make your app run fast? When your app has to do some real work, how do keep the user experience pleasant?

Jank Chrome team's term for stalling the event loop, i.e. not instantly responsive to input; now in Android we say A janky app Eliminate by: Reacting to events quickly Don't hog the event loop ( main / UI) thread! Getting back into the select() / epoll_wait() call ASAP, so you can react to future events Else... quickly (touches, drags)

ANR: App Not Responding Happens when: UI thread doesn't respond to input event in 5 seconds, or a BroadcastReceiver doesn't finish in 10 seconds Typically due to network or storage operations on main thread But users complain about delays much less than 5 seconds!

Some Nexus One Numbers One frame of 60 fps video: 16 ms. Human perception of slow action: 100-200 ms. ~0.04 ms: writing a byte on pipe process A->B, B->A or reading simple /proc files from Dalvik ~0.12 ms: void/void Binder RPC call A->B, B->A ~5-25 ms: uncached flash reading a byte ~5-200+(!) ms: uncached flash writing tiny amount 108/350/500/800 ms: ping over 3G. Variable! ~1-6+ seconds: TCP setup + HTTP fetch of 6k via 3G

Writing to flash (yaffs2) Create file, 512 byte write, delete (ala sqlite.journal in transaction) Flash is different than disks you're likely used to: read, write, erase, wear-leveling, GC Write performance is highly variable! Source: empirical samples over Google employee phones (Mar 2010)

Sqlite Performance There s no such thing as a cheap write Use indexes (see EXPLAIN & EXPLAIN QUERY PLAN) For logging, consider file-append rather than database-write

Lessons Writing to storage is slow Using the network is slow Always assume the worst; performance is guaranteed to produce bad reviews and Market ratings

Lessons Writing to storage is slow Using the network is slow Always assume the worst; performance is guaranteed to produce bad reviews and Market ratings So do your I/O and heavy computation on another thread, not the UI thread!

Tools: asynctask AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Tool: asynctask private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doinbackground(url... urls) { // on some background thread int count = urls.length; long totalsize = 0; for (int i = 0; i < count; i++) { totalsize += Downloader.downloadFile(urls[i]); publishprogress((int) ((i / (float) count) * 100)); } return totalsize; } protected void onprogressupdate(integer... progress) { // on UI thread! setprogresspercent(progress[0]); } protected void onpostexecute(long result) { // on UI thread! showdialog("downloaded " + result + " bytes"); } } new DownloadFilesTask().execute(url1, url2, url3); // call from UI thread!

Tool: asynctask Fire and forget mode private boolean handlewebsearchrequest(final ContentResolver cr) {... new AsyncTask<Void, Void, Void>() { protected Void doinbackground(void... unused) { Browser.updateVisitedHistory(cr, newurl, false); Browser.addSearchUrl(cr, newurl); return null; } }.execute()... return true; }

asynctask Details Must be called from a main thread rather, a thread with a Handler/Looper No nested calls! An activity process may exit before its AsyncTask completes (user goes elsewhere, low RAM, etc). If this is a problem, use IntentService

Tool: android.app.intentservice IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startservice(intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. Intent happens in a Service, so Android tries hard not to kill it Easy way to do use a Service

Calendar's use of IntentService public class DismissAllAlarmsService extends IntentService { @Override public void onhandleintent(intent unusedintent) { ContentResolver resolver = getcontentresolver();... resolver.update(uri, values, selection, null); } } In AlertReceiver extends BroadcastReceiver, onreceive() (main thread) Intent intent = new Intent(context, DismissAllAlarmsService.class); context.startservice(intent);

UI Tips Disable UI elements immediately, before kicking off your AsyncTask to finish the task Use an animation or ProgressDialog to show you re working One example strategy: 1. Immediately, disable UI elements 2. Briefly, a spinner in the title bar 3. If more than 200msec, show a ProgressDialog 4. in AsyncTask onpostexecute, cancel alarm timer

App performance lessons Network and storage UI hurt (but you already knew that) Excessive memory allocation hurts Reflection hurts See Design for Performance at developer.android.com: Static is good, internal getters/setters hurt, floats and enums hurt, make constants final,

Demo: LifeSaver Suppose you re getting a new Android phone. Your contacts and calendar and so on will move over automatically. But your history of phone calls and text messages won t. This is your life! So, run LifeSaver on your old phone and it copies that history to the SD card. Move the SD card to the new phone and run it again and it copies the history back. This takes a while!

Performance Advice Premature optimization is the root of all evil. -Donald Knuth

Performance Advice Premature optimization is the root of all evil. -Donald Knuth 1. Design the simplest thing that could possible work, where could possibly work excludes doing network transactions on the UI thread. 2. If it s fast enough, you re done! 3. If it s not fast enough, don t guess. Measure and find out why. 4. Fix the biggest performance problems. 5. goto 2

Profiling Tools Traceview Log.d() calls with a timestamp aren t terrible Extreme profiling: Aggregate user profile data

Traceview 1. android.os.debug.startmethodtracing(string fname) 2. android.os.debug.stopmethodtracing() 3. adb pull /sdcard/<fname>.trace <fname>.trace 4. traceview <fname>

Demo Profiling Tim s LifeSaver 2 application with traceview.

Traceview: Cautions 1. Overestimates the penalty for method dispatch 2. JIT is disabled 3. Numbers are not quantitatively reliable 4. Really only gives you a general feeling for what s happening

Future directions 1. Optimized screen manager 2. Strict mode, to detect I/O on the UI thread 3. Better profiling tools!

Thank you! feedback: bit.ly/mgddbr Tim Bray, Developer Advocate twbray@google.com android-developers.blogspot.com @AndroidDev