What is swap space?
Is there a recommended size for the swap space?
Are there advantages to using files as swap space instead of disk partitions?
How can I get information about my computer's memory usage?
Which tool should I use to create the file for this purpose?
How do I create a swap file?
How do I delete a swap file?
How do I set priorities if I have more than one swap space?
How do I make the system use more or less the swap space (swappiness), in contrast to the main memory (RAM)?
1 Answers
1. What is swap space
Swap means change, commute, exchange, shift, substitute, switch, trade. (Source: Merriam-Webster Dictionary)
Swap space (file or partition) is a type of virtual memory, an extension to the real memory (RAM). It is located on the computer's storage device (HD or SSD). Since data transfer on HD and SSD is slower than on RAM, the operating system (OS) only resorts to its use when running out of RAM. Run out of memory will cause the computer to freeze and crash.
2. Swap size
The size to be allocated (reserved) as swap depends basically on 4 aspects:
- how much RAM the computer has.
- how much RAM the OS and the apps running demand.
- how much space the computer's HD or SSD has.
- if you hibernate the computer.
Twice the size of the RAM is a general recommendation.
3. Advantages of files over partitions
Files are better for maintenance purposes. They can easily be resized and reallocated. Partitions, on the other hand, can be difficult, or even impossible, to do so.
4. Information retrieval
Show total memory information in human-readable format (-h):
free -h
Show swap file/partition information:
sudo swapon
A swap partition is usually /dev/sda3
; while a swap file is usually placed on the root (/) and named as 'swapfile' (/swapfile
).
5. File creation tool
dd: Utility for copying and converting files. It fills the allocated space with empty blocks. Creates contiguous files.
fallocate: "Used to manipulate the allocated disk space for a file" (manpage). Allocates space to a file, but does not fill the space with blocks. Creates non-contiguous (sparse) files.
The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the file system. This is a problem with files with holes or with copy-on-write files on file systems like Btrfs. Commands like cp(1) or truncate(1) create files with holes. These files will be rejected by swapon. Preallocated files created by fallocate may be interpreted as files with holes too depending on the filesystem. Preallocated swap files are supported on XFS since Linux 4.18. The most portable solution to create a swap file is to use dd and /dev/zero.
(Source: swapon manpage)
dd is the most recommended method to create swap files, since this special type of file cannot contain holes.
References:
- fallocate vs dd for swapfile
- What are the holes in files created with fallocate
- What is a sparse file and why do we need it
- Debian Wiki: Swap
- Arch Wiki: Swap
6. Process of creating a swap file
a) Create the file (8GB).
sudo dd if=/dev/zero of=/swapfile bs=1MiB count=$((8*1024))
Parameters:
- if = input file (standard input)
- of = output file
- bs = block size
- count = number of blocks
b) Restrict access permision
Grant read and write permission to root-user only (file owner).
sudo chmod 600 /swapfile
c) Make the file swappable
sudo mkswap /swapfile
d) Turn on the swap file to the system
sudo swapon /swapfile
e) Make the change permanent
Add the following line to the end of the File System Table (fstab):
/swapfile none swap sw 0 0
sudo nano /etc/fstab
Parameters:
- File System = path and name of swap file (
/swapfile
) - Mount Point = mounted inside the file system (
none
) - Type = indicate this is a swap file (
swap
) - Option = option for swap file (
sw
) - Dump = irrelevant for this case (
0
) - Pass = irrelevant for this case (
0
)
7. Deleting a swap file
a) Turn off the swap file
sudo swapoff /swapfile
b) Remove the swap file
sudo rm /swapfile
c) Make the change permanent
Comment or remove the corresponding entry onto the fstab
file.
sudo nano /etc/fstab
If you want to delete a swap partition, proceed as indicated for deleting a swap file, except for the removing step, which will need to be realized using a tool like fdisk, parted, gparted, gnome disks, or something similar.
If you want to change the swap file size, turn it off (
swapoff
), overwrite it by creating a new one with the same name and the new size, make it swappable (mkswap
), and turn it on (swapon
). If the same name is used, thefstab
won't need to be updated.
8. Priorities for swap spaces
(If you have only one swap space, ignore this section.)
If you have more than one swap space (partition or file), you might want to indicate to the kernel which one has more priority and, therefore, should be used in advance.
Priority level ranges from 0 to 32767. The higher the number, the higher the priority.
The default priority is -2. This is set automatically if you don't specify one.
In case more than one swap space has the same priority, they will be used alternately.
The priority level should be set to the corresponding line on the
fstab
file
/swapfile none swap sw,pri=5 0 0
Where 'pri' stands for 'priority' and '5' is the level used for this example.
9. Swappiness
The swappiness parameter controls the tendency of the kernel to move content out of physical memory onto the swap disk (Ubuntu Community Help Wiki: Swap Faq). More specificaly, is a property for the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100, inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space (Red Hat Customer Portal - Knowledgebase).
The default swappiness value is 60. For most desktops this should be fine. Servers however, may use lower values, like 10 or even 1, depending of its purpose.
A value of 0 means "use the swap space as a very last resort".
With a value of 100, the swap space will be used as RAM.
Check swappiness value
cat /proc/sys/vm/swappiness
Make a transient change
Change the value to 10 temporarily. It will be reset along with the session restart.
sudo sysctl vm.swappiness=10
Make a persistent change
To make a persistent change, edit the file below with the line 'vm.swappiness=10'
sudo nano /etc/sysctl.conf
-
Re. swappiness, please read the relevant kernel documentation. – Stephen Kitt Jul 26 '21 at 07:02
-
@StephenKitt Thanks for the feedback! I suppose you are talking about the definition of swappiness. I will reformulate it. Just red your answer about the topic (https://unix.stackexchange.com/a/493456/134653). The reference to which you pointed here (this page) is from kernel version 2.6.29, more than 10 years old. Do you have newer documentation about swappiness? – Felipe G. M. Maia Jul 26 '21 at 22:56
-
The documentation is current, ignore the introductory comments about 2.6.29. – Stephen Kitt Jul 27 '21 at 06:14
-
@StephenKitt It seems there are changes with newer versions of the kernel, regarding swappiness range (0-100 instead of 0-200); and regarding value zero disabling the swap, instead of zero as the maximum level for swap avoidance, but still enebled. – Felipe G. M. Maia Jul 27 '21 at 22:35