optimizing your android...

36
Optimizing Your Android Applications Alexander Nelson November 22, 2019 University of Arkansas - Department of Computer Science and Computer Engineering

Upload: others

Post on 03-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Optimizing Your Android Applications

Alexander Nelson

November 22, 2019

University of Arkansas - Department of Computer Science and Computer Engineering

Page 2: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

The Problem

Page 3: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Reminder – Immediacy and responsiveness

Three time limits1:

• < 0.1s - User feels system acts instantaneously

• < 1s - Maintain user’s flow of thought

• < 10s - Maintain user’s attention

Plan for these limits!

Use loading screens/timers when necessary

1Jacob Nielsen - 1993

Page 4: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Why do you need to optimize?

Reasons to optimize:

1) Meet time constraints

2) Users complain about apps that run slowly = bad reviews!

3) Inefficient background services use battery

Page 5: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Why is optimizing difficult?

Optimizing mobile applications is difficult because:

• Mobile phones have less compute resources than workstations

• Different mobile devices with various specifications

• UI- or Event-driven code is harder to profile

• Running on the emulator doesn’t necessarily match theperformance on the device

• Especially if the device has a JIT

Page 6: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Optimizing Android Applications

Page 7: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

What are goals for optimization?

The primary goals for optimization are:

• Meet all time constraints

• Use less battery

• Use less cellular data – We already went over this

Page 8: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Optimizing for Performance

Primary focus of this lecture is optimizing for performance

Resources:

• https://developer.android.com/topic/performance

Page 9: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

First Principle

Most important design decision?

Algorithm and data structure selection

No performance tip can outrun a bad design decision

Page 10: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Writing Efficient Code

Two basic rules for writing efficient code:

• Don’t do work that you don’t need to do

• Don’t allocate memory if you can avoid it

Page 11: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Avoid creating unnecessary objects

Creating an object = Memory allocation

Creating a lot of objects forces garbage collection

Tips for reducing object creation:

• Change method signatures to manipulate existing objects

• Slice multi-dimensional arrays into parallel one-dimension

arrays

• Avoid creating short-term temporary objects whenever possible

Page 12: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Prefer Static

If you don’t need to alter an object’s fields – make method static

Invocations will be 15-20% faster

Good coding practice anyway – method signature indicates the

object can’t be manipulated

Use Static Final for constants

• Only applies to primitives and String constants

• Reduces initialization and access time

Page 13: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Use Enhanced For Loop Syntax

The enhanced for loop (or

for-each loop) can be used for

Iterable collections and arrays

Zero is slowest

One is slower than Two without

a JIT

Equivalent with a JIT

Page 14: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Package Access for Private Inner Classes

Accesses to private

values/methods from inside

private inner classes occur

thorough static accessor methods

Performance overhead to use

accessor methods

Use package level instead of

private when not sensitive

Page 15: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Avoid using Floating Point

Rule of Thumb: Floating point is 2x slower than integer on

android devices

In terms of speed, float and double are equivalent

Double is 2x larger in memory

If space isn’t an issue and you require float, use double over float

Along those lines – Avoid divide if possible

Many devices lack a hardware divider

Page 16: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Know and Use Libraries

Library code is likely safer and more optimized than hand-written

code

The system can replace library calls with hand-coded assembler

Using System.arraycopy() is 9x faster on a Nexus One with JIT

than a hand-coded copy loop

Page 17: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Native Methods

Using the Java Native Interface may or may not produce

performance gain

Will require compilation on each architecture the code will run on

Often, the JIT will optimize code equal-to or better than

hand-written code

For more on the JNI with Android:

https://developer.android.com/training/articles/

perf-jni.html

Page 18: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Performance Myths

Invoking methods with an exact typed variable is more efficient

• Slightly true for devices without a JIT

Caching field accesses is faster

• About 20% faster without JIT

• About the same with a JIT

Page 19: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Measure your performance

Before starting to micro-optimize, make sure you have a problem

to solve

Accurately measure existing performance and the goal

Traceview can be useful for profiling

BUT– Traceview currently disables existing JITs, and may

misattribute time

Page 20: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Measuring Performance

Page 21: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Tools to measure performance

Tools:

• Traceview

• Hierarchy View

• Memory Monitor (deprecated)

• Method Tracer (deprecated)

• Android Profiler (Android Stuido 3.0)

Page 22: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Traceview

Traceview –Provides graphical representation of trace logs

Traces generated by instrumenting code with the Debug class

Page 23: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Generating Trace Files

Debug.startMethodTracing() + Debug.stopMethodTracing()

Logs trace data form start->stop

Requires external write permission

startMethodTracing(filename) can be used to create named

tracefiles

Log files are by default stored in getExternalFilesDir()

Page 24: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Hierarchy View

Use to locate an examine view elements within current application

Page 25: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Android Profiler

Android Profiler – New to Android Studio 3.0

Replaces functionality of previous monitoring tools

Page 26: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Advanced Profiling

If running API 25 or lower, you need to enable advanced profiling

Enables:

• The event timeline on all profiler windows

• The number of allocated objects in Memory Profiler

• Garbage collection events in Memory Profiler

• Details about all transmitted files in Network Profiler

Advanced profiling does slow speed (especially involving JIT)

Page 27: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Enabling Advanced Profiling

How to enable Advanced Profiling:

1. Run>Edit Configurations

2. Select app module in left pane

3. Click Profiling Tab

4. Check Enable Advanced Profiling

Page 28: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

CPU Profiler

CPU Profiler – Monitor Application CPU usage at macro level

(For detail, record method traces)

Page 29: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

CPU Profiler

1. Event Timeline – UI Events

2. CPU Timeline – Real time CPU Usage

3. Thread Activity Timeline – Each thread and activity

4. Recording Configurations – Sampled/Instrumented

5. Record Button – Starts/stops recording a method trace

Page 30: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Memory Profiler

Memory Profiler – Identify memory leaks and churn

Shows real-time graph of application memory use

Can capture a heap-dump, force garbage collection, track memory

allocation

Page 31: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Memory Profiler

1. Force Garbage Collect

2. Capture Heap Dump

3. Record Memory Allocation

4. Zoom in/out of timeline

6. Jump forward to live

memory data

7. Event Timeline

8. Memory Use Timeline

Page 32: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Network Profiler

Network Profiler – Display and inspect realtime network activity

Examine how/when app transfers data in order to optimize

Page 33: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Network Profiler

Network Profiler

Page 34: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Inspecting GPU Rendering

To achieve 60 frame per second, GPU should be drawing approx.

every 16ms

You can view GPU rendering graph on the device

Settings >Developer Options >Monitoring >Profile GPU

Rendering

Select “On Screen as Bars”

Open App you want to profile

Page 35: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Inspect GPU Rendering

Page 36: Optimizing Your Android Applicationscsce.uark.edu/~ahnelson/CSCE4623/lectures/lecture18.pdfperformance on the device Especially if the device has a JIT Optimizing Android Applications

Inspect Battery Usage

Battery Historian – Docker image which uses device logs of battery

usage