When is swap moved back to physical memory?

5,866

Solution 1

On demand. In fact, Linux will slowly transfer physical memory to swap when idle (see: "swappiness").

Solution 2

As others have pointed out, pages will only be copied back into RAM when needed (on demand) instead of taking up RAM that might be better left available for cache/buffers.

The fact that the pages are copied back into RAM, not moved, is important and can lead to confusion if you are not aware of it. The page will not be deallocated from swap unless it is no longer needed at all (i.e. the page is deallocated completely), is changed in RAM (so the copy in swap is no longer correct), or swap is running low (and the on-disk blocks are needed to swap some other pages out). This way if the page needs to be swapped out again in future no disk write is needed as the kernel knows there is already a good copy on disk - this can greatly reduce "thrashing" when available RAM becomes critically low but swap space is not also congested.

You can see how many pages are currently in both RAM and swap from cat /proc/meminfo - the SwapCached line is the amount of data that is in pages that are currently both in RAM and on disk. If you think your current swap use it higher than you expect, check the SwapCached value as this may well explain the discrepancy.

Solution 3

During regular operation data from swap is loaded to memory on demand, as other answered, but there is one more case when this happens: it is when swap space is disabled, provided there is enough physical memory to load whole swap content.

Just do:

swapoff -a

…and all your swap data will 'come back' to the memory. The side effect is that disk buffers/caches may get flushed.

Sometimes it may be desirable to do swapoff -a ; swapon -a, e.g. after some buggy memory-leaking process, before crashed, made more important processes swept out – to make sure any process running in the system is loaded into memory and won't be waiting for the swap in a few minutes.

Share:
5,866

Related videos on Youtube

Ztyx
Author by

Ztyx

Updated on September 17, 2022

Comments

  • Ztyx
    Ztyx almost 2 years

    When is swap moved back to physical memory in Linux? Is it only on demand, ie. when it's needed? Or is swap slowly transfered to physical memory when the computer is not on high load?

  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 14 years
  • Ztyx
    Ztyx about 14 years
    Thanks. A short a concise answer. I like that (even though I appreciate everyone else's effort put into their answers)!
  • Ztyx
    Ztyx about 14 years
    Oh, you learn something good every day! Btw, will ´swapoff -a´ fail is there's not enough physical memory? Is there a risk involved in calling swapoff?
  • Ztyx
    Ztyx about 14 years
    There has been discussions surrounding similar issues before. However, not this specific question. See also serverfault.com/questions/100448/… which deals with SwapCached.
  • Axel
    Axel about 14 years
    I don't even remember writing that answer... I could have probably just linked to (or copy+pasted) it instead of typing the above...
  • Jacek Konieczny
    Jacek Konieczny about 14 years
    It will fail if there is not enough physical memory. That is still a risk swapoff won't fail, but the system will run out of memory soon after the swapoff and the OOM will kill random process (in the worst case the shell about to call swapon or the swapon command). In practice it is very unlikely – a lot of memory is used by caches before swapoff is called so it should be enough of RAM for both commands to run.
  • user228505
    user228505 almost 5 years
    This answer is wrong. The question is when memory is paged in, not paged out. This is the opposite. On a page fault the kernel will reload the page from disk/swap. So mostly on demand. You could access all virtual memory of a process to trigger page faults. As a result the pages would be copied back to RAM.