I know there are already stack overflow questions on this, and while I've read a few, I still don't quite understand how to make the calculation for memory usage for a C process.
I read multiple stack overflow questions on the topic:
[1] How to measure actual memory usage of an application or process?
[2] Memory usage of current process in C
[3] Monitor memory usage of child process
I also read these links:
[2] https://locklessinc.com/articles/memory_usage/
From this, I gathered the following points:
I can't just use
VMSize
, as this is is the total amount of virtual memory allocated to the process/program. The program may not use all of it. Is this a correct assumption?Since a program/process consists of the stack + heap + shared libraries + code segments, the calculation is
VMData (Data + Uninitialized Data + Heap Segments sizes) + VmStk (Stack Segment size)
. Am I correct in saying this? Do I also need to addVmExe (Text Segment size)
and/orVmLib (Shared Library usage size)
?A couple of suggestions I found somewhere was to use
getrusage()
(https://linux.die.net/man/2/getrusage), or to useVmRSS
(which, apparently, is how much memory in RAM is occupied by the process). Are these also viable suggestions? I've noticed that using/proc/[pid]/status
is more commonly used. Why use the latter suggestion over the former ones?
Edit: This is for a program I am writing that runs fork()
and exec()
to run multiple processes. I am just suppose to report memory usage statistics of each process.
Thank you!