Android [4.4] KitKat users must have noticed the new option to choose the default runtime environment in Android (Dalvik or ART). Dalvik has been the runtime environment in Android since the very beginning. Although ART was added as an experimental feature in KitKat, it has now replaced Dalvik from Android [5.0] Lollipop onwards.
What is a runtime environment?
When a software program is executed, it is in a runtime state, during which the program sends instructions to the computers processor to access the systems resources. To do this, we have a runtime environment that executes software instructions when a program is running. These instructions translate the software code into machine code (byte code) that the computer is capable of understanding. In simpler terms, it means that all Android application files (APKs) are basically uncompiled instructions.
Why does Android use a virtual machine?
Android uses a virtual machine as its runtime environment to compile and run its applications. Unlike our Virtual Box, this virtual machine does not emulate the entire computer! Using a virtual machine ensures that the application execution is isolated from the core operating system, so even if the application contains some malicious code, it cannot directly affect the system. This provides stability and reliability for the operating system. This also provides more compatibilitysince different processors use different instruction sets and architectures, compilation on the device ensures compatibility with the specific device.
How does Dalvik work? Why is it being replaced?
Dalvik uses a JIT (Just-In-Time) compiler for its virtual machine. Applications need a lot of resources to run. Taking up a lot of resources can slow down the system. With JIT, the resources are fetched just when they are needed, which means the applications get compiled when they are launched and are loaded into RAM. But compiling the entire code when we launch an application takes a lot of time, which translates into what we call lag. However, in this case, the entire code is not compiled on every application launch; rather, the part of the code that is needed to run the application is compiled every time and gets stored in a cache called the Dalvik-cache so that it can be reused. This cache gets optimised with every compilation overtime, and creates something like a tree of dependencies on every device.
Advanced Android users should know that they have to wipe the Dalvik-cache before they install a new ROM in their Android, because this tree of dependency now has to be reconstructed for the new system in the ROM.
But if the compiled application that is loaded into RAM is manually killed, the whole process of compilation has to be done again. Over time, the Dalvik-cache gets bigger and does not get cleaned; this takes up a lot of storage, which slows down the device.
How ART solves this
ART uses an AOT (Ahead-Of-Time) compiler. When an application is installed, the AOT compiler translates the entire code into machine code via compilation. So this means the application doesnt need to be compiled repeatedly. This makes the process of launching and using the application faster and smoother, because the precompiled code (machine code) just needs to be executed and all the resources are readily available. Reducing the number of compilations also improves the battery life of the device. But compiling the entire code means installing the application will take more time and so will storage.
How to enable ART in KitKat devices
Go to Settings > About phone. Keep tapping Build number until a toast message pops up.
Now go back to Settings and a new entry Developer options can be found; turn it on, choose Select runtime and then select ART.
The device needs to reboot, and might take a couple of minutes based on the number of applications you have installed because they need to get optimised for ART.