Understanding Android-III (Android Runtime)
Android runtime (ART) is the managed runtime used by applications and some system services on Android. ART executes the Dalvik Executable format and Dex bytecode specification. ART and Dalvik are compatible runtimes running Dex bytecode. ART is effective and takes less time to open and run apps.
How does it work?
When an app is built APK is generated, part of that APK are .dex files. Those files contain the source code(java class files) of the app including all libraries that are used in low-level code designed for a software interpreter — the bytecode.
When a user runs the app the bytecode written in .dex files is translated by Android Runtime into the machine code — a set of instructions that can be directly understood by the machine and is processed by the CPU.
Dalvik Virtual Machine up to Android K
In the early days of Android smartphones were not so powerful as now. Most phones at a time have very little RAM, some of them even as little as 200MB.
The first Android Runtime, known as Dalvik Virtual Machine(DVM), was implemented to optimize exactly this one parameter: RAM usage.
So instead of compiling the whole app to machine code before running it, it used the strategy called Just In Time compilation, JIT in short.
In this strategy, the compiler works a little bit as an interpreter. It compiles small chunks of code during the execution of the app — at runtime. And because Dalvik only compiles the code that it needs and does it at runtime it allows saving a lot of RAM.
But this strategy has one serious drawback — because this all happens at runtime it obviously has a negative impact on runtime performance. Eventually, some optimizations were introduced to make Dalvik more performant. Some of the frequently used compiled pieces of code were cached and not recompiled again.
But this was very limited because of how scarce RAM was in those early days.
That worked fine for a couple of years but meanwhile, phones became more and more performant and got more and more RAM. And since also apps were getting bigger the JIT performance impact was becoming more of a problem.
ART in Android L
The way ART worked in Android L was a 180-degree change from what we knew from Dalvik. ART instead of using Just in Time compilation like it was done in Dalvik used a strategy called Ahead of Time compilation.
In ART instead of interpreting code at runtime code was compiled before running the app and when the app was running, machine code was already prepared. This approach hugely improved runtime performance since running native, machine code is even 20 times faster than just in time compilation.
Ahead-of-time (AOT) compilation
ART introduces ahead-of-time (AOT) compilation, which can improve app performance. ART also has tighter install-time verification than Dalvik.
At install time, ART compiles apps using the on-device dex2oat tool. This utility accepts DEX files as input and generates a compiled app executable for the target device. The utility should be able to compile all valid DEX files without difficulty. However, some post-processing tools produce invalid files that may be tolerated by Dalvik but cannot be compiled by ART.
Improved garbage collection
Garbage collection (GC) is very resource-intensive, which can impair an app’s performance, resulting in a choppy display, poor UI responsiveness, lag in games, and other problems.
Android Runtime also has better memory management, app developing, and debugging.
ART in Android L used a lot more RAM than Dalvik.
Another drawback is that it took more time to install the app since after downloading the APK the whole app needed to be transformed to the machine code and it also took longer to perform a system update because all apps need to be reoptimized.
For frequently run parts of the app it obviously pays off to have it precompiled but the reality is most parts of the app are opened by the users very very rarely and it almost never pays off to have the whole app precompiled.
And that’s why in Android N, Just In Time compilation was introduced back to Android Runtime along with something called profile-guided compilation.