Notes from OnAndroid 2013 Oct conf: Dalvik VM

This talk was all about Dalvik under the hood. The full slides are available in this SpeakerDeck page. The most useful slides in this talk are towards the end – they contain interesting optimization tips while developing code for Android.

  • Dalvik is optimized for a wide variety of device configurations – 32 MB to 32 GB RAM.
  • Unlike JVM, all classes in Dalvik are compiled into .dex files and packaged into an apk along with res and manifest files. The dex file itself is not compressed though.
  • Java’s JVM is stack-based whereas Dalvik is register-based. The main upside of being a register-based system is lesser instructions and lesser dispatches to accomplish the same task – no push-pop involved and memory footprint is lesser. See slides for an example of instruction optimization. The downside is that complexity of each instruction increases. Java’s JVM is very efficient for desktop machines.
  • Dalvik unifies the constants pool (i.e.) lots of symbols (~estimated at ~60%) are duplicated across classes – only 1 copy is stored across these instances.
  • Dalviks JIT is trace based as opposed to JVM’s method based. Once it figures out a hotspot, it converts it to native and runs the native version from then on.
  • Zygote is the father of all VM’s. When an app is started, the Zygote is forked, to minimize initial load time. The app’s VM  contains a reference to Zygote (with its core libraries) plus the app’s heap + dex files.
  • Development tip #1: Prefer signalling (Notifications) vs Polling.
  • Dev tip #2: Minimize work when there is no user, network or sensor input.
  • Dev tip #3: Monitor the state of the battery. The Android dev site has good tips.
  • Dev tip #4: While looping through Array lists, pre-compute the list size (vs.) using Java’s standard Iterator mechanism. This avoids calling ‘next’ ob every loop.
  • Dev tip #5: Keep garbage collectons low. Avoid short-lived allocations, excessive boxing-autoboxing, pass lists as an argument to a method (instead of returning a list).
  • Dev tip #6: Recycle views and bitmaps.
  • More optimization tips are found here.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s