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?

mvorisek
  • 485
  • 6
  • 19

2 Answers2

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...

Dom
  • 6,628
  • 1
  • 19
  • 24
  • 2
    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 07: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](https://salticidoftheearth.com/2014/01/09/why-swapoff-is-so-darned-slow/)? 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 04 '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.

Florin Asăvoaie
  • 6,932
  • 22
  • 35