[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],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)."]]