Content:
In Linux, swap space acts as an overflow area for your system’s RAM. When physical memory is full, the kernel moves inactive pages of memory to the swap area, freeing up RAM for active processes.
Historically, Linux systems often relied on swap partitions, but modern distributions favour swap files due to their flexibility and ease of management.
Why Linux Switched to Swap Files
Swap partitions are dedicated disk areas reserved for swapping. While effective, they are inflexible – you must allocate their size during partitioning, and resizing requires complex operations or even data loss. Swap files are regular files that can reside on any file system. You can create, resize, or delete them without altering your disk layout, making them easier to work with. The flexibility opens the door to dynamic system management, with expanding and shrinking swap files based on the needs of the system.
It’s also worth mentioning that Windows uses swap files, rather than swap partitions.
Checking for Swap Files
Depending on the distribution you’re using, you may already be using a swap file. Before looking to create a new one, it’s a good idea to check if any are currently in use.
swapon --show
If available, a list of active swap files will be returned. If there is no output, your system is not currently using a swap file.
Creating a Swap File
Before creating the swap file, make sure you have enough storage space to contain the file. Also, consider the speed and usage of the storage that will contain the swap file. If possible, put it on your fastest storage device. If you make heavy use of a particular device, you might get better performance if the swap file is on a secondary drive.
You’ll also need to decide the size of the swap file you want to create. General guidance suggests to allocate twice your RAM size, though with increasing RAM sizes this is becoming increasingly infeasible. We tend to go for a relatively meagre 2GB across all systems, to avoid taking too much storage space.
You can put the file anywhere on your system, but for these code examples, we’ll be using /swapfile
.
fallocate -l 2GB /swapfile
Replace 2GB
with the size you’ve chosen. MB
values are also supported.
If fallocate
is not available on your system, you can instead accomplish the same result using dd
.
dd if=/dev/zero of=/swapfile bs=1M count=2048
In this example, the size is specified by multiplying the block size (bs
) parameter by the count
parameter. With a block size of 1MB
, 2GB
requires a count value of 2048.
Setting Swap File Permissions
As a system file, the permissions of the swap file need to be set to limit access to other users.
chmod 600 /swapfile
Formatting the Swap File
While the file has been created, it needs to be formatted as a swap file before it can be used as one. This can be done using the mkswap
utility.
mkswap /swapfile
Mounting the Swap File
For testing purposes, you can mount the swap file using swapon
.
swapon /swapfile
Verify that the swap file is now listed.
swapon --show
The system should show an output similar to the one below.
NAME TYPE SIZE USED PRIO
/swapfile file 2G 153.3M -2
To ensure the swap file is mounted on boot, you will need to add it to /etc/fstab
. Open /etc/fstab in a text editor, and add the following to the end of the file.
/swapfile none swap sw 0 0
When rebooting the system, you should now see that the swap file is mounted automatically. Just remember to remove this line if you ever delete the swap file, to prevent boot issues.
Resizing an Existing Swap File
Resizing a swap file is a very similar process to creating a new one.
First, you’ll need to make sure the swap file is not currently in use.
swapoff /swapfile
Run fallocate
with the new size on the existing file.
fallocate -l 4G /swapfile
Use mkswap to format the newly enlarged file.
mkswap /swapfile
It can now be re-enabled.
swapon /swapfile
The swap file will now be the new enlarged size. If you auto-mount your partition, you don’t need to change anything in /etc/fstab
– the newly enlarged file will automatically be mounted on the next boot.
Conclusion
Swap files are a flexible and convenient way to manage virtual memory in Linux. They allow you to optimise system performance without the rigid constraints of swap partitions. By following these steps, you can easily create a swap file tailored to the needs of your system.