Finding total RAM consumption of process, including swap

41 Views Asked by At

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?

2

There are 2 best solutions below

1
P.T. On BEST ANSWER

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.

0
Thorkil Værge On

Adding to P.T.'s excellent answer: You can also get htop to report this value by going to Setup -> Columns -> Available Columns and then selecting M_SWAP. This value most likely has the same limitations of shared pages that P.T. describes.