ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
แก้ไขข้อบกพร่องการใช้หน่วยความจําเนทีฟ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Address Sanitizer: HWASan/ASan
นักพัฒนาแพลตฟอร์ม Android ใช้ HWAddressSanitizer (HWASan) เพื่อค้นหาข้อบกพร่องด้านหน่วยความจำใน C/C++
คุณสามารถแฟลชภาพ HWASan ที่คอมไพล์ไว้ล่วงหน้าลงในอุปกรณ์ Pixel ที่รองรับได้จาก ci.android.com (วิธีการตั้งค่าโดยละเอียด)
ตั้งแต่ Android 8.0 (Oreo) เป็นต้นไป คุณสามารถใช้ ASan เพื่อแก้ไขข้อบกพร่องของแอปในอุปกรณ์เวอร์ชันที่ใช้งานจริงที่ไม่ได้รูทได้ด้วย ดูวิธีการได้ใน
วิกิของ ASan
Heapprofd
Android 10 รองรับ heapprofd ซึ่งเป็นเครื่องมือตรวจสอบ heap แบบสุ่มตัวอย่างที่มีค่าใช้จ่ายต่ำ
heapprofd ช่วยให้คุณระบุแหล่งที่มาของการใช้หน่วยความจำแบบเนทีฟไปยังกองซ้อนการเรียกใช้ในโปรแกรมได้
ดูข้อมูลเพิ่มเติมได้ที่
heapprofd - Android Heap Profiler ในเว็บไซต์เอกสารประกอบของ Perfetto
การแก้ไขข้อบกพร่อง Malloc
ดูคำอธิบายโดยละเอียดของตัวเลือกการแก้ไขข้อบกพร่องสำหรับปัญหาเกี่ยวกับหน่วยความจำแบบเนทีฟได้ที่การแก้ไขข้อบกพร่อง Malloc และการติดตามหน่วยความจำแบบเนทีฟโดยใช้การเรียกกลับ libc
libmemunreachable
libmemunreachable ของ Android เป็นเครื่องมือตรวจหาการรั่วไหลของหน่วยความจำของระบบแบบไม่มีค่าใช้จ่ายเพิ่มเติม
โดยใช้เครื่องมือเก็บขยะแบบทำเครื่องหมายและลบออกซึ่งทำงานอย่างไม่แม่นยำในหน่วยความจำเดิมทั้งหมด โดยรายงานบล็อกที่เข้าถึงไม่ได้ว่าเป็นการรั่วไหล ดูวิธีการใช้งานได้ที่หัวข้อเอกสารประกอบของ libmemunreachable
ฮุก Malloc
หากต้องการสร้างเครื่องมือของคุณเอง libc ของ Android ยังรองรับการขัดจังหวะการเรียกใช้การจัดสรร/การปลดปล่อยหน่วยความจำทั้งหมดที่เกิดขึ้นระหว่างการเรียกใช้โปรแกรมด้วย ดูวิธีการใช้ได้จากเอกสารประกอบของ malloc_hooks
สถิติ Malloc
Android รองรับส่วนขยาย mallinfo(3)
และ malloc_info(3)
ของ <malloc.h>
ฟังก์ชัน malloc_info
พร้อมใช้งานใน Android 6.0 (Marshmallow) ขึ้นไป และมีการบันทึกสคีมา XML ไว้ใน <malloc.h>
ของ Bionic
เซิร์ฟเวอร์เครื่องมือตรวจสอบข้อบกพร่องของ Dalvik
นอกจากนี้ คุณยังใช้เซิร์ฟเวอร์ตรวจสอบข้อบกพร่อง Dalvik (DDMS) เพื่อดูภาพกราฟิกของเอาต์พุตการแก้ไขข้อบกพร่อง Malloc ได้ด้วย
หากต้องการใช้ DDMS ให้เปิด UI หน่วยความจำหลักก่อน โดยทำดังนี้
- เปิด
~/.android/ddms.cfg
- เพิ่มบรรทัด:
native=true
เมื่อเปิด DDMS อีกครั้งและเลือกกระบวนการแล้ว คุณสามารถเปลี่ยนไปใช้แท็บการจัดสรรใหม่และป้อนข้อมูลรายการการจัดสรร ซึ่งจะเป็นประโยชน์อย่างยิ่งสำหรับการแก้ไขข้อบกพร่องหน่วยความจำรั่วไหล
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 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-07-27 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."]]