Android OS

Android is an open source, Linux-based software stack created for a wide array of devices and form factors. For example, the Android Runtime (ART) relies on the Linux kernel for underlying functionalities: networking, multi-threading and low-level memory management,..

Why Android use virtual machine?

There are many reason that Google engineers decide to use Android with VM, but two main reason is:

Security : In theory, app code is totally isolated by the VM and cannot even “see” the host OS. So app code that contains malware cannot affect system directly, make app and system more robust and reliable.

: In theory, app code is totally isolated by the VM and cannot even “see” the host OS. So app code that contains malware cannot affect system directly, make app and system more robust and reliable. Platform independent: Android platform can run on different devices with different architectures (ARM, MIPs, x86). To abstract out the need to compile binaries for each architecture, VM comes into play.

Dalvik Virtual Machine

Dalvik Virtual Machine (DVM) is a Process virtual machine for Android.

DVM is a virtual machine that optimized for mobile environment (memory, battery life, performance,..)

Dan Bornstein named it after the Dalvík village in Iceland. Source: Android Developer Blog.

Dalvik EXecutable

Dalvik EXecutable ( .dex ) is Dalvik bytecode which translated from Java bytecode using dx tool (replaced by D8 tool since API 28). This format was designed for systems that have constrained memory and processor speed.

The process of generate new APK

Multiple classes are included in a single .dex file:

You can use APK Analyzer to see what’s inside a DEX file. Source: Android Developers.

Compare with Java VM

The DVM was built specifically for Android, to address the battery life, processing speed. Moreover, the Java tools are free but the JVM is not, so Google engineers made their own VM and made it as free.

JVM vs Dalvik VM

Unlike JVM, which are simple stack machines, the DVM uses a register-based — which requires fewer instructions, fewer code units, avoid unnecessary memory access — resulting in better performance code.

JVM and Dalvik VM flows

Android Runtime

Android Runtime (ART) is the managed runtime used by apps and system services on Android. Replacing the predecessor Dalvik, ART performs the translation of the app’s bytecode into native instructions that are later executed by the device’s runtime environment.

The ART is written to run multiple VMs on low-memory devices. To maintain backward compatibility, ART also uses the same input bytecode as Dalvik — the standard Dalvik EXecutable ( .dex ) files — which also designed for Android to minimize memory footprint.

Android Runtime (ART).

From Android 5.0, each app runs in its own process, with its own instance of ART. But prior to this, it use Dalvik. If your app runs well on ART, then it should work on Dalvik as well, but the reverse may not true.

Just-In-Time vs Ahead-Of-Time

Just-In-Time (JIT) added in Android 2.2. It dynamically compiles bytecode into native machine code at run-time to make app runs faster. It does all this while app is running, and that’s where the “Just-In-Time” comes from.

Ahead-Of-Time (AOT) added in Android 4.4. It statically compiles bytecode into machine code at install-time — using on-device dex2oat tool — and stores data in the device’s storage, to improve run-time performance.

JIT vs AOT

Compare with Dalvik VM

Dalvik is a JIT compilation based engine.

The ART includes a JIT compiler (with code profiling). The JIT compiler complements new AOT compiler and improves runtime performance, speed up apps and system updates.

ART vs Dalvik VM

Furthermore, ART has many more advantage:

Optimized garbage collector : one GC pause instead of two.

: one GC pause instead of two. Loop optimizations: Bounds check, Induction variable is eliminated.

Faster native calls using @FastNative and @CriticalNative annotations.

and annotations. Improve battery life.

Reduce startup time as native code is directly executed.

Faster runtime performance because AOT compile at install-time.

From API 28, convert APK’s DEX files to more compact machine code.

Better debugging support (dedicated sampling profiler, detailed exceptions reporting, and ability to set watchpoints to specific fields).

Dalvik VM vs ART flows

Optimized Garbage Collector

Garbage Collector (GC) can impact app’s performance with “stop-the-world” events, which resulting in frozen frames, poor UI responsiveness. The default GC plan is the CMS (concurrent mark sweep).

The ART improves GC in several ways: