2. Agenda
Tips and Tools
Battery Consumption
App Launch Time
App Size
UI Rendering
Memory Usage
It’s an ongoing process, you need to keep track of things in each release.
Performance is a feature.
3. Battery Consumption
Tips
Optimizing User-Initiated Network Use
1. Pre-fetch Network Data
2. Do not attempt to make unnecessary n/w connections if the n/w connection state is already known.
3. Reduce the Number of Connections by reuse existing connections.
Optimizing App-Initiated Network Use
1. Batch and Schedule Network Requests by using Job schedulers - Firebase job dispatcher. Doze mode.
2. Optimizing Server-Initiated Network Use by using push technology to send server updates.
3. Optimizing General Network Use
a. If n/w is bad, do not refresh ad too often.
b. Compress Data
c. Cache Files Locally
d. Optimize Pre-Fetch Cache Size
Wake lock
4. Battery Consumption
Tools
1. Battery Historian
Host
docker run -p <port> gcr.io/android-battery-historian/stable:3.0 --port <port>
Reset
adb kill-server
adb shell dumpsys batterystats --enable full-wake-history
adb shell dumpsys batterystats --reset
Run Test Case, Analyze via Battery Historian
2. Android Monitor
3. Different HTTP Call tagging
10. App Launch Time
Tips
Load with cached data and then refresh.
Light XML.
Delay time taking tasks.
Tools
i/activitymanager displayed
Activity.reportFullyDrawn()
11. App Size
Tips
Shrink Your Code and Resources
Use drawable objects, reuse resources, compress png/jpeg
Last choice : Build multiple APKs to support multiple screen densities or Application Binary Interfaces (ABIs)
Tools
APK Analyzer - Tool
14. Tips
Avoid nested hiererarcy
Avoid layout_weight parameter as each child needs to be measured twice.
Merge tag with Include to avoid one more level of hierarchy.
Viewstub to lazy load views.
Simple layout when when the layout is inflated repeatedly, such as when used in a ListView or GridView.
Use constraint layout over relativelayout.
Tools
Hierarchy Viewer
Lint tool
Profile GPU Rendering
Layout Inspector
UI Rendering
15. UI Rendering - Overdraw
Waste GPU Time to paint pixels which user don’t
see.
Removing unneeded backgrounds in layouts
Layout Inspector
Flattening view hierarchy
Reducing transparency
16. UI Rendering - Double Taxation
Layout Matters
Layout-and-measure iterations to measure the view’s finalize position.
First layout-and-measure pass to calculates each child object’s position and size
Uses this data, also taking object weights into account, to figure out the proper position of correlated views.
Finalize objects’ positions by performing a second layout pass
Layout-and-measure multiple passes can’t be completely avoided. But avoid in case of
1. Root element
2. Deep view hierarchy beneath it.
3. Inflating it several times as in case of list view.
18. UI Rendering - Use ViewStub
ViewStub - lightweight view.
Each ViewStub simply needs to include the
android:layout attribute to specify the layout to
inflate.
Also, useful to have same generic xml and
inflate the part as per case.
findViewById(R.id.stub)).setVisibility(View.VISI
BLE);
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"
/>
<ViewStub
android:id="@+id/stub"
android:inflatedId="@+id/inflatedLayoutId"
android:layout="@layout/loaded_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<include layout="@layout/empty_view"
android:id="@+id/empty_view" android:visibility="gone" />
</FrameLayout>
20. Memory Usage
Tips
Use services sparingly
Big no to static activities/views
Non-static nested classes - Avoid unintentional reference to any object
Unregister listeners when not in use. - Instance will keep a reference around long after the referenced object has
been terminated. Worst case - Object is activity.
Tools
Leak canary from square.
The Memory Monitor - Heap updates.
HPROF Viewer and Analyzer
Eclipse MAT
The Allocation Tracker
Leads to memory out of exception or nasty Illegal state exception.
29. Sources
Official android performance
Battery Historian
https://medium.com/@elifbon/android-application-performance-step-4-battery-
b1f88d096b1e
Memory Leak, Credit I & II
https://medium.com/freenet-engineering/memory-leaks-in-android-identify-treat-
and-avoid-d0b1233acc8
Rendering-
https://medium.com/@elifbon/android-application-performance-step-1-rendering-
ba820653ad3