You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何调用posix_memalign()会触发SIGKILL信号?

问题:使用posix_memalign()触发SIGKILL但malloc()不会的原因?

下面的代码调用posix_memalign()并初始化字节后最终会触发SIGKILL信号,但换成malloc()则不会,请问原因是什么?

代码示例

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>

void *p; // 迷惑编译器,避免优化

int main() {
    for(int i = 0; i < 1600000; i++) {
        if ((i % 10000) == 0) {
            std::cout << i << std::endl;
        }
        int r = 0;
        r = posix_memalign(&p, 4096, sizeof(uint[1024])); // 会触发SIGKILL
        assert(r == 0);
//        p = malloc(sizeof(uint[1024])); // 不会触发SIGKILL
        assert(p != NULL);
        memset(p, 4, sizeof(uint[1024])); 
    }
    return ((int*)(p))[0];
}

编译运行结果

$ g++ -g -Wall  test.cpp -O3 -o test.exe && ./test.exe
...
1280000
1290000
1300000
1310000
Killed

由于是SIGKILL信号,无法在GDB中捕获。

环境信息

GCC版本

$gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 

系统版本

$ uname -a
Linux swanson-dev-10.67.190.1 5.15.0-76-generic #83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

原因分析

核心差异来自两者的内存分配策略,结合代码的内存泄漏问题,最终触发了Linux的OOM Killer:

  • 内存对齐与分配粒度差异:posix_memalign()要求内存必须按指定对齐值(这里是4096字节,即系统页大小)对齐,且分配的内存会直接占用完整的物理页。你请求的sizeof(uint[1024])在64位系统中是4096字节,刚好一页,每次调用都会分配独立的一页,不会和其他分配共享页面。
    而malloc()对于这类小内存分配,会使用内存池机制,多个小分配可以共享同一个物理页面,大幅降低实际占用的物理内存。

  • 内存泄漏触发OOM Killer:你的代码存在严重内存泄漏——循环160万次,每次分配内存后都不释放。使用posix_memalign()时,总内存需求约为6.1GB(1600000 * 4KB),很快会耗尽系统可用内存,Linux的OOM Killer会选中这个内存占用最高的进程,发送SIGKILL将其终止。
    而malloc()的内存池复用机制让实际占用的物理内存远低于这个数值,不会触发OOM Killer。

  • 验证方法:如果在循环中添加free(p)释放内存,两种分配方式都不会触发SIGKILL;也可以用top命令观察进程的RSS(常驻内存),posix_memalign()版本的RSS会线性增长,而malloc()版本增长平缓。

内容的提问来源于stack exchange,提问作者MemoryWrangler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 21:42:49