linuxmemory-managementcommand-linesystem-calls

Finding total RAM consumption of process, including swap


Using /usr/bin/time -v <program name> I can find the maximum amount of physical memory used by a process by reading the Maximum resident set size number.

But I'm currently running a program that needs to make use of swap. How do I get the total amount of memory, including swapped out pages, that a process uses?


Solution

  • The /proc/<pid>/status file for process <pid> contains several counters for different kinds of memory used by a process. The VmSize is the total virtual address space used. The VmRSS is the current resident size (and VmHWM is the "high water mark" RSS, which I think is the value you're currently getting out of time). The VmSwap line should contain the amount of swap space currently used by the process.

    Beware that many of these counters can include shared pages (e.g., if you have several copies of the same C program running, they'll share the text pages and the C Library text pages, but VmRSS will account for them in each process.) See https://ewx.livejournal.com/579283.html for more discussion of these limitations.

    See https://www.kernel.org/doc/Documentation/filesystems/proc.txt (specifically, Table 1-2) for documentation all of the fields in here.

    Oh, and you can only access the /proc/<pid>/ directory while process <pid> is running.