2

Why is moving from RAM to swap fast and from swap to RAM very slow?

When I run program that needs more memory that is available, I see that the RAM is prety quickly "cleaned" by moving unsused (=the least accessed) pages to swap.

But then when I run

swapoff -a ; swapon -a

it takes really long time (minutes for several GBs of swap) to move the swap back to RAM.

The swap is in addition on SSD. Or do the swapoff -a command some special check, that I can avoid/disable?

2

If possible, you can ask the kernel to not use swap except when all the RAM is used. It is the "vm.swappiness=0" option, to be added in sysctl.conf. You need to reboot to see the effects.

You will not need to clear the swap...

  • 1
    no reboot required. sysctl -p after updating sysctl.conf should suffice. defaults are used at boot until sysctl is called later in the boot process. – Aaron Feb 16 '16 at 21:17
  • I upvoted your answer, but I know this. I am just curious what is going on, as the swap creating is almost instantly and restoring back to RAM takes seconds, sometimes minutes. On powerfull machine with SSD with just a few GBs of swap. – Mvorisek Feb 16 '16 at 23:11
  • @Aaron : of course it is take in real time, but the swap is not cleared when you configure the swappiness. You need to reboot (or umount/mount the swap) to go back to zero swap use. – Dom Feb 17 '16 at 7:17
  • swapoff -a; swapon -a that was being performed will also take care of that. – Aaron Feb 17 '16 at 15:19
  • 1
    @Dom: perhaps read this? Sounds like it's one of those scenarios that Linux isn't being optimized for ... I was looking for a reason as well, when I ran into it just now. – 0xC0000022L Dec 4 '18 at 17:17
0

You should read more about how memory management works in Linux in order to fully understand this.

My guess is that it goes through a lot of the RAM and moves it around to defragment it so it can move the stuff from swap to continuous memory blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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