Why is swap used when a lot of memory is still free?
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.
Related videos on Youtube
Gregg Leventhal
Updated on September 18, 2022Comments
-
Gregg Leventhal over 1 year
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
-
Admin about 10 yearsThat'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.
-
Admin about 10 yearsInactive 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)
-
Admin about 10 yearsIf 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?
-
-
Gregg Leventhal about 10 yearsWhat 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?
-
Graeme about 10 years@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. -
antonone over 4 yearsFull path is
/proc/sys/vm/swappiness
(i.e. default value on openSUSE is60
)