自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
實作 ART 及時編譯器
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 執行階段 (ART) 包含即時 (JIT) 編譯器,可透過程式碼剖析持續改善 Android 應用程式執行時的效能。JIT 編譯器可補足 ART 目前的預先 (AOT) 編譯器,並改善執行階段效能、節省儲存空間,以及加快應用程式和系統更新速度。它還可改善 AOT 編譯器,避免在自動應用程式更新期間或在無線更新 (OTA) 期間重新編譯應用程式時,系統發生減速的情形。
雖然 JIT 和 AOT 使用相同的編譯器,且採用類似的最佳化組合,但產生的程式碼可能不盡相同。JIT 會使用執行階段類型資訊,可進行更佳的內嵌作業,並可進行堆疊替換 (OSR) 編譯,所有這些都會產生略有不同的程式碼。
JIT 架構
圖 1. JIT 架構。
JIT 編譯
JIT 編譯涉及下列活動:
圖 2. 設定檔引導編譯。
- 使用者執行應用程式,然後觸發 ART 載入
.dex
檔案。
- 如果
.oat
檔案 (.dex
檔案的 AOT 二進位檔) 可用,ART 會直接使用該檔案。雖然 .oat
檔案會定期產生,但不一定會包含編譯的程式碼 (AOT 二進位檔)。
- 如果
.oat
檔案不含編譯程式碼,ART 會透過 JIT 和轉譯器執行 .dex
檔案。
- 對於未根據
speed
編譯篩選器 (「盡可能從應用程式中編譯」) 編譯的任何應用程式,系統都會啟用 JIT。
- JIT 設定檔資料會轉儲至系統目錄中的檔案,該檔案只有應用程式可存取。
- AOT 編譯 (
dex2oat
) 服務會剖析該檔案,以便驅動編譯作業。
圖 3. JIT 守護程式活動。
Google Play 服務就是其他應用程式使用的例子,其行為類似於共用程式庫。
JIT 工作流程
圖 4. JIT 資料流。
- 分析資訊會儲存在程式碼快取中,並在記憶體壓力下進行垃圾收集。
- 我們無法保證在應用程式背景執行時擷取的快照會包含完整資料 (即所有經過 JIT 編譯的資料)。
- 系統不會嘗試確保所有內容都已記錄 (因為這可能會影響執行階段效能)。
- 方法可處於三種狀態:
- 解譯 (dex 程式碼)
- JIT 編譯
- AOT 編譯
如果同時存在 JIT 和 AOT 程式碼 (例如因重複的去最佳化而產生),系統會優先使用 JIT 程式碼。
- 如要在不影響前景應用程式效能的情況下執行 JIT,記憶體需求取決於相關應用程式。大型應用程式需要的記憶體比小型應用程式多。一般來說,大型應用程式會在 4 MB 左右穩定下來。
開啟 JIT 記錄功能
如要啟用即時編譯記錄功能,請執行下列指令:
adb root
adb shell stop
adb shell setprop dalvik.vm.extra-opts -verbose:jit
adb shell start
停用 JIT
如要停用 JIT,請執行下列指令:
adb root
adb shell stop
adb shell setprop dalvik.vm.usejit false
adb shell start
強制編譯
如要強制編譯,請執行下列指令:
adb shell cmd package compile
強制編譯特定套件的常見用途:
- 依據設定檔:
adb shell cmd package compile -m speed-profile -f my-package
- 完整:
adb shell cmd package compile -m speed -f my-package
強制編譯所有套件的常見用途:
- 依據設定檔:
adb shell cmd package compile -m speed-profile -f -a
- 完整:
adb shell cmd package compile -m speed -f -a
清除設定檔資料
Android 13 以下版本
如要清除本機設定檔資料並移除已編譯的程式碼,請執行以下操作:
adb shell pm compile --reset
在 Android 14 以上版本
如要只清除本機設定檔資料:
adb shell pm art clear-app-profiles
注意:與 Android 13 以下版本的指令不同,這個指令不會清除透過應用程式安裝的外部設定檔資料 (".dm")。
如要清除本機設定檔資料,並移除由本機設定檔資料產生的編譯代碼 (也就是將其重設為安裝狀態),請執行下列指令:
adb shell pm compile --reset
注意:此指令不會移除從應用程式安裝的外部設定檔資料 (`.dm`) 產生的編譯程式碼。
如要清除所有已編譯的程式碼,請執行下列指令:
adb shell cmd package compile -m verify -f
注意:此指令會保留本機設定檔資料。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Implement ART just-in-time compiler\n\nAndroid runtime (ART) includes a just-in-time (JIT) compiler with code profiling\nthat continually improves the performance of Android applications as they run.\nThe JIT compiler complements ART's current ahead-of-time (AOT) compiler and\nimproves runtime performance, saves storage space, and speeds application and\nsystem updates. It also improves upon the AOT compiler by avoiding system\nslowdown during automatic application updates or recompilation of applications\nduring over-the-air (OTA) updates.\n\n\nAlthough JIT and AOT use the same compiler with a similar set of optimizations,\nthe generated code might not be identical. JIT makes use of runtime type\ninformation, can do better inlining, and makes on stack replacement (OSR)\ncompilation possible, all of which generates slightly different code.\n\nJIT architecture\n----------------\n\n**Figure 1.** JIT architecture.\n\nJIT compilation\n---------------\n\nJIT compilation involves the following activities:\n**Figure 2.** Profile-guided compilation.\n\n1. The user runs the app, which then triggers ART to load the `.dex` file.\n - If the `.oat` file (the AOT binary for the `.dex` file) is available, ART uses it directly. Although `.oat` files are generated regularly, they don't always contain compiled code (AOT binary).\n - If the `.oat` file does not contain compiled code, ART runs through JIT and the interpreter to execute the `.dex` file.\n2. JIT is enabled for any application that is not compiled according to the `speed` compilation filter (which says \"compile as much as you can from the app\").\n3. The JIT profile data is dumped to a file in a system directory that only the application can access.\n4. The AOT compilation (`dex2oat`) daemon parses that file to drive its compilation. \n\n **Figure 3.** JIT daemon activities.\n\n\nThe Google Play service is an example used by other applications that behave\nsimilar to shared libraries.\n\nJIT workflow\n------------\n\n**Figure 4.** JIT data flow.\n\n- Profiling information is stored in the code cache and subjected to garbage collection under memory pressure.\n - There is no guarantee a snapshot taken when the application was in the background will contain complete data (i.e., everything that was JITed).\n - There is no attempt to ensure everything is recorded (as this can impact runtime performance).\n- Methods can be in three different states:\n - interpreted (dex code)\n - JIT compiled\n - AOT compiled\n If both JIT and AOT code exists (e.g. due to repeated de-optimizations), the JITed code is preferred.\n- The memory requirement to run JIT without impacting foreground app performance depends upon the app in question. Large apps require more memory than small apps. In general, large apps stabilize around 4 MB.\n\nTurn on JIT logging\n-------------------\n\nTo turn on JIT logging, run the following commands: \n\n adb root\n adb shell stop\n adb shell setprop dalvik.vm.extra-opts -verbose:jit\n adb shell start\n\nDisable JIT\n-----------\n\nTo disable JIT, run the following commands: \n\n adb root\n adb shell stop\n adb shell setprop dalvik.vm.usejit false\n adb shell start\n\nForce compilation\n-----------------\n\nTo force compilation, run the following: \n\n```\nadb shell cmd package compile\n```\n\nCommon use cases for force compiling a specific package:\n\n- Profile-based: \n\n ```\n adb shell cmd package compile -m speed-profile -f my-package\n ```\n- Full: \n\n ```\n adb shell cmd package compile -m speed -f my-package\n ```\n\nCommon use cases for force compiling all packages:\n\n- Profile-based: \n\n ```\n adb shell cmd package compile -m speed-profile -f -a\n ```\n- Full: \n\n ```\n adb shell cmd package compile -m speed -f -a\n ```\n\nClear profile data\n------------------\n\n### On Android 13 or earlier\n\nTo clear local profile data and remove compiled code, run the following: \n\n```\nadb shell pm compile --reset \n```\n\n### On Android 14 or later\n\nTo clear local profile data only: \n\n```\nadb shell pm art clear-app-profiles \n```\n\nNote: Unlike the command for Android 13\nor earlier, this command doesn't clear external profile data (\\`.dm\\`) that is\ninstalled with the app.\n\nTo clear local profile data and remove compiled code generated from local\nprofile data (i.e., to reset to the install state), run the following: \n\n```\nadb shell pm compile --reset \n```\n\nNote: This command doesn't remove compiled code generated from\nexternal profile data (\\`.dm\\`) that is installed with the app.\n\nTo clear all compiled code, run this command: \n\n```\nadb shell cmd package compile -m verify -f \n```\n\nNote: This command retains local profile data."]]