Create and remove swap files in Ubuntu
There are two basic types of memory that computers use: Random Access Memory (RAM) and swap files (swap space, swap partitions).
RAM is dedicated hardware inside a computer that is used to store data for running programs and services. It is very fast memory and volatile (not saved when you turn off the computer or it crashes).
Swap space is allocated from a computer's Hard Disk Drive (HDD) or Solid State Drive (SSD) (including NVMe SSDs), and is used as a substitute for RAM. By comparison, disk speed, even NVMe and SSDs, is orders of magnitude SLOWER than RAM.
Swap was commonly used back in the days when RAM capacity was very limited and very expensive compared to hard drive space. So you accepted the massive performance drop when you run workloads that required memory greater than your RAM capacity.
In modern day computing, RAM is relatively cheap, and with servers running websites and applications with hundreds/thousands/millions of end users, the performance degradation of swap space could cost businesses a lot of lost revenue, and produce a negative user experience. In fact, going into swap usage commonly crashes servers, or it performs so slowly, that no services are provided at all.
Recommended Swap Size
Home or workstation users might use swap for features like hibernation (so a larger swap than total RAM capacity) or running large programs, but in a server environment, unless your application specifically requires swap space, then at most, you should only allocate a couple of gigabytes (literally, 2 GB is more than sufficient for Operating System ancillary tasks). In fact, many cloud server providers allocate no swap space at all - modern Operating Systems function perfectly fine without any swap space.
Create a swap file
-
Verify the current swap file size. You can use any of the following commands to determine the size of the existing swap file:
free -m swapon -s swapon -show
Note: If there’s no output or the output is
000
, the swap file might not be configured. -
To create the swap file, run the following command:
dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Where:
- if: input file (will always be the same)
- of: output file (you can name this file as you want)
- bs: block size (keep value at 1024)
- count: amount of blocks to read and write, which helps you determine how
much space you need for the swap file.
Note: Remember that if you require a specific size, you can modify the
count
value by multiplying it by the block size value for the new size (in MB). For example, multiplying the old block size,1024
, by the new size,4096
, results in4194304
for the count. -
Provide the
root
user with read and write permissions for the swap file by running the following command:chmod 600 /swapfile
-
Set up the swap area:
mkswap /swapfile
-
Use a text editor to add the line
/swapfile swap swap defaults 0 0
to the /etc/fstab file. -
Activate swap file (this will also test your fstab to ensure you have the correct syntax)
swapon -a
-
If you do not want your swap file to survive a reboot (but be careful leaving a big file lingering about), you can skip step 5 and step 6, and just use
swapon /swapfile
-
Verify that the new swap file configuration using any of the commands in step 1.
For more information on fstab
, visit: https://wiki.archlinux.org/index.php/Fstab
Remove a swap file
-
Run the following command to reactivate the swap file:
swapoff -v /swapfile
-
Use a text editor to remove the /etc/fstab entry.
-
Run the following command to remove the swapfile:
rm -f /swapfile
Updated 17 days ago