หน่วยความจำที่ไม่ได้เริ่มต้นใน C และ C++ เป็นสาเหตุที่พบบ่อยของปัญหาความน่าเชื่อถือ ข้อบกพร่องด้านความปลอดภัยของหน่วยความจำ และการรั่วไหลของข้อมูล Android จะเริ่มต้นหน่วยความจำให้ได้มากที่สุดเพื่อหลีกเลี่ยงปัญหาเหล่านี้
[[["เข้าใจง่าย","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,["# Zero initialized memory\n\nUninitialized memory in C and C++ is a common cause of reliability problems,\nmemory safety bugs and information leaks. To avoid these issues, Android\ninitializes as much memory as possible.\n\nZero initialized userspace memory\n---------------------------------\n\n\nSince Android 12, stack memory is zero initialized\nin all platform native code (including JNI) and heap memory is zero\ninitialized in all platform native processes (such as `netd`)\nbut not in the `zygote` or in apps.\n\n\nFirst and third-party apps built with the NDK are strongly\nrecommended to use the `-ftrivial-auto-var-init=zero` [compiler flag](https://cs.android.com/android/platform/superproject/+/android-latest-release:build/soong/cc/config/global.go;l=183;drc=bd20ccb83ef87bba9fdf31a937ccb7b921d67b73) to zero-initialize their stack local\nvariables. The compiler optimizes away any zeroing that is unnecessary.\nFor example, when a local variable is explicitly initialized\n(such as, `int x = 123;` variable `x` is initialized only once).\nIf the program has a large stack buffer in a performance\nhotspot, the developer can disable initialization using a compiler\nattribute:\n`\n__attribute__((__uninitialized__)) char buf[BUFSIZ];\n`\n\n\nApps can also opt in to heap zero initialization by using the\n`android:nativeHeapZeroInitialized` manifest attribute.\nAlternatively, heap zero initialization can be controlled at runtime\nwith:\n`\nint mallopt(M_BIONIC_ZERO_INIT, level)\n`\n\n\nWhere level is 0 or 1.\n| **Note:** Arm MTE implicitly zero-initializes almost all heap memory, with the exception of a small number of large heap allocations. We encourage C/C++ developers to use zero initialized memory wherever possible.\n\nZero initialized kernel memory\n------------------------------\n\n\nThe kernel stack and heap is zero initialized for GKI kernels, which is [strongly\nrecommended by the CDD](/docs/compatibility/13/android-13-cdd#97_security_features).\n\n\nFor stack initialization, GKI uses the\n`CONFIG_INIT_STACK_ALL_ZERO` config, which results in building the\nkernel using the `-ftrivial-auto-var-init=zero` compiler flag.\nFor heap initialization, GKI uses the\n`CONFIG_INIT_ON_ALLOC_DEFAULT_ON`, which makes all page heap, SLAB\nand SLUB allocations zero-initialized when they are created. This option is\neffectively similar to passing `init_on_alloc=1` as a kernel\nboot-time option.\n\nBug reports\n-----------\n\n\nOur tools generate insightful bug reports that contain additional information\nto aid with debugging. The additional allocation and deallocation stack trace\nhelp better understand the life cycle of a given allocation and lead to\nroot-causing memory safety bugs much faster.\n**Figure 1**: Bug reports generated by memory safety tools\n\n\nDuring development, vendors should monitor the presence of bugs by checking\n`/data/tombstones` and\n`logcat` for native crashes. For more information on\ndebugging Android native code see the information [here](/devices/tech/debug)."]]