Even if you have sufficient memory in your server, it’s still a good idea to have a swap file. In order to reduce disk IO, some cloud providers include one as standard like AWS and some don’t – so you may need to add your own.
This gives you the ability to increase the amount of information that your server can keep in its working memory, with some caveats. Reading from and writing to swap is slower than using memory, but it can provide a good safety net for when your server is low on memory.
Without swap, a server that runs out of memory may start killing applications to free up memory, or even crash. This can cause you to lose unsaved data or experience downtime. To ensure reliable data access, some applications require swap to function.
Check the System for Swap Information
Before we begin, we should take a look at our server’s storage to see if we already have some swap space available. While we can have multiple swap files or swap partitions, one should generally be enough.
We can see if the system has any configured swap by using
swapon, a general-purpose swap utility. With the
swapon will display a summary of swap usage and availability on our storage device:
If the output is empty, it means that your system does not have swap space enabled.
Otherwise if you get something like below, you already have swap enabled on your machine.
NAME TYPE SIZE USED PRIO /dev/dm-1 partition 1.5G 0B -1
Another way of checking for swap space is with the
free utility, which shows us the system’s overall memory usage. We can see our current memory and swap usage (in megabytes) by typing:
total used free shared buffers cached Mem: 4953 314 4638 9 9 108 -/+ buffers/cache: 196 4756 Swap: 0 0 4095
As you can see, our total swap space in the system is 0. This matches what we saw with
Check Available Storage Space
The typical way of allocating space for swap is to use a separate partition that is dedicated to the task. However, altering the partition scheme is not always possible due to hardware or software constraints. Fortunately, we can just as easily create a swap file that resides on an existing partition.
Before we do this, we should be aware of our current drive usage. We can get this information by typing:
Filesystem Size Used Avail Use% Mounted on /dev/vda1 80G 1.5G 86G 3% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 8.3M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
-h flag simply tells
dh to output drive information in a human-friendly reading format. For example, instead of outputting the raw number of memory blocks in a partition,
df -h will tell us the space usage and availability in M (for megabytes) or G (for gigabytes).
As you can see on the first line, our storage partition has 86 gigabytes available, so we have quite a bit of space to work with. Keep in mind that this is on a fresh, medium-sized VPS instance, so your actual usage might be very different.
Although there are many opinions about the appropriate size of a swap space, it really depends on your application requirements and your personal preferences. Generally, an amount equal to or double the amount of memory on your system is a good starting point.
Since my system has 4 gigabytes of memory, and doubling that would take a larger chunk from my storage space than I am willing to part with, I will create a swap space of 4 gigabytes to match my system’s memory.
Create a Swap File
Now that we know our available storage space, we can go about creating a swap file within our filesystem. The user you are logged in as must have sudo privileges to be able to activate swap.
We will create a file called
swapfile in our root (
/) directory, though you can name the file something else if you prefer. The file must allocate the amount of space that we want for our swap file, and it should be created in one contiguous block.
The best way to do this is to use the
dd utility. This command will create a 4 gigabyte file:
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB
After entering your password to authorize
sudo privileges, the swap file will be created. This can take a few moments, then the prompt will be returned to you. We can verify that the correct amount of space was reserved for swap by using
ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Oct 30 11:00 /swapfile
As you can see, our swap file was created with the correct amount of space set aside.
Enable a Swap File
Right now, our file is created, but our system does not know that this is supposed to be used for swap. We need to tell our system to format this file as swap and then enable it.
Before we do that, we should adjust the permissions on our swap file so that it isn’t readable by anyone besides the root account. Allowing other users to read or write to this file would be a huge security risk. We can lock down the permissions with
sudo chmod 600 /swapfile
This will restrict both read and write permissions to the root account only. We can verify that the swap file has the correct permissions by using
ls -lh again:
ls -lh /swapfile
-rw------- 1 root root 4.0G Oct 30 11:00 /swapfile
Now that our swap file is more secure, we can tell our system to set up the swap space for use by typing:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB no label, UUID=b99230bb-21af-47bc-8c37-de41129c39bf
Our swap file is now ready to be used as a swap space. We can begin using it by typing:
sudo swapon /swapfile
To verify that the procedure was successful, we can check whether our system reports swap space now:
Filename Type Size Used Priority /swapfile file 4194300 0 -1
This output confirms that we have a new swap file. We can use the
free utility again to corroborate our findings:
total used free shared buffers cached Mem: 4953 314 4638 9 9 108 -/+ buffers/cache: 196 4756 Swap: 4095 0 4095
Our swap has been set up successfully, and our operating system will begin to use it as needed.
Make the Swap File Permanent
Our swap file is enabled at the moment, but when we reboot, the server will not automatically enable the file for use. We can change that by modifying the
fstab file, which is a table that manages filesystems and partitions.
Edit the file with
sudo privileges in your text editor:
sudo nano /etc/fstab
At the bottom of the file, you need to add a line that will tell the operating system to automatically use the swap file that you created:
/swapfile swap swap sw 0 0
When you are finished adding the line, you can save and close the file. The server will check this file on each bootup, so the swap file will be ready for use from now on.
You’re now all setup with your swap file.
Tweak Your Swap Settings (Optional)
There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap. These configurations are optional in most cases, and the changes that you make will depend on your application needs and your personal preference.
swappiness parameter determines how often your system swaps data out of memory to the swap space. This is a value between 0 and 100 that represents the percentage of memory usage that will trigger the use of swap.
With values close to zero, the system will not swap data to the drive unless absolutely necessary. Remember, interactions with the swap file are “expensive” in that they are a lot slower than interactions with memory, and this difference in read and write speed can cause a significant reduction in an application’s performance. Telling the system not to rely on the swap as much will generally make your system faster.
Values that are closer to 100 will try to put more data into swap in an effort to keep more memory free. Depending on your applications’ memory profile, or what you are using your server for, this might be the better choice in some cases.
We can see the current swappiness value by reading the
swappiness configuration file:
CentOS 7 defaults to a swappiness setting of 30, which is a fair middle ground for most desktops and local servers. For a VPS system, we’d probably want to move it closer to 0.
We can set the swappiness to a different value by using the
sysctl command. For instance, to set the swappiness to 10, we could type:
sudo sysctl vm.swappiness=10
vm.swappiness = 10
This setting will persist until the next reboot. To make the setting persist between reboots, we can add the outputted line to our
sysctl configuration file:
sudo nano /etc/sysctl.conf
Add your swappiness setting to the bottom of the file:
vm.swappiness = 10
When you are finished adding the line, you can save and close the file. The server will now automatically set the swappiness to the value you declared on each bootup.
Another related value that you might want to modify is the
vfs_cache_pressure. This setting affects the storage of special filesystem metadata entries. Constantly reading and refreshing this information is generally very costly, so storing it on the cache for longer is excellent for your system’s performance.
You can see the current value of this cache pressure by querying the
proc filesystem again:
As it is currently configured, our system removes inode information from the cache far too quickly. We can set this to a more conservative setting, like 50, by using
sudo sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50
Again, this is only valid for our current session. We can change that by adding it to our configuration file, like we did with our swappiness setting:
sudo nano /etc/sysctl.conf
At the bottom, add the line that specifies your new value:
vm.vfs_cache_pressure = 50
When you are finished adding the line, you can save and close the file. The server will now automatically set the cache pressure to the value you declared on each bootup.