Thinking of CPU and Memory (2.0)

31
(Code for Responsiveness) Thinking of CPU & Memory

description

charla del droidcon españa 2012

Transcript of Thinking of CPU and Memory (2.0)

Page 1: Thinking of CPU and Memory (2.0)

(Code for Responsiveness)

Thinking of CPU & Memory

Page 2: Thinking of CPU and Memory (2.0)

Terminado!

La aplicación funciona

Page 3: Thinking of CPU and Memory (2.0)

Por que?

1 de cada 4 personas abandona una web que tarda más de 4 segundos en cargar

Page 4: Thinking of CPU and Memory (2.0)

Por que?

● Amazon: +100ms = -1% ventas● Google: un incremento de 0.4 segundos a

0.9 segundos en carga de pàgina equivale a un descenso de tràfico e ingresos del 20%

● Apps móviles?

Page 5: Thinking of CPU and Memory (2.0)

Cada vez somos más vagos exigentes

Page 6: Thinking of CPU and Memory (2.0)

El concepto

Caso clásico:1. Descargas datos2. Parseas3. Descargas más datos: imágenes, etc4. Cargas en memoria5. Los muestras en pantalla

Page 7: Thinking of CPU and Memory (2.0)

Don't be a Java Hero

"Tengo la impresión de que Java fue diseñado para hacer que fuera difícil escribir mal código, mientras que Python está diseñado para hacer

que sea sencillo escribir buen código."– Magnus Lycka

Page 8: Thinking of CPU and Memory (2.0)

Como?

● Compila contra el último SDK (hardware accel, p.ej)● Las splash-screens són malignas● No hacer trabajos en el UI Thread● No bloquear la UI (ProgressDialogs...)● GetView "ligeros"● No descargar los mismos datos 2 veces

○ punto 1 a tratar● Luchar por los 60fps

○ punto 2 a tratar

Page 9: Thinking of CPU and Memory (2.0)

Velocidad

Page 10: Thinking of CPU and Memory (2.0)

Strict Mode

.penaltyLog()

.penaltyDeath()

Page 11: Thinking of CPU and Memory (2.0)

Red(Enemigo nº 1)

Page 12: Thinking of CPU and Memory (2.0)

DDMS (Network Statistics)

Red (Análisis)

Page 13: Thinking of CPU and Memory (2.0)

12s -> 0.4s (wifi mala, móvil bueno)

Red (tip 1) - cache de string

Page 14: Thinking of CPU and Memory (2.0)

LruCache+DiskLruCache

● Displaying Bitmaps Efficiently (Android Developers)

● https://github.com/koush/UrlImageViewHelper○ No gestiona bien vistas recicladas (aún)

Red (tip 2) - cache de bitmaps

Page 15: Thinking of CPU and Memory (2.0)

CPUNo todos los móviles son de 4 núcleos

Page 16: Thinking of CPU and Memory (2.0)

Traceview (code y DDMS)

CPU (Análisis)

Page 17: Thinking of CPU and Memory (2.0)

Guardar y leer object

7s -> 0.8s (móvil malo)

Page 18: Thinking of CPU and Memory (2.0)

Mostrar datosviejos siempre

Ejemplo: Google+Mal-ejemplo: facebook

Page 19: Thinking of CPU and Memory (2.0)

Memória RAMSi no compartes, te echan

Page 20: Thinking of CPU and Memory (2.0)

$ adb shell procrank

Mantener la aplicación en memoria (Análisis)

Page 21: Thinking of CPU and Memory (2.0)

Mantener la aplicación en memoria (Tip)

Page 22: Thinking of CPU and Memory (2.0)

SmoothnessLos preciados 60 fps

Page 23: Thinking of CPU and Memory (2.0)

Smoothness

● Sección "robada" de:○ http://www.curious-creature.org/docs/android-performance-case-study-1.html

○ http://www.curious-creature.org/2012/12/06/android-performance-in-practice/

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

Page 24: Thinking of CPU and Memory (2.0)

Profile GPU Rendering

● profile first 128 frames of every window● Activar en dispositivo (dev. options)● adb shell dumpsys glxinfo com.test.app● se necesitan <16ms por frame

Page 25: Thinking of CPU and Memory (2.0)

Systrace

● Activar en dispositivo (dev. options)● tools/systrace/systrace.py (5 seconds by default)

Page 26: Thinking of CPU and Memory (2.0)

GPU OverDraw

● Como recomendación, podemos pintar cada pixel un máximo de 3 veces

● 9-patch for backgrounds

Page 27: Thinking of CPU and Memory (2.0)

Hierarchy Viewer

Page 28: Thinking of CPU and Memory (2.0)

● Heap dump (memoria por objetos)● Eclipse MAT (memoria por objetos)● PerfMon (memoria, cpu, red en float)● Usage Timelines Pro (cpu, memoria)● traceview (cpu)

Otras utilidades

Page 29: Thinking of CPU and Memory (2.0)

● Guardar un long en Application y mostrar un Toast con la diferencia al mostrar los primeros datos

● Enviar por analytics velocidades de boot

Detectar regresiones

Page 30: Thinking of CPU and Memory (2.0)

"Donald Knuth"

"La optimización prematura es la raíz de todos los males"

Page 31: Thinking of CPU and Memory (2.0)

Reférencias● Google I/O 2012 - Doing More With less: Beign 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