Search notes:

Linux swap space

Swap space provides disk storage to which the kernel can move memory pages when available RAM memory becomes exhausted. Swap space is also needed to store the entire contents of RAM when hibernating a system.

Size and location of swap space

There is no perfect size of swap space.
Generally, the size of swap space is recommended to be twice the amount of available RAM.
Ideally, swap space should be allocated on SSDs rather than rotational disks.
It should be taken into consideration to allocate a swap space on each available physical disk. This enables them to be utilized for parallel read and write operations.
It seems that at least with BTRFS, swap space should be placed on a subvolume (see also here).

Forms of swap space

There are two forms of swap space:

Displaying swap space

Swap usage can be displayed with
$ swapon -s
This is equivalent to
$ cat /proc/swaps
The reported size of the two above commands is measured in kilobytes.
The shell command free shows the amount of free and used memory and swap space.
Alternatively, there is also
$ grep Swap /proc/meminfo

Swappiness

The kernel parameter swappiness defines how quickly (seemingly) unused memory gets swapped out.
The parameter has a value between 0 and 100.
0 indicates that the memory should not be swapped out very quickly; 100 that it should get swapped out as soon as possible.
The default is 60.
The value can be queried with
cat /proc/sys/vm/swappiness
The value can be changed with
echo 42 > /proc/sys/vm/swappiness
The value can be permanently changed in /etc/sysctl.conf.

Swap files

The Kernel expects to be able to write to the swap file directly, without the assistance of the filesystem.
This is a problem on files with holes or on copy-on-write files on filesystems like Btrfs.
Preallocated files created by fallocate may be interpreted as files with holes too depending of the filesystem.
Preallocated swap files are supported on XFS since Linux 4.18.

See also

memory
mkswap intializes a swap partition , swapon activates swap space.

Index