Enrique Lopez Manas "Android High Performance"

60
Android High Performance Enrique López Mañas Google Developer Expert & IT Consultor

Transcript of Enrique Lopez Manas "Android High Performance"

Android High Performance

Enrique López Mañas

Google Developer Expert & IT Consultor

How it did start?

Performance

"how well a person, machine, etc. does a piece of work or an activity"

Cambridge

But what is high performance?

• Strategies to create efficient software

Layout

Energy and battery

Security

Multithreading Programming patterns

Debugging techniques

Why is important?

• User Engagement

• Costs

• Maintenance

• Quality

3 Important Limits

0.1s 1s 10s

[Miller 1968; Card et al. 1991]:

What this means in Android?

ANR

ANR

• No response for input event in 5 seconds

• BroadCast receiver still executing after 10 seconds

How to avoid it?

Background operations? Show feedback!

How to avoid it?

Splash Screen?

How to avoid it?

Calculations? Worker thread!

How to avoid it?

Systrace! TraceView!

Debug

DDMS

Networking

Layouts

TV: 24fps(NTSC), 25fps(PAL)

New cam-coders / slow motion: 48fps+

Blur can occur!

Apps: 60fps!

every 16 ms!

Layouts

Layouts

Reusing layouts

Reusing layouts

Reusing layouts

ViewStub

ViewStub.inflate() View.setVisibility()

ViewStub

Hierarchy Viewer

Profile GPU Rendering

Profile GPU Rendering

• Blue: draw • Purple: prepare • Red: process • Orange: execute

Memory

“the ability to remember information, experiences, and people"

Cambridge

Memory - Enumerations

Memory - Enumerations

Memory - Enumerations

Memory - Enumerations

Memory - Enumerations @IntDef

Memory - Enumerations @IntDef

Memory - Enumerations Summary

• Unnecessary allocations • AVOID USING THEM • Change as much as possible with static final integer values

Memory - Constants

Constants in the code should be static and final to take advantage of memory savings and to avoid initialization in Java compiler

Memory - Strings

Memory - Strings

Memory - Strings

Memory Leaks

“a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released.” Wikipedia

Memory Leaks - static fields

Memory Leaks - static fields

Memory Leaks - non-static inner classes

Memory Leaks - WeakReference

Threading

“Threading is a method of hair removal originating in Asia.”

Wikipedia

Threading

“is the smallest sequence of programmed instructions that can be managed independently by a scheduler”

Wikipedia

Threading

• AsyncTask • Loader • Threads…

Know your war!

Threading - AsyncTask

Communicate Worker Thread with UI

Does not need to communicate UI? Does not need to notify user? DOES NOT NEED ASYNCTASK!

If the AsyncTask has all parameters Void, you are only implementing doInBackground() and you do not need an AsyncTask

Threading - AsyncTaskLoader

Use it to fetch data

Same features as AsyncTask…

…plus the Activity life-cycle independency and data caching

Threading - AsyncTaskLoader

Threading - Services

• Not threads! • Executed in UI Thread by default • Never start a long running operation! • Lifecycle

Threading - IntentService

• Particular implementation of a Service • Execute sequential operations in background • Don’t need to handle the lifecycle

Networking

“process of communication between different terminal nodes to exchange data”

Me

Networking - best practices

Latency Gauging

Networking - best practices

Batch connections

• Wait, collect and send • (Analytics)

Networking - best practices

Pre-fetch

• Download as much as possible • (specially if idle connection)

Networking - best practices

Queuing connections

Networking - best practices

Caching responses

Networking - best practices

Last modified

Networking - best practices

Exponential back-off

I want your feedback!

http://bit.ly/feedbackMobicode

@eenriquelopez