Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.
Swap space can be a dedicated swap partition, a swap file, or a combination of swap partitions and swap files. On our cloud servers, we don’t have swap activated by default because we have very generous memory in all of our servers.
You can add swap to your server if you require, however, we would recommend that you upgrade your server if you need additional resources. The upgrade process is seamless and only requires a short server restart to adjust your disk storage.
Check Swap Status
It’s good practice to make sure that there isn’t a swap partition or file that was activated previously. Some specific applications such as cPanel create a swap file on your behalf, so it’s important to check that there isn’t an existing one. You can run this command to view the status of your swap.
# swapon -s
Filename Type Size Used Priority
If you see output similar to the one above with nothing listed under file name, it means that the server has no swap running on it. If you see any entries, it means that there is an existing swap defined on your machine.
Determine Swap File Size
There is usually a lot of different opinions on deciding on the amount of swap to setup on your server, ranging from people saying to use the same amount of memory to those mentioning to use twice the amount of memory. However, we find that it should be decided based on how much disk space and memory you have, so let’s start by checking how much disk space we have.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.8G 36G 5% /
udev 489M 4.0K 489M 1% /dev
tmpfs 199M 244K 199M 1% /run
none 5.0M 8.0K 5.0M 1% /run/lock
none 498M 0 498M 0% /run/shm
/dev/vdb 416K 416K 0 100% /configdrive
We see that we have 36 gigabytes available in our primary file system. This server also has 1GB of memory, therefore, we can comfortably run twice the size of the memory (so 2GB swap in this case). Please note that if you have a large server (8GB or more), you most likely don’t need any more than 8GB of swap. For this example, we’ll decide on a 2GB swap.
Create and Enable Swap
We will need a blank file, the size of our swap. We can use the dd Linux tool to create that blank file. Afterwards, we will update the permissions to secure the swap file. The following command will Let’s also take this moment to appreciate how fast our SSDs write to disk! Feel free to post how fast your server can run this command in the comments.
# dd if=/dev/zero of=/swap bs=1M count=2048 oflag=direct
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 2.40066 s, 894 MB/s
# chown root:root /swap
# chmod 0600 /swap
The command above will write zero’s to a file named /swap, the size of 2048MB (2 gigabytes). The size is determine by multiplying the block size and count (2048 blocks of 1 megabyte = 2048 megabytes). The oflag=direct flag means to write directly to disk and not buffer/cache anything to memory, which means the numbers above are straight-to-disk speeds.
The next step is to configure the file as a swap file and enable it inside the operating system, we can do this with the following two simple commands:
# mkswap /swap
# swapon /swap
When we run the mkswap command, it should output some information about the configuration of the swap file similar to the following:
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=5c9e1ecf-6b5c-4825-918e-a52da8ec0394
Once you’ve ran the above commands, you should be able to check your swap status as we’ve shown above, but this time, you’ll see the new swap file that we created. You can check it the following way and the output should be similar to the one below:
# swapon -s
Filename Type Size Used Priority
/swap file 2097148 0 -1
Swap File Persistence
We have manually enabled the swap file, however, if you reboot the server, you will see that the swap file will disappear. This is due to the fact that it needs to be mounted on each boot. We can do this by adding the following lines to the /etc/fstab file.
# echo "/swap none swap sw 0 0" >> /etc/fstab
Once you add that, when your server boots again, it will automatically mount the swap file we created at /swap as the swap for machine. Your Ubuntu server should now start taking advantage of the swap right away. You can check the status of your swap by typing the following command:
# free -m
total used free shared buffers cached
Mem: 994 119 875 0 27 32
-/+ buffers/cache: 59 935
Swap: 2047 0 2047
As you see, this server has 1 gigabyte of memory, 2 gigabytes of swap, however, as the server does not have any big processes running on it, it is not currently using the swap.