Suppose a program asks for some memory, but there is not enough free memory left. There are several different ways Linux could respond. One response is to select some other used memory, which has not been accessed recently, and move this inactive memory to swap.
However, I see many articles and comments that go beyond this. They say even when there is a large amount of free memory, Linux will sometimes decide to write inactive memory to swap. Writing to swap in advance means that when we eventually want to use this memory, we do not have to wait for a disk write. They say this is a deliberate strategy to optimize performance.
Are they right? Or is it a myth? Cite your source(s).
Please understand this question using the following definitions:
- free memory - the "free" memory displayed by the free command. This is the
/proc/meminfois a virtual text file provided by the kernel. See proc(5), or RHEL docs.
- even when there is a large amount of free memory - for the purpose of argument, imagine there is more than 10% free memory.
There are a couple of reports of Linux writing to swap when there is plenty of free memory. "Opportunistic swapping" might explain these reports. At the same time, at least one alternative cause was suggested. As a first step in looking at the possible cause: Does Linux ever perform "opportunistic swapping" as defined above?
Here are some search terms: linux "opportunistic swapping" OR (swap "when the system has nothing better to do" OR "when it has nothing better to do" OR "when the system is idle" OR "during idle time")
In the second-highest result on Google, a StackExchange user asks "Why use swap when there is more than enough free space in RAM?", and copies the results of the
free command showing about 20% free memory. In response to this specific question, I see this answer is highly voted:
Linux starts swapping before the RAM is filled up. This is done to improve performance and responsiveness:
Performance is increased because sometimes RAM is better used for disk cache than to store program memory. So it's better to swap out a program that's been inactive for a while, and instead keep often-used files in cache.
Responsiveness is improved by swapping pages out when the system is idle, rather than when the memory is full and some program is running and requesting more RAM to complete a task.
Swapping does slow the system down, of course — but the alternative to swapping isn't not swapping, it's having more RAM or using less RAM.
The first result on Google has been marked as a duplicate of the question above :-). In this case, the asker copied details showing 7GB
MemFree, out of 16GB. The question has an accepted and upvoted answer of its own:
Swapping only when there is no free memory is only the case if you set
swappinessto 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.
swappinessparameter basically just controls how much it does this.
The other quote does not explicitly claim the swapped data is retained in memory as well. But it seems like you would prefer that approach, if you are swapping even at times when you have 20% free memory, and the reason you are doing so is to improve performance.
As far as I know, Linux does support keeping a copy of the same data in both main memory and swap space.
I also noticed the common claim that "opportunistic swapping" happens "during idle time". I understand it's supposed to help reassure me that this feature is generally good for performance. I don't include this in my definition above, because I think it already has enough details to make a nice clear question. I don't want to make this more complicated than it needs to be.