This is an old revision of the document!
Table of Contents
Android Study
Background Processing
Doze
Alarms to not fire in Doze mode.
Foreground Services
Background Location
Saving Data
- Shared Preferences
- SQL
IME KEyboard Input
Starting from Android 1.5, the Android platform offers an Input Method Framework (IMF). https://android-developers.googleblog.com/2009/04/updating-applications-for-on-screen.html
An input method editor (IME) is a user control that enables users to enter text. To add an IME to the Android system, you create an Android application containing a class that extends InputMethodService.
Alarm Manager
Periodically eexecute
FusedLocationProvider
Uses Google Play API. Must add this to your gradle.build.
Must ask for ACCESS_BACKGROUND_LOCATION permission.
In Manifest:
<uses-permission android:name=“android.permission.ACCESS_COARSE_LOCATION”/>
private FusedLocationProviderClient fusedLocationClient;
// ..
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
Getting the last location:
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
}
}
});
LocationManager
ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION in manifest
Google Cloud Messaging (GCM)
Server informs the app on phone that there is new data on server. Battery efficient, the device does not have to poll.
Note: When using GCM, your app can pass messages in normal or high priority. Your server should typically use normal priority to deliver messages. Using this priority level prevents devices from being woken up if they are inactive and in a low-power Doze state. Use high priority messages only if absolutely required.
Pre-Fetching
- Download 2 to 5 minutes of data likely to be used. Such as one full song.
- 1 to 5Mb on a 3G network
- Measure usage so you know what best to download.
- Use HTTP live streaming where possible
- Record and determine usage patterns to know what to prefetch
- News all want to download a lot of articles and pictures and feed to app asynchronously to provide good user experience.
- Possibly schedule downloads when device is charging.Intent.ACTION_BATTERY_CHANGED
- Use Google Cloud Messaging (see above) to let the server notify app to fetch updates.
- Track activity of user and pre-fetch when user is not interacting with the app, but has the app open. Intent.ACTION_STRING. This is “Context Driven Prefetching”
https://www.youtube.com/watch?time_continue=175&v=Rk1u7VVmadE&feature=emb_logo
Batch Network Requests
- Network access is the biggest battery drain.
- After a network request the chip waits around using power for an extra 20 to 60 seconds.
- Piggy-back on other services firing up network. GCM Network Manager!
Use GCM Network Manager. It will allow you to batch requests and send/receive efficiently.
Android Performance Patterns Videos
https://www.youtube.com/playlist?list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE
- Screen refreshed 60 =time/sec, every 16ms.
- If a process will take 16ms, do it in a thread!
Threading methods:
- AsyncTask
- HandlerThread
- ThreadPool
- IntentService
HandlerThread
- Long running thread.
- Looper accessed by a Handler
- Gets jobs from a queue
- Should set thread priority!
ThreadPool Class
- ThreadPoolExecutor manages thread Pools. Spins up threads and you toss work to them.
- Only for large numbers of jobs
- Lots of work into little buckets
- Should set thread priority!
Each tread costs 64k minimum
AsyncTasks
- queue up.
- Waits for previous one to complete.
- If canceled, onCanceled() is called. Should not create AsyncTask as an inner class. If Activity gets destroy its reference will remain in memory until the task completes!
Intent Service Class
- Moves all intent responses to another thread for you
- Acts like a service, inherits from Service
- Makes your app less likely to be killed by the system
- Not a general purpose threading solution. Really only for intents
- Can call RunonUIThread or LocalBroadcast an intent to the Activity to update UI
LoaderManager
- Avoids memory leaks when UI is destroyed but threads are active
Latency
- Need to adapt to changing network quality
- 1) Gather info about the network, 2) Make adjustments
- Use connectivityManager and get the network connection type
- Use the emulator and use “Emulator Throttling”
- Use AT&Ts Network Attenuator
Minimize Asset Size
- Make as small as possible to avoid network bloat
- Large assets drain battery
- Large assets costs money for the user
- Don't use PNGs if not necessary
- Play with JPG quality to reduce size
- Store different sizes and qualities of images on your server
- JSON and XML are horribly large
- Protocol Buffers
- Nano-proto Buffers
- Flat Buffers
Use Network Monitor Tool in Android Studio to view asset usage issues
Service Performance Patterns
- Services can drain system resources
- Services run on the UI Thread!
Minimize Code
- Use Proguard to remove unused code, obfuscate method names, create a smaller size
- Enable this by an entry in the gradle file. MinifyEnabled“
- “ShrinkResources” will remove unused resources from build
Cache
- Use LRUCache to cache data. It is easy!
Approximation
- Use easier to calculate values when possible.
- Example: may not need high resolution position info all the time
- “Good Enough. Let's Ship it”
Culling
- Remove code where you are wasting processing time.
- Use ClipRect when drawing custom views
- Overdraw is not so much a problem in the latest OS improvements
Data Serialization
- Don't extend your class to a Serializable class. Inefficient.
- GSON library is more efficient than Serializable, but uses JSON (bloated)
- JSON and XML are inefficient!
- Android resource files are XML but are compiled into a more efficient structure for run-time
- Protocol Buffers - Not good for mobile development, lots of overhead
- Nana-Proto-Buffers is Proto Bufs optiminized for mobile development
- FlatBuffers focused on performance - Efficient! Best to use.
- SharedPreferences is a key/value store, best for simple storage
- Parcelable - best for passing between running processes
USE FlatBuffers to pass data instead of JSON!
- SQLite - Store structured data
Smaller Serialized Data
- To serialize objects efficiently - Struct-of-Arrays is efficient - then FlatBuffer the result
Caching UI Data
- While displaying cached data while fetching new data, inform user with some type of “Syncing” message/
CPU Frequency Scaling
- The OS slows down processor to save battery
- Frame rates may go down
- Use Systrace to determine what is happening
Array Maps
- More efficient than HashMap
- Runtime access grows a bit as data size grows
- Inserts and deletions cost a bit more
- Uses less memory when small
- Best used if number of key value pairs is not large. There is an efficiency hit, so use HashMap for large sets of key/vale pairs
- Don't need to use iterator, can use index in a for loop. More efficient
- Use when you have < 1000 objects
- Use when you have Maps of Maps
Beware Autoboxing
- Ex: Using Integer as int, may cause a lot of temp object creation in for loops!
- This overhead shows up in HashMap access. So use specific types:
- Special types of HashMap for NO Autoboxing:
- HashMap <obj, obj>
- SparseBoolMap<bool, obj>
- SparceIntMap<int, obj>
- SparceLongMapMlong, obj>
- LongSparceMap<long, obj>
- SparseMaps always use primitive type as the key
- Use Allocation Tracker to find where a lot of inter objects are coming from one call site.
- With traceView find where a lot of allocations are happening
Price of Enums
- Enums take a lot of memory
- Enums are like Gremlins
- Try to avoid enums!
- Use @intDef1) to avoid memory costs
- ProGuard has an option to optimize enums
Trimmed and Shared Memory
- onTrimMemeory(trimValue) is called to inform your app to release unnecessary memory. All in-memory apps are notified. Activity, Service, Fragment all get this notification.
- onLowMemory() callback happens after all other apps are killed
- ActivityManager.isLowRamDevice(), you can query this
Do NOT Leak Views
- Don't reference views inside of Async callbacks
- Don't reference views from static obhects.
- Be careful storing views in collections or WeakHashMap s.
- Use Allocation Tracker in Android Studio
Location and Battery
- setInterval()
- Back off interval if user is not changing position much
- Updated at fastest rate of any apps. You can get flooded
- setFastestInterval(), limits how fast your app gets positions
- LocationRequest.setPrioroty()
- FusedLocationProvider
layouts
- Watch cascading repositioning
- Use Systrace
- Minimize the depth in view hierarchy
- Avoid calling layout requests when not necessary
Object Pools
- Can be the source of a lot of GC (Garbage Collection) events
- Allocate in groups - allocates have overhead
- Pre-allocate groups at start of app to make startup fast. Minimized having to go back to the heap many times at start of app.
- You are in charge of freeing from the pool. No auto GC.
- When freeing an object, clen up variables to make sure they do not reference other objects. This is a memory leak.
- USe the AllocationTracker
- Reduces memory churn
Iterators or Index?
- Every time you get an iterator it results in a memory allocation
- it.next() takes some resource to get the next.
- Adnroid platform team does not use iterators!!!
Tools to use
- Systrace
- Network Monitor Tool - in Android Studio
- Network Attenuator from AT&T
- ARO Tool from AT&T
- Allocation Tracker
- TraceView
- Battery Historian
NOTES:
New Java 5 syntax:
for (Object obj : list {
}