What command can be used to force release everything in swap partition back to memory?

From this Ask Ubuntu question:

You can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

Thus:

$ free -tm
...
Swap:         6439        196       6243
...
$ sudo swapoff -a
$ sudo swapon -a
$ free -tm
...
Swap:         6439          0       6439
...

As noted in a comment, if you don't have enough memory, swapoff will result in "out of memory" errors and on the kernel killing processes to recover RAM.


As noted, simply deactivating all swap will cause the kernel to start killing things if it doesn't have enough free memory. If you wish to avoid that, create a second set of swap first. Then:

swapon /second/swap/device && swapoff /first/swap/device 
swapon /first/swap/device && swapoff /second/swap/device

This will still swap in everything, but if there's not enough space it'll get shifted to the second swap device instead of randomly killing things. Then just shift it all back.

Tags:

Linux

Swap