Memory usage of a given process (using Linux proc filesystem)

memoryprocprocess

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.

Best Answer

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 ...

Related Question