1

I'm trying to calculate all of the swap memory usage by a linux system and I see a difference between the sum of all swap usage by processes (calculated using the below line) vs free or meminfo output.

Calculated total swap usage by:

for file in /proc/*/status ; do grep VmSwap $file; done | grep kB | grep -v "0 kB"

It seems there are other factors that's contributing to the Swap Used.

1
  • 1
    Can you tell us what you got and what you expected (and why you expected it)? Commented Sep 11, 2017 at 16:38

1 Answer 1

3

There are at least two swap users you missed:

  1. tmpfs: Any mounted tmpfs instances (usually at minimum /tmp and /run, with other directories potentially included) is backed by swap space. Normally, data in tmpfs lives in the kernel's page cache. When the system starts to run low on memory though, this data may get forced out to swap so that the data is retained while still freeing up space for other things. As far as I know, tmpfs usage does not get accounted to any process when calculating swap usage (from a practical perspective, you can't account it to any process just like you can't account any given file to a specific process). The same also goes for hugetlbfs instances (hugetlbfs is a special type of tmpfs).
  2. Shared memory segments: For named POSIX shared memory segments, the data ends up stored in /dev/shm, which is itself a tmpfs mount, and falls under the above . For anonymous shared memory segments, I believe (but I'm not certain, and don't have the patience or skills to try and test) that space usage is also not accounted to a specific process.

There might be other swap users, but those are the only two non-process swap users I know of for certain.

You must log in to answer this question.