0

I wrote two C codes and i wanted to compare them by execution time and RAM memory usage. I got the execution time with no problems. Now, for the RAM memory usage, i got values too, but im having some doubts on interpretating them.

I used two functions to get them.

First one, using the proc filesystem (based on How to determine CPU and memory consumption from inside a process?)

void getValue(){
    FILE* file = fopen("/proc/self/status", "r");
    char line[128];

    while (fgets(line, 128, file) != NULL){
        printf("%s", line);
    }
    fclose(file);
}

In this one, i took the value of VmRSS line.

Second one, using the Getrusage API (based on Memory usage of current process in C):

struct rusage r_usage;
getrusage(RUSAGE_SELF, &r_usage);
printf("%ld\n", r_usage.ru_maxrss);

Using these functions, i got different values, but all of them were between 500KB ~ 700KB. But i found the values strange, and i started to erase variables and functions from one of my codes, until there was only the functions above left inside it. I executed it and the result was a slightly lower value than the previous result.

So, i was expecting some bytes as its size, but it is hundreds of KB. Why does it use more memory that it seems like it should?

I used Ubuntu 20.04 on Windows 10 WLS

JHGrotto
  • 11
  • 1
  • The library functions that you use will affect RSS, since it has to load the library into memory. – Barmar Sep 04 '20 at 03:37
  • Perhaps create a new question along the lines of "Why does my C program use more memory that it seems like it should?" or something like that. The question isn't really about how to read the memory usage figures from the platform -- you're already doing that. Really, your question is about how to interpret them. – Kevin Boone Sep 04 '20 at 14:40
  • Exactly. i dont know what happens behind these values, that's why i put "doubts" on title. But ill make changes to make it more clear. – JHGrotto Sep 04 '20 at 20:44
  • And talking about what sir Barmar said, so, by default, will most of simple programs reach that memory value? – JHGrotto Sep 04 '20 at 20:55
  • Heck, many simple programs will be much larger. Try writing code in Go... – Charles Duffy Sep 04 '20 at 21:26
  • Anyhow, with C -- compiling with `-Os`, telling the compiler you care more about size than speed, will help. Using `tcc` as your compiler will help more. – Charles Duffy Sep 04 '20 at 21:30
  • If it's heap usage in particular you're interested in measuring, valgrind's "massif" tool is handy for that purpose. – Jeremy Friesner Sep 04 '20 at 21:37
  • Opening and reading from a file is likely to bring in more parts of the stdio library, which will affect your resident memory. – Barmar Sep 04 '20 at 21:47
  • There's probably little point in comparing the memory usage of small programs, there's too much noise. You should only consider these statistics useful when they get into multiple megabytes. – Barmar Sep 04 '20 at 21:49
  • Thanks for the replies. Even if there is little point, i wanted to try to get some info. I used valgrind's massif, with `--stacks=yes` and got 7kB as the peak memory, even when the program was empty. I used `$size ./myProgram` when executing, and got some info about the code segment too. – JHGrotto Sep 06 '20 at 02:52

0 Answers0