Before moving directly to ART (Android Runtime), we need to understand what the runtime environment is and also understand some basic things like the functioning of JVM and Dalvik VM.
What is the runtime?
In a simpler term, it is a system used by the operating system that is responsible for converting the code you write in a high-level language such as Java to machine code and understands it by CPU / Processor
The runtime consists of software instructions that run when your program is running, even if they are not essentially part of the code for that particular piece of software.
CPU or a more general term, our computers only understand the machine language (binary codes). So, in order for it to run on the CPU, the code must be converted to the machine code which is performed by a translator
Next, the generation of the translator in a sequence is shown.
1. Assemblers: It directly translates the assembly codes to machine codes, so it is very fast.
2. Compilers: Translate the code into assembly codes and then use assemblers to translate the code into binary. The use of this compilation is slow but the execution is fast. But the biggest problem with the compiler is that the resulting machine code is dependent on the platform. In other words, the code that runs on a machine may not run on a different machine.
3. Interpreters: Translate the code while executing it. As the translation occurs at runtime, the execution is slow.
How does the execution of the JAVA code work?
To maintain the independence of the code platform, JAVA developed JVM, i.e. Java Virtual Machine. It developed JVM specific to each platform which means that JVM depends on the platform. The Java compiler converts the .java files into .class files, which is called bytecode. This byte code is delivered to the JVM which converts it into machine code.
This is slower than C ++ compilation but faster than interpretation.
How does Android code execution work?
On Android, Java classes become a DEX bytecode. The DEX bytecode format is translated into native machine code through ART or Dalvik runtimes. Here, the DEX bytecode is independent of the device architecture.
Dalvik is an engine based on JIT (Just in time) compilation. There were inconveniences to use Dalvik, therefore, since Android 4.4 (KitKat) ART was introduced as runtime and since Android 5.0 (Lollipop) it has completely replaced Dalvik. Android 7.0 adds a Just-In-Time (JIT) compiler with code profiles for Android Runtime (ART) that constantly improves the performance of Android applications as they run.
Key point: Dalvik used the JIT (Just in time) compilation while ART uses the AOT (Ahead of time) compilation.
Below is the code snippet that explains the difference between Dalvik Virtual Machine and Java Virtual Machine.
Just in time (JIT)
With the Dalvik JIT compiler, every time the application is run, it dynamically translates a part of the Dalvik bytecode into machine code. As execution progresses, more bytecode is compiled and cached. Since JIT compiles only part of the code, it has a smaller memory footprint and uses less physical space on the device
Ahead of Time (AOT)
ART is equipped with an Ahead-of-Time compiler. During the app installation phase, DEX bytecode is statically converted to machine code and stored in the device storage. This is a one-time event that occurs when an app is installed on a device. Because there is no need for JIT compilation, code execution is much faster.
Since ART executes the application machine code directly (native execution), it does not hit the CPU as much as compiling a just-in-time code with Dalvik. Battery consumption is reduced due to low CPU usage.
ART also uses the same DEX bytecode as input for Dalvik. An application compiled using ART requires additional time for compilation when an application is installed and occupies a slightly larger amount of space to store the compiled code.
Why does Android use the virtual machine?
Android uses a virtual machine as its runtime environment to run the APK files that constitute an Android application. The advantages are presented below:
- The application code is isolated from the central operating system. So even if some code contains some malicious code, it will not directly affect the system files. It makes the Android operating system more stable and reliable.
- It provides cross-compatibility or platform independence. It means that even if an application is compiled on a platform such as a PC, it can still be run on the mobile platform using the virtual machine.
ART benefits
- Applications run faster since DEX bytecode translation is done during installation.
- Reduces application startup time since native code is executed directly
- Improves battery performance because it saves energy used to interpret byte codes line by line.
- Improved garbage collector.
- Enhanced developer tool.
Drawbacks of ART
- The installation of the application takes longer due to the conversion of DEX bytecode into machine code during installation.
- As the native machine code generated in the installation is stored in the internal storage, more internal storage is required.
For more details about ART and Dalvik, you can visit the Android training in Chandigarh.