Debugging Native Memory Use

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:

  1. Open ~/.android/ddms.cfg
  2. 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.