C 可执行文件在两台不同 Linux 计算机上的资源分配

Resource Allocation of a C executable on two different Linux Computers

我正在两台不同的 linux 计算机(Arch on Huawei Laptop 8GB RAM,Ubuntu iMac 2017 32GB RAM)上编译和 运行 以下 c 文件。

#include <stdio.h>
#include <sys/resource.h>

long get_mem_usage()
{
    struct rusage myusage;
    getrusage(RUSAGE_SELF, &myusage);
    return myusage.ru_maxrss;
}

int main()
{
    printf("usage: %ld\n", get_mem_usage());
    return 0;
}

编译器是: gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1

gcc(Arch Linux 9.3.0-1)9.3.0

在 Ubuntu,我始终得到:

usage: 2432
usage: 2432
usage: 2432

在 Arch 上,输出不一致且更大:

usage: 100584
usage: 100964
usage: 100524

我很困惑为什么两者之间的这些值差异如此之大 computers/distros。这种内存分配模式的原因是什么?分配这些内存资源的是编译器吗?还是内核决定内存分配?

可执行文件很可能与其他几个进程共享内存。我停止了桌面环境并杀死了大部分不需要的程序并得到了一致的值 1500。尽管启用了桌面管理器,但分配给进程的内存大小不同。