Mobile ProgrammingUnit 1110 min read
DVM vs. ART, Surface Manager: Android’s Runtime & Display Engine
Unit 11 of Mobile Programming dives into Android’s advanced runtime (DVM vs. ART) and the Surface Manager library, explaining how apps execute efficiently and how Android handles UI rendering, animations, and hardware acceleration—critical for performance optimization and modern app development.
TAKEAWAYS:
- DVM (Dalvik Virtual Machine) was Android’s original runtime, using AOT compilation for
.dexfiles and JIT for dynamic optimization, but was replaced by ART for better performance. - ART (Android Runtime) uses AOT compilation at install time and profile-guided optimization (PGO) to reduce startup time and improve battery life, making it the default in modern Android.
- Surface Manager is a core Android library that coordinates UI rendering across multiple layers (e.g.,
SurfaceFlinger,Surface,HardwareBuffer) to enable smooth animations, multitasking, and hardware-accelerated graphics. - Hardware acceleration offloads rendering to the GPU via
SurfaceFlinger, reducing CPU load but requiring careful management to avoid memory leaks or stuttering. - AOT vs. JIT: ART’s AOT compiles code to native machine code upfront, while DVM relied on JIT during runtime, leading to slower initial execution in DVM.
- Surface layers: The stack includes
Application,ViewRoot,Surface, andHardwareBuffer, withSurfaceFlingermerging them into a single display output.
1. Android Runtime: DVM vs. ART
Android’s runtime determines how apps execute. Let’s compare the two systems side by side.
1.1 Dalvik Virtual Machine (DVM)
DVM was Android’s original runtime (pre-Android 5.0 Lollipop). It used .dex files (Dalvik Executable) and had two key components:
- AOT (Ahead-of-Time) compilation:
.dexfiles were compiled to bytecode and optimized before execution. - JIT (Just-In-Time) compilation: Dynamically compiled frequently used code to native machine code at runtime for performance gains.
How DVM Worked (Simplified):
- APK →
.dex: The.dexfile was extracted from the APK during installation. - DVM Loads
.dex: The VM loaded and executed the bytecode. - JIT Optimization: Frequently used methods were compiled to native code for faster execution.
Limitations of DVM:
- Slower startup time: JIT compilation happened during runtime, delaying app launch.
- Higher CPU usage: Dynamic compilation consumed more battery and processing power.
- No profile-guided optimization (PGO): Couldn’t optimize based on real-world usage patterns.
Visual: DVM Execution Flow
graph TD
A["APK"] --> B[".dex File"]
B --> C["DVM Loads .dex"]
C --> D["JIT Compilation (Runtime)"]
D --> E["Native Execution (Slower Startup)"]
C --> F["JIT Cache (Optional Optimization)"]
F --> E1.2 Android Runtime (ART)
ART replaced DVM starting with Android 5.0 (Lollipop). It introduced AOT compilation at install time and profile-guided optimization (PGO) for better performance.
Key Features of ART:
- AOT Compilation: The
.dexfile is compiled to native machine code during installation (or when the app is first run). - Profile-Guided Optimization (PGO): ART analyzes how the app is used (e.g., which methods are called most frequently) and optimizes those paths.
- Faster Startup: No runtime JIT compilation means apps launch quicker.
- Better Battery Life: Less dynamic compilation reduces CPU usage.
How ART Works (Simplified):
- APK →
.dex→ Native Code: The.dexfile is compiled to native machine code before the app runs. - PGO Optimization: ART uses usage data to optimize hot paths (frequently executed code).
- Instant Execution: The app runs natively without JIT delays.
Visual: ART vs. DVM Compilation
graph TD
subgraph ART
A["APK"] --> B[".dex File"]
B --> C["AOT Compiles to Native Code at Install"]
C --> D["PGO Optimizes Hot Paths"]
D --> E["Instant Native Execution"]
end
subgraph DVM
F["APK"] --> G[".dex File"]
G --> H["DVM Loads .dex"]
H --> I["JIT Compiles at Runtime"]
I --> J["Native Execution"]
endComparison Table: DVM vs. ART
| Feature | DVM | ART |
|---|---|---|
| Compilation | JIT at runtime | AOT at install time |
| Startup Time | Slower (JIT delay) | Faster (native code ready) |
| Battery Efficiency | Higher CPU usage | Lower CPU usage |
| Optimization | None (static analysis) | Profile-Guided Optimization (PGO) |
| Native Execution | After JIT compilation | Immediately after install |
2. Surface Manager: Android’s Display Engine
The Surface Manager library is a core part of Android’s UI rendering system. It manages how apps display content on the screen, including:
- Layers: Each app’s UI is divided into layers (e.g.,
Surface,HardwareBuffer). - Hardware Acceleration: Offloads rendering to the GPU for smoother animations.
- Multitasking: Handles overlapping windows (e.g., split-screen mode).
2.1 Key Components of Surface Manager
SurfaceFlinger: The main process that composes all UI layers into a single display output.Surface: A handle to a buffer (e.g.,Bitmap,Texture) that represents a portion of the screen.HardwareBuffer: A GPU-accelerated buffer for efficient rendering.ViewRoot: The bridge between the app’sViewhierarchy and theSurface.
Visual: Surface Manager Layer Stack
graph TD
A["Display Output"] --> B["SurfaceFlinger"]
B --> C["Layer 1: App A's Surface"]
B --> D["Layer 2: App B's Surface"]
B --> E["Layer 3: System UI"]
C --> F["ViewRoot"]
F --> G["View Hierarchy"]
G --> H["Views (Buttons, Text, etc.)"]
C --> I["HardwareBuffer: GPU-Accelerated Rendering"]2.2 How Surface Manager Works
- App Renders UI: The app’s
Viewhierarchy is drawn into aSurface. SurfaceFlingerComposes Layers: It merges all surfaces (apps + system UI) into one output.- Hardware Acceleration: Uses
HardwareBufferto render directly to the GPU, reducing CPU load. - Display Output: The final composed image is sent to the screen.
Worked Example: Animating a Button Suppose you animate a button’s scale in an app:
- The
View(button) is drawn into aSurface. SurfaceFlingercomposes thisSurfacewith other layers (e.g., other apps, system bars).- The GPU renders the scaled button using
HardwareBuffer. - The final image is displayed on the screen.
Visual: Button Animation Steps
3. Real-World Applications
In the Real World
WhatsApp (Android)
- ART’s AOT Compilation: WhatsApp uses ART to compile its
.dexfiles to native code, ensuring fast startup and smooth messaging even on low-end devices. - Surface Manager: When you swipe between chats, WhatsApp’s UI layers are managed by
SurfaceFlinger, allowing smooth transitions without lag.
- ART’s AOT Compilation: WhatsApp uses ART to compile its
Pathao (Ride-Hailing App)
- Hardware Acceleration: Pathao’s maps and real-time location updates rely on
SurfaceFlingerto render GPU-accelerated graphics, ensuring the driver’s location updates smoothly. - Multitasking: If you open Pathao while using another app (e.g., music player),
SurfaceFlingercomposes both UIs without conflicts.
- Hardware Acceleration: Pathao’s maps and real-time location updates rely on
Ncell (Mobile Network)
- ART for Battery Life: Ncell’s apps (e.g., MyNcell) use ART to reduce CPU usage, extending battery life for users who check data plans or top-ups frequently.
- Surface Manager for UI: The app’s UI layers are efficiently managed, ensuring quick responses even during network fluctuations.
4. Performance Considerations
4.1 Advantages of ART
- Faster Execution: No runtime JIT compilation means quicker app launches.
- Better Battery Life: Less CPU usage during execution.
- Smoother Animations: GPU-accelerated rendering via
SurfaceFlinger.
4.2 Challenges
- Memory Usage: AOT compilation requires more storage space for native code.
- Cold Start: First-time app launches may still be slower if ART hasn’t optimized the
.dexfile yet. - Hardware Acceleration Overhead: Poorly optimized GPU rendering can cause stuttering or crashes.
Worked Example: Loan Interest Calculation (Bank App) Consider a bank app like NMB Bank that calculates loan interest:
- DVM: The interest calculation would involve JIT compilation, slowing down the response time if the method is called frequently.
- ART: The calculation is pre-compiled to native code, so the result is instant, improving user experience.
5. Exam Tips
- DVM vs. ART:
- Emphasize that ART uses AOT compilation at install time, while DVM relied on JIT at runtime.
- Mention PGO as a key advantage of ART for optimizing hot paths.
- Surface Manager:
- Focus on the layered architecture (
SurfaceFlinger,Surface,HardwareBuffer). - Explain how hardware acceleration reduces CPU load but requires careful management.
- Focus on the layered architecture (
- Real-World Tie-Ins:
- Relate ART to faster app launches (e.g., WhatsApp, Pathao).
- Relate Surface Manager to smooth animations (e.g., Ncell’s UI, Daraz’s product carousels).
- Common Pitfalls:
- Avoid confusing AOT (compile-time) with JIT (runtime).
- Don’t forget to mention that SurfaceFlinger is the compositor, not a single layer.
Sample Exam Question: "Explain how ART improves the performance of a mobile app like Daraz compared to DVM. Also, describe the role of SurfaceFlinger in rendering a product carousel smoothly."
Model Answer Structure:
- ART’s AOT Compilation: Faster startup and lower CPU usage → Daraz loads quicker.
- PGO Optimization: Hot paths (e.g., product search) are optimized for speed.
- SurfaceFlinger: Composes Daraz’s UI layers with other apps (e.g., music player) smoothly.
- Hardware Acceleration: GPU renders the carousel without stuttering.
Final Note: This unit is highly visual. Always draw:
- The DVM vs. ART compilation flow.
- The Surface Manager layer stack.
- A traced example of how ART optimizes a method call (e.g., a button click in Pathao).
Based on the TU BCA syllabus for Mobile Programming (CACS351), unit 11.
Discussion
Loading…