自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
對原生記憶體使用情形進行偵錯
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Address Sanitizer:HWASan/ASan
Android 平台開發人員會使用 HWAddressSanitizer (HWASan) 找出 C/C++ 中的記憶體錯誤。
您可以從 ci.android.com 將預先建構的 HWASan 映像檔刷新至支援的 Pixel 裝置 (詳細設定操作說明)。
自 Android 8.0 (Oreo) 起,您也可以在未解鎖的正式版裝置上使用 ASan 偵錯應用程式。如需操作說明,請參閱
ASan wiki。
Heapprofd
Android 10 支援 heapprofd,這是一種低負擔的取樣堆積分析器。heapprofd 可讓您將原生記憶體用量歸因於程式中的呼叫堆疊。詳情請參閱 Perfetto 說明網站上的「
heapprofd - Android 堆積分析器」。
Malloc debug
如要全面瞭解原生記憶體問題的偵錯選項,請參閱「Malloc 偵錯」和「使用 libc 回呼追蹤原生記憶體」。
libmemunreachable
Android 的 libmemunreachable 是零負擔的原生記憶體流失偵測器。它會使用不精確的標記和掃描垃圾收集器,掃描所有原生記憶體,並將任何無法存取的區塊視為記憶體流失。如需相關使用操作說明,請參閱 libmemunreachable 說明文件。
Malloc 掛鉤
如要自行建構工具,Android 的 libc 也可攔截在程式執行期間發生的所有配置/釋放呼叫。如需相關使用操作說明,請參閱 malloc_hooks 說明文件。
Malloc 統計資料
Android 支援 <malloc.h>
的 mallinfo(3)
和 malloc_info(3)
擴充功能。Android 6.0 (Marshmallow) 以上版本支援 malloc_info
函式,其 XML 架構記錄在 Bionic 的 <malloc.h>
中。
Dalvik 偵錯監視程序伺服器
您也可以使用 Dalvik 偵錯監視器伺服器 (DDMS),取得 Malloc 偵錯輸出的圖形檢視畫面。
如要使用 DDMS,請先開啟原生記憶體 UI:
- 開啟「
~/.android/ddms.cfg
」
- 新增這一行:
native=true
重新啟動 DDMS 並選取程序後,您可以切換至新的原生配置分頁,並在該分頁中填入配置清單。這項功能特別適合用於偵錯記憶體流失問題。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Debug native memory use\n\nAddress Sanitizer: HWASan/ASan\n------------------------------\n\nAndroid platform developers use [HWAddressSanitizer](/docs/security/test/hwasan)\n(HWASan) to find memory bugs in C/C++.\n\nYou can flash prebuilt HWASan images to supported Pixel devices from\n[ci.android.com](https://ci.android.com/builds/branches/aosp-master-with-phones-throttled/grid?) ([detailed setup instructions](https://developer.android.com/ndk/guides/hwasan)).\n\nSince Android 8.0 (Oreo) it's also possible to use ASan to debug apps on non-rooted production devices. You can find instructions on the [ASan wiki](https://github.com/google/sanitizers/wiki/AddressSanitizerOnAndroidO).\n\n\u003cbr /\u003e\n\nHeapprofd\n---------\n\nAndroid 10 supports heapprofd, a low-overhead, sampling heap profiler.\nheapprofd lets you attribute native memory usage to callstacks in your program.\nSee [heapprofd - Android Heap Profiler](https://docs.perfetto.dev/#/heapprofd) on the\n[Perfetto documentation site](https://docs.perfetto.dev/#/)\nfor more information.\n\nMalloc debug\n------------\n\nSee [Malloc\nDebug](https://android.googlesource.com/platform/bionic/+/android16-release/libc/malloc_debug/README.md) and [Native\nMemory Tracking using libc Callbacks](https://android.googlesource.com/platform/bionic/+/android16-release/libc/malloc_debug/README_api.md) for a thorough description of the\ndebugging options available for native memory issues.\n\nlibmemunreachable\n-----------------\n\nAndroid's libmemunreachable is a zero-overhead native memory leak detector.\nIt uses an imprecise mark-and-sweep garbage collector pass over all native memory,\nreporting any unreachable blocks as leaks. See the\n[libmemunreachable\ndocumentation](https://android.googlesource.com/platform/system/memory/libmemunreachable/+/android16-release/README.md) for usage instructions.\n\nMalloc hooks\n------------\n\nIf you want to build your own tools, Android's libc also supports intercepting all\nallocation/free calls that happen during program execution. See the\n[malloc_hooks\ndocumentation](https://android.googlesource.com/platform/bionic/+/android16-release/libc/malloc_hooks/README.md) for usage instructions.\n\nMalloc statistics\n-----------------\n\n\nAndroid supports the [`mallinfo(3)`](http://man7.org/linux/man-pages/man3/mallinfo.3.html)and [`malloc_info(3)`](http://man7.org/linux/man-pages/man3/malloc_info.3.html) extensions to `\u003cmalloc.h\u003e`.\nThe `malloc_info` function is available in Android 6.0 (Marshmallow) and higher and\nits XML schema is documented in Bionic's\n[`\u003cmalloc.h\u003e`](https://android.googlesource.com/platform/bionic/+/android16-release/libc/include/malloc.h).\n\nDalvik Debug Monitor Server\n---------------------------\n\nYou can also use the [Dalvik Debug\nMonitor Server (DDMS)](https://developer.android.com/studio/profile/ddms.html) to obtain a graphical view of Malloc Debug\noutput.\n\nTo use DDMS, first turn on its native memory UI:\n\n1. Open `~/.android/ddms.cfg`\n2. Add the line: `native=true`\n\nUpon relaunching DDMS and selecting a process, you can switch to the new\nnative allocation tab and populate it with a list of allocations. This is\nespecially useful for debugging memory leaks."]]