Friday, December 20, 2013

ART or Dalvik? Which one is better?

ART (Android RunTime) is the next version of Dalvik. Dalvik is the runtime, bytecode, and VM used by the Android system for running Android applications.

ART has two main features compared to Dalvik:
  1. Ahead-of-Time (AOT) compilation, which improves speed (particularly start-up time) and reduces memory footprint (no JIT).
  2. Improved Garbage Collection (GC).
An ahead-of-time (AOT) compiler is a compiler that implements ahead-of-time compilation. This refers to the act of compiling an intermediate language, such as Java bytecode, into a system-dependent binary. Hence your apps are compiled to native code once, and stored on your device to run effectively as native. This differs from the traditional VM model, which interprets bytecode. Interpreters are slow, so VM developers added a technology called Just-in-Time (JIT) compilation, which compiles (and hopefully optimizes) your code to native code on-the-fly. Dalvik is a JIT'ing VM. The downside to JIT is that the JIT compiler runs while you are using your app, adding latency and memory pressure. The upside is that the JIT compiler can perform some optimizations possible, like runtime profile-guided optimizations, pseudo-constant propagation or indirect/virtual function inlining.

AOT is like JIT, but it runs once—say, at app installation time. AOT in most cases produces machine optimized code, just like a 'standard' native compiler. Beside it lacks the ability to perform profile-directed optimizations, AOT transforms the bytecode of an existing virtual machine into machine code. AOT compilers can perform complex and advanced code optimizations which in most cases of JITing will be considered much too costly.  In most situations with fully AOT compiled programs and libraries it is possible to drop considerable fraction of runtime environment, thus saving disk space, memory and starting time. Because of this it can be useful in embedded or mobile devices. I think AOT is the right step for Android and ART looks quite impressive.

ART was first included in Android KitKat, but isn't yet enabled by default. You can enable it via Settings > Developer options > Select runtime > Use ART.

Note: There is a possibility that some of the Android applications may not work with ART, because it is an entirely new VM. It may has bugs or might be some areas of the core APIs are not compatible. This is why it is not yet the default VM in Android.