ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Android Runtime — How Dalvik and ART work?

Paulina Sadowska
ProAndroidDev
Published in
7 min readApr 14, 2021

--

This post was created alongside the video that I posted on my YouTube channel 🎥

Android Runtime is one of the core building blocks in the Android ecosystem. Most of you probably heard the terms: and If you ever wondered and to make our apps as fast as possible —ou’ll learn all that in this article 🚀.

So let's get into it!

What is Android Runtime?

When we build our app and generate APK, part of that APK are . Those files contain the source code of our app including all libraries that we used in low-level code designed for a software interpreter — .

When a user runs our app the bytecode written in — a set of instructions, that can be directly understood by the machine and is processed by the CPU.

Android Runtime also but, to not make this article too long .

The compilation of the bytecode into machine code can be done using various strategies and all those strategies have their tradeoffs. And to understand how Android Runtime works today we have to move back in time a couple of years and first learn about Dalvik.

Dalvik (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.
No wonder the first Android Runtime, known as Dalvik, .

So instead of compiling the whole app to machine code before running it, it used the strategy called , 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 — .

And because Dalvik only compiles the code that it needs and does it at runtime it allows .

But this strategy has one serious drawback — because this all happens at runtime it obviously has a .

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.

That’s why in there was a new Android Runtime introduced: ART.

ART (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 .

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.

Drawbacks:

Another drawback is that since after downloading the APK the whole app needed to be transformed to the machine code and 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

And that’s why in Just In Time compilation was introduced back to Android Runtime along with something called .

Profile-guided compilation (Android N)

Profile guided compilation is a strategy that allows to constantly improve the performance of Android apps as they run. By default, the app is compiled using the strategy, but when ART detects that some methods are “hot” which means that they run frequently, ART can

This strategy allows providing the best possible performance for key parts of the app while reducing the RAM usage. Since as it turns out for most apps only 10% to 20% of code is frequently used.

After this change ART, no more impacted the speed of app installs and system updates. Precompilation of key parts of an app happened only while the device was idle and charging to minimize the impact on the device battery.

The only drawback of this approach is that in order to get profile data and precompile frequently used methods and classes That means because in that case only Just in Time compilation is going to be used.

And that’s why to improve this initial user experience in Google introduced .

Profiles in the cloud (Android P)

The main idea behind profiles in the cloud is that So in order to improve performance right after installation, we can collect profile data from people who already used this app. This aggregated profile data is used to create a file called a

So when a new user installs the app this file is downloaded alongside the application. ART uses it to precompile classes and methods that are frequently run by most of the users. That way,

After the user runs an app, ART will gather user-specific profile data and recompile code that is frequently used by this particular user when the device is idle.

And the best part of all this is that we, developers who are making apps don’t have to do anything to enable this feature.

Summary

Android Runtime is responsible for compiling bytecode which is a part of an APK into device-specific machine code which can be understood directly by the CPU.

The first Android Runtime implementation was called which used compilation to optimize usage of RAM, which was very scarce at the time.

In order to improve performance in was introduced which used That allowed achieving better runtime performance but caused longer installation time and more RAM usage.

That’s why in was introduced back into ART and allowed to achieve better performance for the parts of the code that are frequently run.

To allow users to get the best performance possible right after an app is installed in Google introduced which complements previous optimizations by adding the which is downloaded with APK and allows ART to precompile parts of the code which are most frequently run by previous app users.

All these optimizations allow Android Runtime to make our apps as performant as possible 🚀.

--

--

Responses (9)

Write a response