3

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?

Shahin
  • 1,196
  • 1
  • 8
  • 15
  • Some thoughts about debugging get_mem_usage on [this old question](https://stackoverflow.com/questions/14881843/c-program-immediately-using-2-gb-of-ram-how-to-find-culprit) e.g. keep the process running and check maps and smaps under proc. The reason there was static initialisation from libraries linked in though, which I guess you're not doing. I assume your Arch install is using glibc, and linking dynamically? – Rup Apr 17 '20 at 07:38
  • @Rup That link you shared is super useful! I guess there were processes that were sharing memory. I restarted the system and ran the executable again on my laptop. It now shows `1500`. I'll have to investigate and confirm this – Shahin Apr 17 '20 at 07:52

1 Answers1

1

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.

Shahin
  • 1,196
  • 1
  • 8
  • 15