Starting March 27, 2025, we recommend using android-latest-release
instead of aosp-main
to build and contribute to AOSP. For more information, see Changes to AOSP.
Debug native memory use
Stay organized with collections
Save and categorize content based on your preferences.
Address Sanitizer: HWASan/ASan
Android platform developers use HWAddressSanitizer
(HWASan) to find memory bugs in C/C++.
You can flash prebuilt HWASan images to supported Pixel devices from
ci.android.com (detailed setup instructions).
Since 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.
Heapprofd
Android 10 supports heapprofd, a low-overhead, sampling heap profiler.
heapprofd lets you attribute native memory usage to callstacks in your program.
See
heapprofd - Android Heap Profiler on the
Perfetto documentation site
for more information.
Malloc debug
See Malloc
Debug and Native
Memory Tracking using libc Callbacks for a thorough description of the
debugging options available for native memory issues.
libmemunreachable
Android's libmemunreachable is a zero-overhead native memory leak detector.
It uses an imprecise mark-and-sweep garbage collector pass over all native memory,
reporting any unreachable blocks as leaks. See the
libmemunreachable
documentation for usage instructions.
Malloc hooks
If you want to build your own tools, Android's libc also supports intercepting all
allocation/free calls that happen during program execution. See the
malloc_hooks
documentation for usage instructions.
Malloc statistics
Android supports the mallinfo(3)
and malloc_info(3)
extensions to <malloc.h>
.
The malloc_info
function is available in Android 6.0 (Marshmallow) and higher and
its XML schema is documented in Bionic's
<malloc.h>
.
Dalvik Debug Monitor Server
You can also use the Dalvik Debug
Monitor Server (DDMS) to obtain a graphical view of Malloc Debug
output.
To use DDMS, first turn on its native memory UI:
- Open
~/.android/ddms.cfg
- Add the line:
native=true
Upon relaunching DDMS and selecting a process, you can switch to the new
native allocation tab and populate it with a list of allocations. This is
especially useful for debugging memory leaks.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-06-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-18 UTC."],[],[],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."]]