Android Runtime Environment explained

1132

6

2016-02-19 01:18

Edited by Pulak at 2016-02-18 22:48

Before getting into the topic, first let us understand the basic.


What Is A Runtime Environment?


To put in simplest terms, runtime comprises of software instructions that execute when your program is running. These instructions basically translate the software's own code into the code that the computer is capable of running. Therefore, all computer languages require some sort of runtime environment that can properly execute the code written in the programming language.


Android makes use of a virtual machine as its runtime environment in order to run the APK files that constitute an Android application. The advantage of using a virtual machine is twofold – firstly, the app code is isolated from the core operating system, ensuring that should something go wrong, it’s contained in an isolated environment and does not effect the primary OS. And secondly, it allows for cross-compatibility, meaning even if an app is compiled on another platform (such as a PC, as is usually the case with developing mobile apps) , they can still be executed on the mobile platform using the virtual machine.


Runtime Environment in Android


An android device uses any one of the following runtimes -

  • Dalvik
  • ART


ART (Android RunTime) has been introduced from Android KitKat as an option. Prior to that dalvik was the only runtime. From Android L ART has been promoted as the primary and dalvik as an extra option. We can consider ART/dalvik as mobile optimized version of JVM (Java Runtime Machine).


Why ART is preferred over dalvik?


Android apps comes in .apk format. Application code are essentially written in Java (.javaclasses) which are bundled in apk files. While application gets bundled, java classes are compiled and converted to bytecodes (.dex) files. This is common for both Dalvik as well as ART. Difference is what comes next.


Dalvik uses JIT (Just In Time) technology, where every time an application is launched byte code is interpreted line by line. If a routine or function is identified as hotspot it is directly compiled to native code (machine code) by JIT with optimization. This leads to time and memory footprint overhead as this is done on the fly when application is running. The JIT compiled code is then run directly everytime thereafter (no interpretation needed) which increases the performance.


ART uses AOT (Ahead Of Time) trchnology, where when an application (.apk) file is installed entire byte code is converted to machine code and stored in the persistent storage. As this is on installation this happens only once. Every time the application is launched this machine code is directly executed. No need of interpreter. Yes it takes more storage space as compiled native code is stored in the storage but takes less CPU and less RAM (memory footprint).

Following are the basic differences between ART and Dalvik

DalvikART
Uses Just-In-Time (JIT) approach, which results in lower storage space consumption but longer app load timesUses Ahead-Of-Time (AOT) approach, which compiles apps when they’re installed, resulting in faster load times and lower processor usage.
Cache builds up over time, so boot times are fasterCache is built at first boot, hence rebooting device takes significantly longer
Works better for lower internal storage devices as space occupied is lesserConsumes much more internal storage space since it stores compiled apps in addition to the APKs