自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
Android 執行階段和 Dalvik
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 執行階段 (ART) 是 Android 上應用程式和部分系統服務使用的受管理執行階段。ART 和其前身 Dalvik 最初是專為 Android 專案而設計。ART 會在執行階段執行 Dalvik 執行檔 (DEX) 格式和 DEX 位元碼規格。
ART 和 Dalvik 都是執行 DEX 位元碼的相容執行階段,因此針對 Dalvik 開發的應用程式在搭配 ART 執行時應可正常運作。不過,有些在 Dalvik 上可用的技巧不適用於 ART。如要瞭解最重要的問題,請參閱「在 Android 執行階段 (ART) 驗證應用程式行為」。
ART 功能
以下是 ART 實作的一些主要功能。
預先編譯 (AOT)
ART 會導入預先 (AOT) 編譯,可改善應用程式效能。ART 的安裝時間驗證比 Dalvik 更嚴格。
在安裝期間,ART 會使用裝置端的 dex2oat 工具編譯應用程式。這個公用程式會接受 DEX 檔案做為輸入內容,並為目標裝置產生已編譯的應用程式執行檔。這項公用程式應可輕鬆編譯所有有效的 DEX 檔案。不過,部分後置處理工具會產生無效檔案,雖然 Dalvik 可以容許這些檔案,但 ART 無法編譯這些檔案。詳情請參閱「處理垃圾收集問題」。
改善垃圾收集
垃圾收集 (GC) 會大量耗用資源,可能會影響應用程式的效能,導致畫面顯示斷斷續續、UI 回應速度緩慢等問題。ART 可透過多種方式改善垃圾收集:
- 大部分是並行設計,僅有一次 GC 暫停
- 並行複製,以減少背景記憶體用量和碎片
- GC 暫停時間的長度與堆積大小無關
- 針對清理最近分配的短暫物件的特殊情況,提供總 GC 時間較短的收集器
- 改善垃圾收集作業的使用者體驗,讓垃圾收集作業更有效率,因此在一般用途中,
GC_FOR_ALLOC
事件極為罕見
改善開發和偵錯功能
ART 提供多項功能,可改善應用程式開發和偵錯作業。
支援取樣分析工具
過去,開發人員會使用 Traceview 工具 (專門用於追蹤應用程式執行情形) 做為剖析器。雖然 Traceview 會提供實用的資訊,但在 Dalvik 上,其結果會因個別方法呼叫的額外負擔而偏離,而且使用這項工具會明顯影響執行時間效能。
ART 新增了專屬的取樣分析工具支援,不受這些限制。這樣一來,您就能更準確地查看應用程式執行情形,且不會造成明顯的速度減緩。在 KitKat 版本中,Traceview 新增了 Dalvik 的取樣支援功能。
支援更多偵錯功能
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
現在會顯示應用程式嘗試使用空值指標執行的動作相關資訊,例如應用程式嘗試寫入的欄位,或是嘗試呼叫的方法。以下列舉一些常見的例子:
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 開放原始碼計畫 Issue Tracker 回報問題。請附上 adb bugreport
,並附上 Google Play 商店中的應用程式連結 (如適用)。否則,請盡可能附加可重現問題的 APK。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間: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."]]