10

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

Presume that I have enough memory.

17

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.

  • 3
    Explaining this further, if you don't have enough available RAM swapoff will result in out of memory errors and the kernel will start killing processes to recover some. Use this with caution. – bahamat Aug 14 '12 at 9:15
  • Better hope so; linux behaves pretty hilariously when you run out of memory with no active swap at all... – Shadur Aug 14 '12 at 9:19
1

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.

0

One-liner:

sudo bash -c 'swapoff -a && swapon -a'

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.