Android Runtime: ART & Execution Model
Android Runtime: ART & Execution Model
1. Execution Flow
flowchart TD
A["Kotlin / Java Code"] --> B["Compiler"]
B --> C["JVM Bytecode (.class)"]
C --> D["D8 / R8"]
D --> E["DEX Bytecode (.dex)"]
E --> F["APK"]
F --> G["ART"]
G --> H["Interpretation + JIT + AOT"]
H --> I["Native Machine Code"]
I --> J["CPU Execution"]---
2. APK Bytecode
An APK (Android Package Kit) is the installable package of an Android application.
It generally contains:
* classes.dex – compiled application bytecode
* AndroidManifest.xml – app and component information
* resources.arsc – compiled resources
* res/ – layouts, images and other resources
* lib/ – native .so libraries
* assets/ – raw application files
Compilation process
Kotlin/Java source
↓
.class bytecode
↓
D8 or R8 compiler
↓
classes.dex
↓
Packaged inside APKImportant point: Android does not normally execute JVM `.class` files directly. They are converted into DEX bytecode.---
3. DEX
DEX means Dalvik Executable Format.
It is a compact bytecode format designed for Android devices.
Why Android uses DEX
* Optimized for mobile devices
* Reduces duplicated information
* Requires less storage and memory
* Combines multiple .class files
* Allows ART to load and execute application code
Multiple DEX files
A DEX file can reference a limited number of methods. Large applications may contain:
classes.dex
classes2.dex
classes3.dexThis is called MultiDex.
---
4. ART
ART means Android Runtime. It is the managed runtime used to execute Android applications.
ART replaced Dalvik as the default runtime starting with Android 5.0.
Main responsibilities
* Load DEX bytecode
* Load application classes
* Execute methods
* Compile frequently used code
* Manage application memory
* Perform garbage collection
* Handle threads and exceptions
Each Android application normally runs inside its own process with its own ART runtime environment.
---
5. Interpretation
In interpretation, ART reads and executes DEX instructions one by one.
DEX instruction → Interpreter → CPU operationAdvantages
* Code can start without waiting for compilation
* Useful for methods that are rarely executed
* Reduces unnecessary compilation
Disadvantages
* Slower than native machine code
* Instructions must be translated repeatedly
ART may initially interpret a method and later compile it if the method is used frequently.
---
6. JIT Compilation
JIT means Just-In-Time compilation.
JIT compiles frequently executed methods into native machine code while the application is running.
Working
flowchart LR
A["Method called"] --> B["Interpreter"]
B --> C{"Frequently used?"}
C -->|No| B
C -->|Yes| D["JIT compilation"]
D --> E["Native code"]
E --> F["Faster execution"]ART records frequently used methods in a runtime profile. These methods are often called hot methods.
Advantages
* Compiles only frequently used code
* Improves runtime speed
* Uses less installation time than compiling the complete app
Limitations
* Compilation uses some CPU and memory
* The first few method executions may be slower
---
7. AOT Compilation
AOT means Ahead-Of-Time compilation.
AOT compiles DEX bytecode into native machine code before the method needs to execute.
It can happen during:
* Application installation
* Device idle time
* Charging
* Background dex optimization
Advantages
* Faster method execution
* Less runtime compilation
* Better performance for commonly used code
Limitations
* Compiled code takes additional storage
* Compiling the complete application can increase installation time
Modern ART combines interpretation, JIT and profile-guided AOT instead of relying on only one approach.
---
8. Interpretation vs JIT vs AOT
| Feature | Interpretation | JIT | AOT |
|---|---|---|---|
| When code is translated | During every execution | During runtime | Before execution |
| Initial startup | Quick | Usually quick | Fast after compilation |
| Method performance | Slower | Fast after warm-up | Fast |
| Storage requirement | Low | Medium | Higher |
| Main use | Rarely used code | Frequently used code | Important/profiled code |
---
9. Class Loading
Class loading means finding a class definition and making it available to ART.Basic steps
1. The app requests a class.
2. The class loader checks whether it is already loaded.
3. If not, it finds the class in DEX files.
4. ART verifies the class and its bytecode.
5. Memory is prepared for static fields.
6. The class is initialized.
7. Its methods become available for execution.
Common class loaders
* BootClassLoader – loads core Android framework classes
* PathClassLoader – loads classes from the installed APK
* DexClassLoader – can load code from external DEX or APK files
Classes are usually loaded only when required. Loading too many classes during startup can slow down the application.---
10. Method Execution
When an Android method is called:
1. ART confirms that the class is loaded.
2. ART finds the method implementation.
3. It creates or updates the method’s stack frame.
4. Parameters and local variables are placed in the frame.
5. ART selects available execution code:
* Interpreter
* JIT-compiled code
* AOT-compiled code
6. The CPU executes the instructions.
7. The method returns its result.
8. Its stack frame is removed.
Example
fun add(a: Int, b: Int): Int {
return a + b
}This function is converted into DEX instructions. ART may interpret it initially. If it is called repeatedly, JIT or AOT compilation can convert it into native machine code.
---
11. DEX Optimization
DEX optimization improves how application bytecode is loaded and executed.
ART can perform optimization:
* During installation
* After application usage profiles are collected
* While the device is idle and charging
* After an application or Android system update
Profile-guided optimization
App runs
↓
ART identifies frequently used code
↓
Runtime profile is generated
↓
Important code is compiled using AOT
↓
Future execution becomes fasterThis approach avoids compiling every method and focuses on the code that users actually execute.
Android may also use baseline profiles supplied with the app. These profiles identify important startup and navigation paths in advance.
---
12. Runtime Performance
ART performance depends on several factors.
Application startup
Startup becomes slower when the app performs:
* Excessive class loading
* Heavy object creation
* Large static initialization
* Too much work in Application.onCreate()
* Synchronous disk or network operations
Runtime execution
Frequently executed methods benefit from JIT and AOT compilation. However, unnecessary repeated calculations and excessive method calls can still affect performance.
DEX and application size
Large applications may contain more classes and DEX files, which can increase:
* Class-loading work
* APK size
* Startup time
* Memory usage
R8 can reduce this by removing unused code and optimizing bytecode.
Better runtime performance practices
* Keep Application.onCreate() lightweight
* Use lazy initialization
* Avoid unnecessary object creation
* Move expensive work away from the main thread
* Remove unused code using R8
* Provide baseline profiles for critical user journeys
* Measure performance instead of assuming the cause
---
13. Complete Execution Summary
flowchart TD
A["App installed"] --> B["APK and DEX extracted"]
B --> C["ART loads required classes"]
C --> D["Methods initially interpreted"]
D --> E["ART records hot methods"]
E --> F["JIT compiles hot methods"]
F --> G["Runtime profile saved"]
G --> H["Profile-guided AOT optimization"]
H --> I["Faster future startup and execution"]Common Interview Mistakes
* Saying APK directly contains Java source code
* Saying Android executes .class files directly
* Saying ART uses only AOT compilation
* Treating JIT and interpretation as the same thing
* Saying every class is loaded when the app starts
* Assuming the complete app is always compiled during installation
* Confusing DEX bytecode with native machine code
Important Interview Questions
1. What is ART?
ART is Android’s managed runtime. It loads DEX code and executes it through interpretation, JIT compilation and AOT compilation.
2. What is DEX?
DEX is Android’s compact executable bytecode format. Java or Kotlin .class bytecode is converted into DEX before being packaged into the APK.
3. Does ART use JIT or AOT?
Modern ART uses both, along with interpretation. JIT optimizes hot code during runtime, while profile-guided AOT prepares important code for future execution.
4. What is a hot method?
A hot method is a method that is executed frequently. ART may compile it with JIT and record it in the app’s runtime profile.
5. What happens when a method is called?
ART loads its class if necessary, finds the method, prepares its stack frame and executes it using interpreted, JIT-compiled or AOT-compiled code.
6. Why doesn’t Android compile every method using AOT?
Compiling everything would increase installation time and storage usage. Profile-guided optimization focuses on frequently used code.
7. What is DEX optimization?
It is the process of verifying, compiling and optimizing DEX code to improve application startup and runtime performance.
8. How does class loading affect startup?
Loading and initializing many classes during startup consumes CPU and memory. Heavy static initialization can delay the first screen.
9. What is the role of R8?
R8 removes unused code, shrinks the application and optimizes bytecode before it is converted into the final DEX files.
10. Explain ART execution in one line.
> ART loads DEX bytecode and uses interpretation, JIT and profile-guided AOT compilation to execute Android applications efficiently.