0

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:

[1] https://www.quora.com/What-is-the-simplest-and-most-accurate-way-to-measure-the-memory-used-by-a-program-in-a-programming-contest-environment

[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 add VmExe (Text Segment size) and/or VmLib (Shared Library usage size)?

  • A couple of suggestions I found somewhere was to use getrusage() (https://linux.die.net/man/2/getrusage), or to use VmRSS (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!

B.M. Corwen
  • 205
  • 3
  • 10
  • First you need to define what you mean by "memory usage". Physical RAM only, or ??? – Chris Dodd Nov 13 '19 at 19:53
  • 2
    “there are already stack overflow questions on this, and while I've read a few” -- it would certainly be helpful to link them to the question. And, in general, as you already discovered, there is no single “memory usage” figure in a modern OS. Read-only ELF sections may be mapped to the same physical RAM, R/W data may be swapped out. If you could state your _ultimate_ problem better, i.e. _why_ do you need the number, that would be helpful: it would show _which_ of the “memory usages” is the right one for your use case. – kkm inactive - support strike Nov 13 '19 at 19:55
  • You should begin by actually specifying what you mean the "memory usage of a process". Once you have a precise definition, finding out the corresponding value will become easy and you will wonder what was it that you thought was hard. – AlexP Nov 13 '19 at 20:01
  • @Chris Dodd @kkm @AlexP I believe what is being asked of me is how much memory is being used by a process, when I check on said process's statistics in the `/proc/[pid]/status` folder. I've also linked the stack overflow questions I looked at previously (edited above), but I mostly looked at the two other links I listed above. – B.M. Corwen Nov 13 '19 at 20:11
  • Those links tell you how to see how much of different classes of memory you are using. So the question becomes, which classes of memory do you care about? – Chris Dodd Nov 14 '19 at 02:49

0 Answers0