4

I'm trying to make sense of this free -m output:

             total       used       free     shared    buffers     cached
Mem:          7971       7608        363          1         17       4703
-/+ buffers/cache:       2887       5084
Swap:          975        967          8

So swap is used even though only 3GB out of 8GB RAM is actually used by processes? Why does this happen, shouldn't the OS use less memory for fs cache instead of using swap?

OS is Ubuntu 14.04.3 LTS with default settings.

Fluffy
  • 2,077

2 Answers2

2

shouldn't the OS use less memory for fs cache instead of using swap?

I guess the OS was short on RAM once in the past and swapped out some memory.

The shortage is now over but the pages left in the swap are not in use so it is better for the OS to keep RAM for "hot" data including fs cache instead of these unused pages.

jlliagre
  • 61,204
2

Linux is quite aggressive in swapping out pages to disk.

This doesn't mean that there is no copy of the page in RAM any more, but it can still be duplicated in the swap cache (a RAM section which caches pages written to the disk). The advantage of this is, that pages in RAM can be freed immediately in case some process needs memory. You can check the amount by looking at:

grep SwapCached /proc/meminfo

Also if pages of a process are swapped out, it doesn't mean that those pages are ever needed by the process, but can be from some linked library, which functions/data segments are never used.

Have a look at https://serverfault.com/questions/550793/how-to-find-what-is-using-linux-swap-or-what-is-in-the-swap for a introduction to the topic, there is also a link to scripts to see what is swapped out.

Virtual memory management can be confusing at times, as a rule of thumb:

Usually you shouldn't care about how big the swap is, but how much gets swapped in and out on normal activity (check vmstat).

Nemo
  • 574
Thomas
  • 486