4

When I run free -h, I get:

             total       used       free     shared    buffers     cached
Mem:          251G       208G        43G       179G       144M       190G
-/+ buffers/cache:        17G       233G
Swap:         5.6G       5.6G       1.1M

How can shared memory(179G) + cached memory(190G) exceed total memory (251G) ? Can memory be count as both of cached memory and shared memory?

  • 2
    Welcome to U&L! Yes, apparently it can. See, for example, this explanation from Red Hat. See the line: "Cached: Memory in the pagecache (Diskcache and Shared Memory)" – fra-san Nov 29 '18 at 12:47
  • I voted to re-open this question because the answers to the duplicate target don't answer this question or explain how "shared memory is also counted as cached" -- as pointed out in sourcejedi's answer. – Anthony Geoghegan Jul 19 '19 at 11:56

1 Answers1

8

All shared memory is also counted as cached.

shared memory is implemented using tmpfs internally. tmpfs is implemented as a thin wrapper for the page cache, just without having any backing store (except that tmpfs is swappable).


man free does not explain this. At least on my system (provided by procps-ng, last updated 2016-06-03). Sorry. All man free tells you is that cache is taken from Cached in /proc/meminfo. If you read man proc (from man-pages, updated 2017-09-15), it has documentation for the fields in the meminfo file, but it still fails to inform you that Cached includes Shmem.

You can see this with an experiment. While you have free memory (not available), you can create a file in a tmpfs, for example:

dd bs=1M count=100 < /dev/zero > /dev/shm/test.tmp

The result of this is that both the shared and cached figures in free -m increase by 100.

If you only have available memory, and do not have enough free memory to test this properly, you can create free memory by dropping as much page cache as possible, by running echo 1 | sudo tee /proc/sys/vm/drop_caches. Of course dropping page cache can be very bad for performance. Do not do this on a real server :-).

sourcejedi
  • 50,249