自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
Android 运行时和 Dalvik
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android Runtime (ART) 是 Android 上的应用和部分系统服务使用的托管式运行时。ART 及其前身 Dalvik 最初是专为 Android 项目打造的。作为运行时的 ART 可执行 Dalvik 可执行 (DEX) 文件并遵循 DEX 字节码规范。
ART 和 Dalvik 是运行 DEX 字节码的兼容运行时,因此针对 Dalvik 开发的应用也能在 ART 环境中运作。不过,Dalvik 采用的一些技术并不适用于 ART。有关最重要问题的信息,请参阅在 Android Runtime (ART) 上验证应用行为。
ART 功能
以下是 ART 实现的一些主要功能。
预先 (AOT) 编译
ART 引入了预先编译机制,可提高应用的性能。ART 还具有比 Dalvik 更严格的安装时验证。
在安装时,ART 使用设备自带的 dex2oat 工具来编译应用。此实用工具接受 DEX 文件作为输入,并为目标设备生成经过编译的应用可执行文件。该工具应能够顺利编译所有有效的 DEX 文件。但是,一些后处理工具会生成无效文件,Dalvik 可以接受这些文件,但 ART 无法编译这些文件。如需了解详情,请参阅处理垃圾回收问题。
垃圾回收方面的优化
垃圾回收 (GC) 会耗费大量资源,这可能有损于应用性能,导致显示不稳定、界面响应速度缓慢以及其他问题。ART 通过以下几种方式对垃圾回收做了优化:
- 大多采用并发设计,具有一次 GC 暂停
- 并发复制,可减少后台内存使用和碎片
- GC 暂停的时间不受堆大小影响
- 在清理最近分配的短时对象这种特殊情况中,回收器的总 GC 时间更短
- 优化了垃圾回收的工效,能够更加及时地进行并行垃圾回收,这使得
GC_FOR_ALLOC
事件在典型用例中极为罕见
开发和调试方面的优化
ART 提供了大量功能来优化应用开发和调试。
支持采样分析器
一直以来,开发者都使用 Traceview 工具(用于跟踪应用执行情况)作为性能分析器。虽然 Traceview 可提供有用的信息,但每次方法调用产生的开销会导致 Dalvik 分析结果出现偏差,而且使用该工具明显会影响运行时性能。
ART 添加了对没有这些限制的专用采样分析器的支持,因而可更准确地了解应用执行情况,而不会明显减慢速度。KitKat 版本为 Dalvik 的 Traceview 添加了采样支持。
支持更多调试功能
ART 支持许多新的调试选项,特别是与监控和垃圾回收相关的功能。例如,您可以:
- 查看堆栈跟踪中保留了哪些锁,然后跳转到持有锁的线程。
- 询问指定类的当前活动的实例数、请求查看实例,以及查看使对象保持有效状态的参考。
- 过滤特定实例的事件(如断点)。
- 查看方法退出(使用“method-exit”事件)时返回的值。
- 设置字段观察点,以在访问和/或修改特定字段时暂停程序执行。
优化了异常和崩溃报告中的诊断详细信息
当发生运行时异常时,ART 会为您提供尽可能多的上下文和详细信息。ART 会提供 java.lang.ClassCastException
、java.lang.ClassNotFoundException
和 java.lang.NullPointerException
的更多异常详细信息。(较高版本的 Dalvik 会提供 java.lang.ArrayIndexOutOfBoundsException
和 java.lang.ArrayStoreException
的更多异常详细信息,这些信息现在包括数组大小和越界偏移量;ART 也提供这类信息。)
例如,java.lang.NullPointerException
现在会显示有关应用尝试处理 null 指针时所执行操作的信息,例如应用尝试写入的字段或尝试调用的方法。一些典型示例如下:
java.lang.NullPointerException: Attempt to write to field 'int
android.accessibilityservice.AccessibilityServiceInfo.flags' on a null object
reference
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String java.lang.Object.toString()' on a null object reference
ART 还通过纳入 Java 和原生堆栈信息,在应用原生代码崩溃报告中提供更实用的上下文信息。
报告问题
如果您遇到任何并非由应用 JNI 问题导致的问题,请通过 Android 开源项目问题跟踪器报告这些问题。请在报告中加入 adb bugreport
信息和应用在 Google Play 商店中的链接(如果有)。否则,如果可能,请附加可重现该问题的 APK。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Android runtime and Dalvik\n\nAndroid runtime (ART) is the managed runtime used by apps and some system\nservices on Android. ART and its predecessor Dalvik were originally created\nspecifically for the Android project. ART as the runtime executes the Dalvik\nexecutable (DEX) format and DEX bytecode specification.\n\nART and Dalvik are compatible runtimes running DEX bytecode, so apps\ndeveloped for Dalvik should work when running with ART. However, some\ntechniques that work on Dalvik do not work on ART. For information about the\nmost important issues, see [Verifying\napp behavior on the Android runtime (ART)](http://developer.android.com/guide/practices/verifying-apps-art.html).\n\nART features\n------------\n\nHere are some of the major features implemented by ART.\n\n### Ahead-of-time (AOT) compilation\n\nART introduces ahead-of-time (AOT) compilation, which can improve app\nperformance. ART also has tighter install-time verification than Dalvik.\n\nAt install time, ART compiles apps using the on-device\n**dex2oat** tool. This utility accepts [DEX](/docs/core/runtime/dex-format) files as input and generates\na compiled app executable for the target device. The utility should be able to\ncompile all valid DEX files without difficulty. However, some post-processing\ntools produce invalid files that may be tolerated by Dalvik but cannot be\ncompiled by ART. For more information, see [Addressing\nGarbage Collection Issues](http://developer.android.com/guide/practices/verifying-apps-art.html#GC_Migration).\n\n### Improved garbage collection\n\nGarbage collection (GC) is very resource intensive, which can impair an app's performance,\nresulting in choppy display, poor UI responsiveness, and other problems. ART improves garbage\ncollection in several ways:\n\n- Mostly concurrent design with a single GC pause\n- Concurrent copying to reduce background memory usage and fragmentation\n- The length of the GC pause is independent of the heap size\n- Collector with lower total GC time for the special case of cleaning up recently-allocated, short-lived objects\n- Improved garbage collection ergonomics, making concurrent garbage collections more timely, which makes [`GC_FOR_ALLOC`](http://developer.android.com/tools/debugging/debugging-memory.html#LogMessages) events extremely rare in typical use cases\n\n### Development and debugging improvements\n\nART offers a number of features to improve app development and debugging.\n\n#### Support for sampling profiler\n\nHistorically, developers have used the [Traceview](http://developer.android.com/tools/help/traceview.html)\ntool (designed for tracing\napp execution) as a profiler. While Traceview gives useful information,\nits results on Dalvik have been skewed by the per-method-call overhead, and use\nof the tool noticeably affects run time performance.\n\nART adds support for a dedicated sampling profiler that does not have these\nlimitations. This gives a more accurate view of app execution without\nsignificant slowdown. Sampling support was added to Traceview for\nDalvik in the KitKat release.\n\n#### Support for more debugging features\n\nART supports a number of new debugging options, particularly in monitor- and\ngarbage collection-related functionality. For example, you can:\n\n- See what locks are held in stack traces, then jump to the thread that holds a lock.\n- Ask how many live instances there are of a given class, ask to see the instances, and see what references are keeping an object live.\n- Filter events (like breakpoint) for a specific instance.\n- See the value returned by a method when it exits (using \"method-exit\" events).\n- Set field watchpoint to suspend the execution of a program when a specific field is accessed and/or modified.\n\n#### Improved diagnostic detail in exceptions and crash reports\n\nART gives you as much context and detail as possible when runtime exceptions\noccur. ART provides expanded exception detail for [java.lang.ClassCastException](http://developer.android.com/reference/java/lang/ClassCastException.html),\n[java.lang.ClassNotFoundException](http://developer.android.com/reference/java/lang/ClassNotFoundException.html),\nand [java.lang.NullPointerException](http://developer.android.com/reference/java/lang/NullPointerException.html).\n(Later versions of Dalvik provided expanded exception detail for [java.lang.ArrayIndexOutOfBoundsException](http://developer.android.com/reference/java/lang/ArrayIndexOutOfBoundsException.html)\nand [java.lang.ArrayStoreException](http://developer.android.com/reference/java/lang/ArrayStoreException.html),\nwhich now include the size of the array and the out-of-bounds offset, and ART\ndoes this as well.)\n\nFor example, [java.lang.NullPointerException](http://developer.android.com/reference/java/lang/NullPointerException.html)\nnow shows information about what the app was trying to do with the null pointer,\nsuch as the field the app was trying to write to, or the method it was trying to\ncall. Here are some typical examples: \n\n```\njava.lang.NullPointerException: Attempt to write to field 'int\nandroid.accessibilityservice.AccessibilityServiceInfo.flags' on a null object\nreference\n``` \n\n```\njava.lang.NullPointerException: Attempt to invoke virtual method\n'java.lang.String java.lang.Object.toString()' on a null object reference\n```\n\nART also provides improved context information in app native crash reports,\nby including both Java and native stack information.\n\nReport problems\n---------------\n\nIf you run into any issues that aren't due to app JNI issues, report\nthem through the [Android Open Source\nProject Issue Tracker](/docs/setup/contribute/report-bugs#platform). Include an `adb bugreport` and link to\nthe app in Google Play store if available. Otherwise, if possible, attach an\nAPK that reproduces the issue.\n| **Reminder:** Issues (including attachments) are publicly visible."]]