Profiling tools and Android Performance patterns

31
Profiling tools and Android performance patterns Providing a good user experience

Transcript of Profiling tools and Android Performance patterns

Page 1: Profiling tools and Android Performance patterns

Profiling tools and Android performance patternsProviding a good user experience

Page 2: Profiling tools and Android Performance patterns

The mobile agency for food retail

Page 3: Profiling tools and Android Performance patterns

Jonathan Maldonado Contreras @Jonazan2

Android Developer

Page 4: Profiling tools and Android Performance patterns

“Mobile internet will become

the most important medium

in people's lives.”

Founded in 2002

Page 5: Profiling tools and Android Performance patterns

• Adding emotion to transactional loyalty

• 120 developers, designers & specialists, HQ in Amsterdam, NL

• Operating in 3 continents: Europe, North America and Asia

The magic happens at the intersection

IceMobile

Creativity Technology

Page 6: Profiling tools and Android Performance patterns

Performance is about user experience

Apps nowadays are all about fancy animations, complex transitions and custom views

And the experience must be the most robust and similar as possible in any device

Page 7: Profiling tools and Android Performance patterns

CPU GPU

BatteryMemory Network

Page 8: Profiling tools and Android Performance patterns

GPU profilingUnderstanding the rendering pipeline

Page 9: Profiling tools and Android Performance patterns

Our apps must run at 60 frames per second

————1000ms60 frames

= 16,66ms per frame

In this 16,66 ms we have to:

• Do any computation

• Update our screen to the GPU

• Draw the screen

• Deal with GC events

Page 10: Profiling tools and Android Performance patterns

Settings Developer options

Profile GPU rendering Show on

bars

• Green: 16ms barrier

• Blue: create and update the views

display list

• Purple (4.0+): time transferring

resources to rendering thread

• Red: android 2d renderer in OpenGL

• Orange: CPU’s waiting time

Profiling GPU rendering

16ms

Page 11: Profiling tools and Android Performance patterns

A lot of frame drops? Check for:

• Unnecessary invalidations that may trigger a cascade of invalidations.

(GPU View Updates can help profiling)

• RelativeLayouts in a high hierarchy level

• Nested weights in LinearLayouts

• Unnecessary overdraw

• Customs views not properly made

Patterns to avoid bad performance

Page 12: Profiling tools and Android Performance patterns

• Use <merge> as root in your layout

• Avoid unnecessary layouts

• Don’t make allocations or heavy operations in OnDraw

• Remove unnecessary calls to invalidate()

• Consider creating your own ViewGroup

invalidate() hierarchy invalidations onDraw()

Patterns for custom views

Make them as simple as possible

OnMeasure/OnLayout…

Page 13: Profiling tools and Android Performance patterns

• True color: No overdraw

• Blue: Overdrawn once

• Green: Overdrawn twice

• Pink: Overdrawn three times

• Red: Overdrawn four times

Profiling GPU overdraw

Page 14: Profiling tools and Android Performance patterns

• Depends on the GPU architecture: Nvidia’s Tegra is different than

ImaginationTech’s SGX and so on.

• Hierarchy Viewer (ViewServer by @RomainGuy for any phone) and

Tracer

Steps to follow when you have too much overdraw:

• Simplify your drawables

• Use 9patch with transparent parts

• Caution with setting alpha in your views

About overdraw

Page 15: Profiling tools and Android Performance patterns

Memory profilingUnderstanding the garbage collector

Page 16: Profiling tools and Android Performance patterns

Displays:

• Free and allocated memory in real time

• GC events

Allows us:

• Cause GC events

• Start the allocation tracker

• Dump the Java Heap

Memory monitor

Page 17: Profiling tools and Android Performance patterns

• If our app is using an excessive amount of memory

• When your app is running out of memory

• If a GC event occurs and the memory is not properly free

• If the app is slow due to a vast amount of GC events, a GC cascade

What the memory monitor can tell us

Runtime

totalMemory()

maxMemory()

current heap size (bytes)

max heap size (bytes)

Page 18: Profiling tools and Android Performance patterns

Heap dump

We can use the heap dump to check:

• The current memory state for our app

• The amount of objects

• Type of those objects

• Hierarchy between references

Page 19: Profiling tools and Android Performance patterns

Allocation Tracker

Allocation tracker displays:

• Which classes and methods are allocating which objects

• It’s possible to see allocated memory in each thread of your app

Page 20: Profiling tools and Android Performance patterns

Allocation tracker

Heap

Memory monitor

All integrated within Android Studio

Page 21: Profiling tools and Android Performance patterns

Using our profiling tools in Android Studio:

1. Open memory monitor before opening an activity

2. Start the allocation tracker and dump the heap after loading the

activity

3. Go to a different activity and force a GC event

4. Stop allocation tracker, dump the heap...and investigate

Discovering memory leaks

Allocation space in the HEAP

* The allocation space is different between Dalvik and ART

Allocation space in the HEAP

GC EVENT

Leaked Memory

Leaked Memory

Allocated Memory

Allocated Memory

Needed Memory

Needed Memory

OUT OFMEMORYERROR

Page 22: Profiling tools and Android Performance patterns

Don’t leak contexts in inner classes.

Don’t leak views inside an activity.

Don’t use WeakHashMap as cache. Only the keys are weak references.

Use the proper context. AppContext != View context

Use static classes instead of Inner classes.

Use WeakReference or SoftReference.

Release callbacks in a proper way (handlers, observers...)

Avoiding memory leaks

Page 23: Profiling tools and Android Performance patterns

The principal cause of GC cascades is memory churn: high number of

objects get allocated and potentially free in a short period of time.

GC cascades: discovering and avoiding memory churn

16ms 16ms 16ms 16ms

GCUPDATE GCUPDATE UP D

Framedrop

GC ATE

draw draw draw

Page 24: Profiling tools and Android Performance patterns

• Don’t allocate a large amount of unnecessary objects

• Immutable classes: String

• Autoboxing: Integer, Boolean…

• Don’t allocate in onDraw

• Consider using Object Pools

Patterns to avoid memory churn

Page 25: Profiling tools and Android Performance patterns

• SparseArray family to avoid

autoboxing

• Hundred of objects, not

thousands

• Use for with size and get

• Integer constants

• Unnecessary objects:

• Inmutable classes: String

• Autoboxing: Integer, Boolean…

• Foreach and iterators

• Enums

Patterns to use in critic areas of the app

inner loops animations custom views

Page 26: Profiling tools and Android Performance patterns

• Handle Bitmaps in a proper way:

• Decode bitmaps to the desire size

• BitmapFactory.Options (inSampleSize, inDensity,

inTargetDensity)

• Load bitmaps in memory at the dimensions is going to be

displayed

• Don’t scale if you don’t need to (createScaledBitmap(btimap, int,

int).

• Use LRU cache.

Handling bitmaps

inSampleSize density

Page 27: Profiling tools and Android Performance patterns

Performance is necessary for every app

“If we can write a map in a single line and you can put a value and get a value, we don’t care how long it takes...”

Conference attendant at CppCon2014

“Ok, [...] people who don’t care about how long it takes is also the

reason why I’ve to wait 30 seconds for Word to boot up”

Mike Acton

Engine director at Insomniac Games

Page 28: Profiling tools and Android Performance patterns

Tools, not rules

Page 29: Profiling tools and Android Performance patterns

• Android Performance Case Study by @RomainGuy

• Performance Profiling Tools - https://developer.android.com/tools/

performance/index.html

• Android performance patterns videos by Colt McAnlis (@duhroach)

• Google + community: http://goo.gl/g7mxmI

References

Page 30: Profiling tools and Android Performance patterns

@icemobile /company/icemobileicemobile.com/tech

Thank you!

Page 31: Profiling tools and Android Performance patterns