7

This question already has an answer here:

In the below 2.6.18 Linux Kernel (Red Hat) server, there is a lot of free memory, but I see some swap is used. I always thought of swap as an overflow when memory has been depleted. Why would it swap with about 7GB (50%) free memory? Swappiness is 60 (default).

Meminfo output:

MemTotal:     16436132 kB
MemFree:       7507008 kB
Buffers:        534804 kB
Cached:        2642652 kB
SwapCached:      39084 kB
Active:        6001828 kB
Inactive:      2532028 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:     16436132 kB
LowFree:       7507008 kB
SwapTotal:     2097144 kB
SwapFree:      1990096 kB
Dirty:             236 kB
Writeback:           0 kB
AnonPages:     5353644 kB
Mapped:          45764 kB
Slab:           330660 kB
PageTables:      34020 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:  10315208 kB
Committed_AS: 14836360 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    264660 kB
VmallocChunk: 34359472735 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB

marked as duplicate by slm, Hauke Laging, strugee, Braiam, jasonwryan Apr 5 '14 at 6:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    That's almost no swapping. Also, if you started swapping, the swap is not emptied immediately so perhaps the few Ks you see used are left over from an operation a few seconds or minutes ago that took up more RAM. – terdon Apr 4 '14 at 17:33
  • 2
    Inactive memory is eligible to be swapped out. RAM is a valuable, limited resource, so leaving a chunk unused is best avoided. Using it for fs caching is a better use. (windows does the same thing, 'tho it's a lot more annoying about it... it'll swap before dumping cache) – Ricky Beam Apr 4 '14 at 17:46
  • If the dup from U&L doesn't make it clear take a look at this AU Q&A titled: Why is swap being used even though I have plenty of free RAM? – slm Apr 4 '14 at 20:55
12

Swapping only when there is no free memory is only the case if you set swappiness to 0. Otherwise, during idle time, the kernel will swap memory. In doing this the data is not removed from memory, but rather a copy is made in the swap partition.

This means that, should the situation arise that memory is depleted, it does not have to write to disk then and there. In this case the kernel can just overwrite the memory pages which have already been swapped, for which it knows that it has a copy of the data.

The swappiness parameter basically just controls how much it does this.

  • What determines the data which is written to swap exactly? I am sure there was much more than a few KB of data in memory, but only a very small portion was currently swapped. I assume there is a complex algorithm, but perhaps you can describe high level? Is it pages of memory from applications with a certain process/IO state? – Gregg Leventhal Apr 4 '14 at 18:58
  • @GreggLeventhal TBH, I'm not really sure. AFAIK the amount of access the memory gets (or at least time since the last access) is the main factor in what gets swapped - the idea is only prioritise swapping of data which isn't actually in use. The amount that gets swapped (or at least how often swapping happens) is a function of the memory usage and the swappiness, this much is clear. AFAIK the process is agnostic of the application itself, it only considers what is happening with the memory pages themselves. – Graeme Apr 4 '14 at 19:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.