3

What happens if I execute the command

sudo swapoff -a && swapon -a

having RAM full 25GB/32GB and swap full 20GB/32GB? 25+20 = 45 > 32 so there's no way RAM can fit all operating data. On the other hand, it's only for a few seconds before swap is turned on again.

The goal of it is that the performance of my laptop is better when I restart swap from time to time and I want to use crontab to do it once a day. I don't want to worry what happens if there's not enough memory. I don't care if a swap file is created temporarily to contain the content of the swap partition. I don't want the kernel to kill any processes. Can I somehow instruct kernel that if the RAM capacity is insufficient, it should create a swap file temporarily for swapoff + swapon?

1 Answers1

0

swapoff will fail with an error if there are not sufficient free pages in RAM for the contents of the swap file

From man

EXIT STATUS
   swapoff has the following exit status values since v2.36:

0 success

2 system has insufficient memory to stop swapping (OOM)

4 swapoff syscall failed for another reason

8 non-swapoff syscall system error (out of memory, ...)

16 usage or syntax error

32 all swapoff failed on --all

64 some swapoff succeeded on --all

The command swapoff --all returns 0 (all succeeded), 32 (all failed), or 64 (some failed, some succeeded).

  • The old versions before v2.36 has no documented exit status, 0 means success in all versions.