2

I want to know which files from /proc directory and which fields of these files I need to calculate the memory usage of a given pid. I've been using the "stat" file and the "vsize" parameter that is in this file but it isn't a good calculation. Anyone knows a better formula for this? Thanks, Ana.

  • 3
    You should define precisely what memory usage means to you. – Basile Starynkevitch Aug 18 '15 at 17:06
7

Indeed you need to use /proc/; so read carefully proc(5).

For process 1234 you want to read /proc/1234/maps (or /proc/1234/smaps) to get the address space, and to read /proc/1234/status & /proc/1234/statm

For your own process (programmatically) use /proc/self/maps, /proc/self/status, /proc/self/statm

Notice that memory usage is a very ambiguous term on Linux. How would you count a file segment mmap-ed by two processes? See mmap(2) & getrusage(2)

Try cat /proc/self/maps, cat /proc/$$/maps in a terminal. Read wikipages on address space, virtual memory, page cache, ASLR, ELF, RSS, working set ...

  • Thanks Basile. I want to calculate a percentage of memory usage. As command "top" calculates. I am coding in C so I will obtain the parameters with fscanf and I will use the formula that I want then. Thanks. – Fran Rios Aug 18 '15 at 17:24
  • 4
    Since top is a free software, you could study its source code and understand what it is calculating. My point is that memory usage is ambiguous and you should define what you want. – Basile Starynkevitch Aug 18 '15 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.