Thinking cpu & memory - DroidCon Paris 18 june 2013

32
(Code for Responsiveness) Thinking of CPU & Memory

description

http://www.paug.fr

Transcript of Thinking cpu & memory - DroidCon Paris 18 june 2013

Page 1: Thinking cpu & memory - DroidCon Paris 18 june 2013

(Code for Responsiveness)

Thinking of CPU & Memory

Page 2: Thinking cpu & memory - DroidCon Paris 18 june 2013

Done!

The app works!

Page 3: Thinking cpu & memory - DroidCon Paris 18 june 2013

Why?

25% of people leave a web page if it takes more than 4 seconds to load

Page 4: Thinking cpu & memory - DroidCon Paris 18 june 2013

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

Page 5: Thinking cpu & memory - DroidCon Paris 18 june 2013

The user is very demanding

Mobile != Computer

Page 6: Thinking cpu & memory - DroidCon Paris 18 june 2013

The concept

Classic case:1. Download some data2. Parse3. Download more data (images, audios, ...)4. Load it on memory5. Show it on screen

Page 7: Thinking cpu & memory - DroidCon Paris 18 june 2013

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

Page 8: Thinking cpu & memory - DroidCon Paris 18 june 2013

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

Page 9: Thinking cpu & memory - DroidCon Paris 18 june 2013

Speed

Page 10: Thinking cpu & memory - DroidCon Paris 18 june 2013

Strict Mode

.penaltyLog()

.penaltyDeath()

Page 11: Thinking cpu & memory - DroidCon Paris 18 june 2013

Network(Public enemy #1)

Page 12: Thinking cpu & memory - DroidCon Paris 18 june 2013

DDMS (Network Statistics)

Red (Análisis)

Page 13: Thinking cpu & memory - DroidCon Paris 18 june 2013

12s -> 0.4s (bad network, good smartphone)

Network - cache downloaded data

(this code isn't complete)

Page 14: Thinking cpu & memory - DroidCon Paris 18 june 2013

Cache:● Universal-Image-Loader● Picasso

Bandwidth:● don't send too big bitmaps● WebP (~-30%)

Bitmaps

Page 15: Thinking cpu & memory - DroidCon Paris 18 june 2013

CPUNot everybody have a Nexus 4

Page 16: Thinking cpu & memory - DroidCon Paris 18 june 2013

Traceview (code y DDMS)

CPU (Analyze)

Page 17: Thinking cpu & memory - DroidCon Paris 18 june 2013

Cache objects

7s -> 0.8s (bad smartphone)

Page 18: Thinking cpu & memory - DroidCon Paris 18 june 2013

Show old data always

Page 19: Thinking cpu & memory - DroidCon Paris 18 june 2013

RAMIf you don't share you get kicked

Page 20: Thinking cpu & memory - DroidCon Paris 18 june 2013

$ adb shell procrank

Keep the app in memory (Analyze)

Page 21: Thinking cpu & memory - DroidCon Paris 18 june 2013

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

Page 22: Thinking cpu & memory - DroidCon Paris 18 june 2013

Keep the app in memory

Page 23: Thinking cpu & memory - DroidCon Paris 18 june 2013

Smoothness*The wanted 60fps

*(Section "stolen" from Romain Guy)

Page 24: Thinking cpu & memory - DroidCon Paris 18 june 2013

Smoothness

● Profile GPU rendering (4.1)● GPU Overdraw (4.2)● Systrace (4.1)● Hierarchy Viewer

Page 25: Thinking cpu & memory - DroidCon Paris 18 june 2013

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

Page 26: Thinking cpu & memory - DroidCon Paris 18 june 2013

Systrace

● Enable on device (dev. options)● tools/systrace/systrace.py or ddms

Page 27: Thinking cpu & memory - DroidCon Paris 18 june 2013

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

Page 28: Thinking cpu & memory - DroidCon Paris 18 june 2013

Hierarchy Viewer

Page 29: Thinking cpu & memory - DroidCon Paris 18 june 2013

● PerfMon (memory, cpu, network on a floating window)● Usage Timelines (cpu, memory)

Other tools

Page 30: Thinking cpu & memory - DroidCon Paris 18 june 2013

● 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

Page 31: Thinking cpu & memory - DroidCon Paris 18 june 2013

"Donald Knuth"

"Premature optimization is the root of all evil"

Page 32: Thinking cpu & memory - DroidCon Paris 18 june 2013

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