Throughout the rapid and tumultuous history of Android, which is now five years old, almost every aspect of the OS has been changed, updated, or overhauled in some way. Everything, that is, except for the most important part: Dalvik, the virtual machine that runs almost every Android app, has remained virtually the same since day one – and Dalvik is slow.
Now, with Android 4.4 KitKat, Google has revealed that a Dalvik replacement is in the works – a replacement, called Android Runtime (ART), which should improve the performance of Android apps by a huge margin. The early version of ART in Android 4.4 already speeds up apps by around 100 per cent, and the final version should be even better.
When you run software on a computer, such as a PC or smartphone, you are nearly always running compiled code. Compiled code is source code that has been compiled, by the developer, into code that the computer can understand (machine code). If you open an EXE file, you see compiled code – gobbledegook machine code that your CPU can execute. Windows, your web browser, Crysis 3, Linux, iOS apps – these are all examples of compiled code.
The other prime example of interpreted code is Java – and Dalvik is essentially Google’s version of Java. Java is desirable because a developer can write a program once, and then have it executed on any hardware platform that has a working interpreter (the Java Virtual Machine). For the same reason, because Android was designed to run on a huge range of platforms, hardware specs, and form factors, Google decided to use the Dalvik virtual machine for Android apps. This way a developer can write a single Dalvik app and be assured that it will run on smartphones, tablets, TVs, embedded devices, and so on.
Java/Dalvik isn’t quite that slow, but it’s still significantly slower than natively compiled code. In many cases, especially with modern processors, this speed difference isn’t glaringly obvious, but it all adds up. It’s impossible to lay the blame for Android’s slower responsiveness or higher power consumption solely on Dalvik – but it’s definitely a significant factor. It’s a simple equation: Interpreted code takes longer to execute and consumes more CPU time, thus reducing battery life and overall responsiveness.
Android Runtime – ART
Google knows all this about Dalvik, of course, which is why it’s been working on its replacement – Android Runtime (ART) – for more than two years. An early version of ART is included with Android 4.4 and can be enabled in: Settings > Developer Options > Select Runtime.
ART straddles an interesting mid-ground between compiled and interpreted code, called ahead-of-time (AOT) compilation. Currently, Android apps are interpreted at runtime (using the JIT) every time you open them up. This is slow (iOS apps, in comparison, are compiled native code, which is much faster). With ART enabled, each Android app is compiled to native code when you install it. Then, when it’s time to run the app, it performs with all the alacrity of a native app.
It obviously takes some time to perform the AOT compilation at install time, but the long-term gains from apps that load and run faster will easily make up for this inconvenience. You probably won’t even notice the AOT compilation of small apps; but for larger apps, we’re talking about a noticeable delay. If you switch an existing device from Dalvik to ART, you’re talking about a wait time of a few minutes while Android performs AOT for all your installed apps.
The main advantage of ART is that it allows Android developers to continue writing the exact same code, and having their apps work across a wide range of hardware specs and form factors – but now their apps will run significantly faster, feel more responsive, and your device’s battery life should improve.
Early testing indicates that ART is twice as fast as Dalvik. ART’s compiled code should also perform more consistently than JIT Dalvik, too, reducing UI latency and stuttering. Computationally intensive apps, such as photo and video editors, will probably see the most impressive gains, but if this early build of ART is anything to go by, there should be significant improvements across the board. ART could be the change that finally makes Android as responsive and snappy feeling as iOS.
There’s no timeline for the official introduction of ART, but given that Google has been working on it for a couple of years, and that the implementation in Android 4.4 feels quite mature, it’s probably not that far away. It will probably be a headline feature in Android 4.5 – or more likely, given the scale of the change, Android 5.0.
For more on KitKat, check out 11 smart new features Google is introducing with Android 4.4.