自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
调试原生内存使用问题
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Address Sanitizer (HWASan/ASan)
Android 平台开发者使用 HWAddressSanitizer (HWASan) 在 C/C++ 中查找内存错误。
您可以通过 ci.android.com,将预构建的 HWASan 映像刷写到支持的 Pixel 设备上(详细设置说明)。
自 Android 8.0 (Oreo) 开始,您可以使用 ASan 在未取得 root 权限的正式版设备上对应用进行调试。您可以在
ASan wiki 上找到相关说明。
Heapprofd
Android 10 支持 happrofd,它是一种低开销的采样堆分析器。您可以通过 heapprod 将原生内存使用率归因于程序中的调用堆栈。
如需了解详情,请参阅 Perfetto 文档网站上的 heapprofd - Android 堆分析器。
Malloc 调试
有关本地内存问题可用调试选项的完整说明,请参阅 Malloc 调试和使用 libc 回调跟踪本地内存。
libmemunreachable
Android 的 libmemunreachable 是一个零开销的本地内存泄漏检测器。
它会使用不精确的“标记-清除”垃圾回收器遍历所有本机内存,同时将任何不可访问的块报告为泄漏。有关使用说明,请参阅 libmemunacachable 文档。
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,请先打开其本机内存界面:
- 打开
~/.android/ddms.cfg
- 添加以下行:
native=true
重新启动 DDMS 并选择一个进程后,您可以切换到新的本地分配选项卡,并使用分配列表进行填充。这对于调试内存泄漏尤为有用。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","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-03-26。"],[],[],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."]]