thinking cpu & memory - droidcon paris 18 june 2013
DESCRIPTION
http://www.paug.frTRANSCRIPT
(Code for Responsiveness)
Thinking of CPU & Memory
Done!
The app works!
Why?
25% of people leave a web page if it takes more than 4 seconds to load
why?
● Amazon: +100ms = -1% sales● Google: going from 0.4 seconds to 0.9 seconds loading
a page causes a 20% decrease of benefits● I don't have data about smartphones, but IMHO is even
worse
The user is very demanding
Mobile != Computer
The concept
Classic case:1. Download some data2. Parse3. Download more data (images, audios, ...)4. Load it on memory5. Show it on screen
Don't be a Java Hero
It seems to me that Java is designed to make it difficult for programmers to write bad code, while Python is designed to make it easy to
write good code.” — Magnus Lycka, Aug. 18, 2005
How?
● Compile always with the latest SDK (hardware accel, ...)● Splash-screens are evil● Don't do work on UI Thread● Don't block the UI (ProgressDialogs...)● Efficient GetViews● Don't download same data 2 times● Fight for the 60fps
Speed
Strict Mode
.penaltyLog()
.penaltyDeath()
Network(Public enemy #1)
DDMS (Network Statistics)
Red (Análisis)
12s -> 0.4s (bad network, good smartphone)
Network - cache downloaded data
(this code isn't complete)
Cache:● Universal-Image-Loader● Picasso
Bandwidth:● don't send too big bitmaps● WebP (~-30%)
Bitmaps
CPUNot everybody have a Nexus 4
Traceview (code y DDMS)
CPU (Analyze)
Cache objects
7s -> 0.8s (bad smartphone)
Show old data always
RAMIf you don't share you get kicked
$ adb shell procrank
Keep the app in memory (Analyze)
Other tools to analyze memory usage
● adb shell dumpsys meminfo● Heap dump
○ capture it with DDMS○ +HeapDumpOnOutOfMemoryError
● MAT (Memory Analyzer Tool)○ analyzes your heap dump○ hprof-conv package.hprof package-converted.hprof○ It has a stand-alone version if you don't want to use Eclipse
Keep the app in memory
Smoothness*The wanted 60fps
*(Section "stolen" from Romain Guy)
Smoothness
● Profile GPU rendering (4.1)● GPU Overdraw (4.2)● Systrace (4.1)● Hierarchy Viewer
Profile GPU Rendering● profile last frames rendered● You need to enable it on device (dev.options)● you need <16ms per frame to get 60fps● ddms -> System Information -> Frame render time
Systrace
● Enable on device (dev. options)● tools/systrace/systrace.py or ddms
GPU Overdraw
● As a general recommendation we can paint every pixel a max of 3 times
● 9-patch for backgrounds● lint can warn you about layouts
with possible overdraw
Hierarchy Viewer
● PerfMon (memory, cpu, network on a floating window)● Usage Timelines (cpu, memory)
Other tools
● Save a long with start time on Application and show a diff with current time on a toast when you are end "printing" screen
● Send through analytics performance data● You can do it on all parts of your app
Detect regressions
"Donald Knuth"
"Premature optimization is the root of all evil"
References● Google I/O 2012 - Doing More With less: Being a Good
Android Citizen● Designing for Performance (developer.android.com)● "Displaying Bitmaps Efficiently" Android Developers● http://www.curious-creature.org/docs/android-performance-
case-study-1.html● http://www.curious-creature.org/2012/12/06/android-
performance-in-practice/
twitter: @oriolj+Oriol Jiménez