Upload
horace-mcdaniel
View
218
Download
0
Tags:
Embed Size (px)
Citation preview
Mobile Application Optimization
Sanath NandasiriProgrammer.(Not a singer)
(Exilesoft)
Outline
• Why we need optimization.• Performance tuning life cycle. • Networking Optimizations.• Memory Optimizations.• UI Optimization.• Reduce Size of APK.
Why we need optimization
Pretty simple.
Performance TuningYou might have good reason to optimize
Networking Optimizations
• Network handling.• Network Request.• Batching background work.• Battery.
Network handlingRespect your user
Network radio
Network RequestHow to make good request.
Type of Network request.
Server pulling is bad? It drains Battery so past
Server push is better ( Using GCM)
Good way of handling the server pulling
Try to batching when ever possible.
Also prefetching data for future use.
Batching background work
Sync later.
Use alarm manager
Exact Alarm Inexact Alarm
How to set
Using Sync Adapters.
GcmNetworkManager (Pre L)
JobScheduler API (L).
BatteryMay be your major concern.
Measure battery consumption.
• Use battery historian tool.• https://github.com/google/battery-historian.• adb shell dumpsys batterystats --reset• adb shell dumpsys batterystats> report.txt• historian.py report.txt> raport.html
DemoUse battery historian tool
Memory Optimizations.
• HashMap.• AutoBoxing.• SparseArrayFamily.• Enums.• Iterator or index.• BitMaps.• Memory Leaks.
HashMap
HashMap
ArrayMaps
ArrayMaps search
ArrayMap search
ArrayMap allocation vs HashMap allocation
Where to use ArrayMap
AutoBoxing
What is autoboxing
Primitive vs Generics
Problem with HashMap and ArrayMap
SparseArray Family
No autoboxing
Enums.Enums sucks.
Cost of Enums
“Enums often require more than twice as much memory as static constants.You should strictly avoid using enums on Android”
Integer vs Enums
20+ byte for each
12-16 byte array
This is 13x more than integer version
Using Integer constant with @IntDef
Stop using Enums.if already used, try to setup proguard
Iterator or index.
Method code
for Index Int size = list.size();for(int i = 0 ;I < size ; i++){ Object o = list.get(i)}
Iterator for(Iterator it = list.iterator(); it.hasNext();){ Object o = it.next();}
for simple for(Object o : list){…}
Method for Iterate a list
Stats of all methods
Bitmaps.Memory hungry
Bitmap need continues memory blocks
Select smaller pixel format
Scale the Bitmap
Reuse Bitmap
Good to use libs
Memory LeaksAvoid, May cause to crash or freeze.
Memory leaks
• GC not prevent memory leaks• Reference to an unused object preventing GC.• Reference to Activity (Context)• View• Drawables• Etc…
Object in Heap
• Shallow Heap• How large this object is. (size in byte)
• Retained Heap• Total memory freed by removing particular object.
How to detect memory leaks
• Using MAT (Memory Analyzer Tool)• It uses Dominator tree to calculate retained heap.
Dominator tree
• Dominator : closest object on every path to node
Demouse MAT
Memory Analysis for Android Applications
UI OptimizationBeauty need more care.
Hierarchy Viewer Walkthrough
Hierarchy Viewer to see UI view hierarchy
Avoid much deeper layout hierarchy.
Custom View for more complex view
LintUse Lint, to have a hint.
Improving Your Code with lint
How lint works
Lint report
DemoUse Lint tool.
Reduce Size of APKTo save users data.
Putting Your APKs on Diet
Steps
• Run Proguard• Use Lint extensively• Be opinionated about resources• Use hdpi, xhdpi and xxhdpi
• Minimize resources configurations• Compress images• Code reuse