Thursday, June 4, 2015

Developing for Android, Part III - The Rules - Performance!

The next post in the series is Developing for Android, III: The Rules: Performance.

tl;dr : While performance and memory are closely intertwined, this article is more about runtime performance not memory-related.  Some basics that come out of article are:

* Avoid Expensive Operations during Animations and User Interaction - this can cause Jank
   - Measurement and layout is an expenive operation
   - Inflation happens on UI thread, and can be expensive (larger view hiearchy, more expensive)

* Launch Fast - User expects lightning fast feedback that the app is running
   - Android uses a Starting Window, which is blank but applies the applications theme (can have a specified background drawable)
   - Don't just use Starting window if your app takes seconds to deliver content.
   - Use a ViewStub as placeholder for views which can be expanded later, Avoid expensive ops like decoding large bitmaps.
   - Avoid Memory Churn
   - Avoid initilization code in your Application Object.

* Avoid Complex View Hiearchies - Find cheaper ways to represent nested content
   - Might try a custom view
   - If user can interact with an element (touch, focus, etc.) then element should be its own view vs combined.

* Avoid RelativeLayout near the top of the View Hierarchy
   - Expensive
   - Requires two measurement passes (gets exponential for each nested RelativeLayout)
   - Consider GridLayout - pre-processes child view relationship to avoid double measurement

* Avoid Expensive Operations on the UI Thread
   - This includes onDraw() or onLayout
   - Don't try to call webservices from UI Thread (will throw NetworkOnMainThreadException)
   - Don't do db transactions
   - Consider using Loaders (which handle android configuration changes / lifecycle)
   - Enable Strict Mode in Developer Options
   - UI Thread should be used for mainipulating view properties & drawing, everything else should be async

* Minimize Wakeups
  - Consider disabling BroadcastReceivers if you don't care about them
  - Make sure you carefully choose which intents your app should respond to - don't be overly broad.

* Develop for the Low End
  - Your primary dev device should not be a high end device
  - You should have a variety of devices including those with 512MB memory & 768x480 or less resloution

* Measure Performance
  - Use the tools to track performance
  - Aim for 60fps


No comments:

Post a Comment