# Android 运行时

Android 运行时（ART）是 Android 操作系统上执行应用字节码的托管运行时，它通过提前编译（AOT）、即时编译（JIT）和解释执行相结合的方式，运行 Dalvik 可执行格式（DEX）文件。它在 Android 4.4 中作为可选项引入，随后在 Android 5.0（API 级别 21）中取代早期的 Dalvik 虚拟机，成为默认运行时。<sup>[1](https://developer.android.com/guide/platform/index.html)</sup><sup> • </sup><sup>[2](https://ocw.cs.pub.ro/courses/_media/osp/lectures/1.introduction_notes.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 角色 | Android 应用的托管运行时；每个应用运行在独立进程中，拥有自己的 ART 实例<sup>[1](https://developer.android.com/guide/platform/index.html)</sup> |
| 输入格式 | Dalvik 可执行格式（DEX）字节码，专为 Android 设计并针对最小内存占用进行优化；`d8` 等构建工具将 Java 源码编译为 DEX<sup>[1](https://developer.android.com/guide/platform/index.html)</sup> |
| 编译 | 混合 AOT 与 JIT 编译，配合配置文件引导的优化；设备端的 `dex2oat` 编译器生成 `.oat` 二进制文件<sup>[1](https://developer.android.com/guide/platform/index.html)</sup><sup> • </sup><sup>[3](https://source.android.google.cn/docs/core/runtime/jit-compiler?hl=en)</sup> |
| 垃圾回收 | 默认收集器为并发复制（CC），这是一种分代、基于区域、以并发为主、会移动对象的收集器<sup>[4](https://github.com/harshdhamaniya/android-security-handbook/blob/main/13-ART/README.md)</sup> |
| 分发方式 | 自 Android 11 起，ART 与核心库 libcore 打包为 `com.android.art` APEX 模块<sup>[5](https://source.android.com/docs/core/ota/modular-system/art?authuser=1)</sup> |
| 运行时检测 | 当 ART 在使用中时，`System.getProperty("java.vm.version")` 返回 `"2.0.0"` 或更高版本<sup>[6](https://developer.android.com/guide/practices/verifying-apps-art?authuser=3)</sup> |
| 实测收益 | 在 2019 年的一项基准研究中，原生 C 代码在 ART 下的运行速度比在 Dalvik 下快 59%<sup>[7](https://www.researchgate.net/publication/331167460_Performance_Comparison_of_Dalvik_and_ART_on_Different_Android-Based_Mobile_Devices)</sup> |

## 历史与对 Dalvik 的取代

从 Android 1.0 到 4.4，应用运行在 Dalvik 上，这是一个依赖 JIT（即时）编译的虚拟机，在执行过程中将字节码翻译为机器码。<sup>[2](https://ocw.cs.pub.ro/courses/_media/osp/lectures/1.introduction_notes.pdf)</sup><sup> • </sup><sup>[8](https://www.sciencedirect.com/science/article/abs/pii/S0164121216301364)</sup> ART 在 Android 4.4 中作为实验性功能引入，与 Dalvik 并存，并在 2014 年 11 月发布的 Android 5.0（Lollipop）中成为默认运行时。<sup>[2](https://ocw.cs.pub.ro/courses/_media/osp/lectures/1.introduction_notes.pdf)</sup><sup> • </sup><sup>[9](https://zaguan.unizar.es/record/167834/files/texto_completo.pdf?version=2)</sup> ART 最初的显著特性是 AOT（提前）编译，即在安装时而非执行时编译应用。<sup>[8](https://www.sciencedirect.com/science/article/abs/pii/S0164121216301364)</sup>

DEX 字节码格式本身在过渡中得以保留：应用仍以 DEX 文件形式分发，ART 对同一份字节码进行解释执行、JIT 编译和 AOT 编译。<sup>[1](https://developer.android.com/guide/platform/index.html)</sup><sup> • </sup><sup>[10](https://repositum.tuwien.at/handle/20.500.12708/190032)</sup> Google 的文档指出了一种兼容性上的不对称：在 ART 上运行良好的应用通常也能在 Dalvik 上运行，但反过来未必成立。<sup>[1](https://developer.android.com/guide/platform/index.html)</sup>

## 执行模型：AOT、JIT 与配置文件

ART 的现代编译策略是混合式且由配置文件引导的。当用户运行应用时，ART 加载 `.dex` 文件并检查是否存在对应的 `.oat` 文件，即由 `dex2oat` 编译器生成的 AOT 二进制文件。如果 `.oat` 文件包含已编译代码，ART 直接使用；如果没有，ART 则通过 JIT 编译器和解释器执行 DEX 文件。<sup>[3](https://source.android.google.cn/docs/core/runtime/jit-compiler?hl=en)</sup> 对于未在 `speed` 过滤器下编译的任何应用，JIT 都会启用；该过滤器指示编译器尽可能多地编译应用。<sup>[3](https://source.android.google.cn/docs/core/runtime/jit-compiler?hl=en)</sup>

**配置文件引导的优化。** 应用运行期间，JIT 配置文件数据会被转储到仅该应用可访问的系统目录中的一个文件；AOT 编译守护进程（`dex2oat`）解析该文件以驱动其编译。<sup>[3](https://source.android.google.cn/docs/core/runtime/jit-compiler?hl=en)</sup> `speed-profile` 编译器过滤器仅对配置文件显示为热点的代码进行 AOT 编译，而非编译全部代码。<sup>[11](https://android-developers.googleblog.com/2024/09/become-better-android-developer-compiler-explorer.html)</sup> 开发者可以随应用或库附带基线配置文件（Baseline Profile），使 ART 在首次使用前即对指定代码路径进行 AOT 编译；Google 报告称，这通过避免解释执行和 JIT 步骤，使代码执行速度从首次启动起提升约 30%。<sup>[12](https://developer.android.com/topic/performance/baselineprofiles/overview?authuser=447)</sup> 若缺少此类配置文件，应用代码的每一部分则必须在内存中被解释执行后再经 JIT 编译，或在设备空闲时保存到 `odex` 文件中。<sup>[12](https://developer.android.com/topic/performance/baselineprofiles/overview?authuser=447)</sup>

`dex2oat` 编译器是一个独立的二进制文件（`/apex/com.android.art/bin/dex2oat64`），作为独立进程调用，从不在应用内部运行。<sup>[4](https://github.com/harshdhamaniya/android-security-handbook/blob/main/13-ART/README.md)</sup> 在 Android 9（API 级别 28）及更高版本上，ART 可以将应用包的 DEX 文件转换为更紧凑的机器码。<sup>[1](https://developer.android.com/guide/platform/index.html)</sup>

## 垃圾回收

ART 的默认收集器是并发复制（CC），这是一种分代、基于区域、以并发为主、会移动对象的收集器，在应用运行的同时压缩 Java 堆。<sup>[4](https://github.com/harshdhamaniya/android-security-handbook/blob/main/13-ART/README.md)</sup><sup> • </sup><sup>[6](https://developer.android.com/guide/practices/verifying-apps-art?authuser=3)</sup> 垃圾回收与应用同时进行：仍在使用的对象被从正在收集的区域转移到新区域，随后被清空的区域整体回收。<sup>[4](https://github.com/harshdhamaniya/android-security-handbook/blob/main/13-ART/README.md)</sup> 由于每个线程都拥有自己的 TLAB（线程本地分配缓冲区）——即区域中专为该线程保留的一部分——内存分配得以快速进行，通常只需推进一个指针，无需同步。<sup>[4](https://github.com/harshdhamaniya/android-security-handbook/blob/main/13-ART/README.md)</sup>

堆被划分为行为不同的空间：`ImageSpace` 存放从 `.art` 文件映射的启动镜像和应用镜像对象，大部分为只读且共享；`RegionSpace` 是主要的分配空间，对象在此移动；`LargeObjectSpace` 存放超过阈值的分配，通常是大型数组，且不移动；`NonMovingSpace` 存放不可移动的对象，如类；`ZygoteSpace` 存放从 Zygote 继承的对象，在所有应用间以写时复制方式共享。<sup>[4](https://github.com/harshdhamaniya/android-security-handbook/blob/main/13-ART/README.md)</sup>

截至 Android 14 QPR1，Android 有两个生产级垃圾收集器：分代并发复制收集器和更新的并发标记-压缩收集器。<sup>[13](https://www.steveblackburn.org/pubs/papers/android-ismm-2024.pdf)</sup> [Android 17](https://www.edgepedia.org/zh/android-17) 为并发标记-压缩垃圾收集器增加了更频繁、资源消耗更低的年轻代收集。<sup>[14](https://developer.android.google.cn/blog/posts/android-17-is-here?hl=en)</sup>

## 内存管理与 Zygote

ART 和 Dalvik 虚拟机使用分页和内存映射来管理内存。<sup>[15](https://developer.android.com/topic/performance/memory-overview?authuser=57)</sup> 应用进程从 Zygote 进程启动，Zygote 在启动时为每个应用提供一组公共的预加载库和类，并实现很少被写入的数据的写时复制共享，从而大幅降低内存占用。<sup>[13](https://www.steveblackburn.org/pubs/papers/android-ismm-2024.pdf)</sup> 垃圾回收的时机通常无法从应用代码中控制，即便是一次快速的回收也可能影响应用性能。<sup>[15](https://developer.android.com/topic/performance/memory-overview?authuser=57)</sup>

## 模块化交付

ART 模块旨在独立于完整的系统更新，向用户交付运行时优化、功能和错误修复，使用户不再受限于设备出厂时搭载的运行时。<sup>[5](https://source.android.com/docs/core/ota/modular-system/art?authuser=1)</sup> [Android 10](https://www.edgepedia.org/zh/android-10) 的运行时模块工作包括 ART、其托管核心库 libcore，以及原生组件 Bionic 和 ICU；到 [Android 11](https://www.edgepedia.org/zh/android-11)，ART 和 libcore 以不可更新的 APEX 形式分发，而 Bionic 和 ICU 留在平台上，以便更易于更新。<sup>[5](https://source.android.com/docs/core/ota/modular-system/art?authuser=1)</sup> 该模块以 `com.android.art` 形式分发，因为 APK 内的应用和服务需要它存在才能启动。<sup>[5](https://source.android.com/docs/core/ota/modular-system/art?authuser=1)</sup>

## 对开发者的影响

ART 的 JNI（[Java 原生接口](https://www.edgepedia.org/zh/java-native-interface)）比 Dalvik 更严格，Google 建议使用 CheckJNI 模式来捕获 C/C++ 代码中的常见问题。<sup>[6](https://developer.android.com/guide/practices/verifying-apps-art?authuser=3)</sup> 由于 CC 收集器在压缩过程中会移动对象，与压缩式收集器不兼容的技术——例如保存指向对象实例数据的原始指针——应予以避免，尤其是在 JNI 代码中。<sup>[6](https://developer.android.com/guide/practices/verifying-apps-art?authuser=3)</sup>

实测比较表明 ART 优于其前身。在[印度尼西亚大学](https://www.edgepedia.org/zh/university-of-indonesia) 2019 年的一项研究中，在 Android 4.1、4.2、4.4、6.0 和 7.0 上运行快速排序、浮点计算和素数算法，原生 C 代码在 ART 下的性能比在 Dalvik 下快 59%。<sup>[7](https://www.researchgate.net/publication/331167460_Performance_Comparison_of_Dalvik_and_ART_on_Different_Android-Based_Mobile_Devices)</sup> 从基于字节码的执行向基于二进制的执行的更广泛转变——ART 的优化编译器提前、即时或两者兼有地生成原生代码——是一项持续的努力，旨在提升应用运行时性能并降低资源需求。<sup>[10](https://repositum.tuwien.at/handle/20.500.12708/190032)</sup>

## References

1. [Platform architecture, Android Developers](https://developer.android.com/guide/platform/index.html)
2. [Introduction notes, Operating Systems course, Politehnica University of Bucharest](https://ocw.cs.pub.ro/courses/_media/osp/lectures/1.introduction_notes.pdf)
3. [Implement ART just-in-time compiler, Android Open Source Project](https://source.android.google.cn/docs/core/runtime/jit-compiler?hl=en)
4. [ART internals, Android Security Handbook (GitHub)](https://github.com/harshdhamaniya/android-security-handbook/blob/main/13-ART/README.md)
5. [Android Runtime (ART) module, Android Open Source Project](https://source.android.com/docs/core/ota/modular-system/art?authuser=1)
6. [Verifying app behavior on the Android runtime (ART), Android Developers](https://developer.android.com/guide/practices/verifying-apps-art?authuser=3)
7. [Fikri, A., Presekal, A., Sari, R. F., Harwahyu, R. (2019). Performance Comparison of Dalvik and ART on Different Android-Based Mobile Devices, ISRITI 2018](https://www.researchgate.net/publication/331167460_Performance_Comparison_of_Dalvik_and_ART_on_Different_Android-Based_Mobile_Devices)
8. [Computational offloading mechanism for native and Android runtime based mobile applications, Journal of Systems and Software](https://www.sciencedirect.com/science/article/abs/pii/S0164121216301364)
9. [Analyzing the Impact of the Virtual Machine, University of Zaragoza](https://zaguan.unizar.es/record/167834/files/texto_completo.pdf?version=2)
10. [Of Ahead Time: Evaluating Disassembly of Android Apps Compiled to Binary OATs Through the ART, TU Wien reposiTUm](https://repositum.tuwien.at/handle/20.500.12708/190032)
11. [Tools, not Rules: become a better Android developer with Compiler Explorer, Android Developers Blog](https://android-developers.googleblog.com/2024/09/become-better-android-developer-compiler-explorer.html)
12. [Baseline Profiles overview, Android Developers](https://developer.android.com/topic/performance/baselineprofiles/overview?authuser=447)
13. [Memory Management on Mobile Devices, ISMM 2024](https://www.steveblackburn.org/pubs/papers/android-ismm-2024.pdf)
14. [Android 17 is Here, Android Developers Blog](https://developer.android.google.cn/blog/posts/android-17-is-here?hl=en)
15. [Overview of memory management, Android Developers](https://developer.android.com/topic/performance/memory-overview?authuser=57)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/android-runtime

English original: https://www.edgepedia.org/android-runtime
