clinuxgetrusage

Resource Allocation of a C executable on two different Linux Computers


I am compiling and running the following c file on two different linux computers (Arch on Huawei Laptop 8GB RAM, Ubuntu on iMac 2017 32GB RAM).

#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;
}

The compilers are: gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1

gcc (Arch Linux 9.3.0-1) 9.3.0

On Ubuntu, I consistently get:

usage: 2432
usage: 2432
usage: 2432

On Arch, the output was not consistent and much larger:

usage: 100584
usage: 100964
usage: 100524

I am fairly confused why these values differ to such a degree between the two computers/distros. What is the cause of this memory allocation pattern? Is it the compiler that's assigning those memory resources? Or is it the kernel that decides memory allocation?


Solution

  • The executable was most likely sharing memory with a few other processes. I stopped the desktop environment and killed most of the unwanted programs and got a consistent value of 1500. Though with the desktop manager enabled, there were varying memory sizes allocated to the process.