2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
ネイティブ メモリの使用状況をデバッグする
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
AddressSanitizer(HWASan / ASan)
Android プラットフォームのデベロッパーは、C/C++ のメモリバグを検出するために HWAddressSanitizer(HWASan)を使用します。
ci.android.com からサポート対象の Google Pixel デバイスにビルド済みの HWASan イメージを書き込めます(詳細な設定手順)。
Android 8.0(Oreo)以降では、ASan を使用して、ルート権限がない製品版デバイス上でもアプリをデバッグできるようになりました。使用方法については、
ASan Wiki をご覧ください。
Heapprofd
Android 10 は、オーバーヘッドの少ないサンプリング ヒープ プロファイラである heapprofd をサポートしています。heapprofd では、プログラム内のコールスタックにネイティブ メモリの使用を関連付けることができます。詳細については、Perfetto ドキュメント サイトの heapprofd - Android Heap Profiler をご覧ください。
malloc デバッグ
ネイティブ メモリの問題に使用できるデバッグ オプションの詳細については、malloc デバッグと libc コールバックを使用したネイティブ メモリ トラッキングをご覧ください。
libmemunreachable
Android の libmemunreachable を使用すると、オーバーヘッドがゼロのネイティブ メモリリークを検出できます。精密でないマークアンドスイープ ガベージ コレクタパスをすべてのネイティブ メモリ上で使用し、到達不能なブロックをリークとして報告します。使用方法については、libmemunreachable のドキュメントをご覧ください。
malloc フック
独自のツールをビルドする場合、Android の libc は、プログラムの実行中に発生するすべての割り当て / フリーコールのインターセプトもサポートしています。使用方法については、malloc_hooks のドキュメントをご覧ください。
malloc 統計情報
Android は、<malloc.h>
に対する mallinfo(3)
および malloc_info(3)
拡張機能をサポートしています。malloc_info
関数は、Android 6.0(Marshmallow)以上で利用できます。その XML スキーマは、Bionic の <malloc.h>
に記載されています。
Dalvik Debug Monitor Server
Dalvik Debug Monitor Server(DDMS)を使用して、Malloc デバッグの出力をグラフィカル表示することもできます。
DDMS を使用するには、まずネイティブ メモリ UI を有効にします。
~/.android/ddms.cfg
を開きます。
native=true
の行を追加します。
DDMS を再起動してプロセスを選択すると、新しいネイティブ割り当てタブに切り替えて、割り当てのリストを入力できます。これは、特にメモリリークのデバッグで役立ちます。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-03-26 UTC。
[[["わかりやすい","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-03-26 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."]]