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 -s
flag, swapon
will display a summary of swap usage and availability on our storage device:
swapon -s
If nothing is returned by the command, then the summary was empty and no swap file exists.
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:
[root@clusterserver3 ~]# free -m
total used free shared buff/cache available
Mem: 3704 104 3464 8 135 3433
Swap: 3967 0 3967
[root@clusterserver3 ~]#
create a Swap File
Now that we know our available storage space, we can go about creating a swap file within our filesystem. 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.
The fastest and easiest way to create a swap file is by using fallocate
. This command creates a file of a preallocated size instantly. We can create a 4 gigabyte file by typing:
fallocate -l 4G /swapfile
[root@clusterserver3 /]# ls -ltr /swapfile
-rw-r--r-- 1 root root 4294967296 Jan 2 00:34 /swapfile
[root@clusterserver3 /]#
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 chmod
:
chmod 600 /swapfile
[root@clusterserver3 /]# mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=75cdf9e6-f034-4aed-845e-59035534eeaf
[root@clusterserver3 /]#
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:
vi /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
weak 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
The 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:
cat /proc/sys/vm/swappiness
30
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.
Cache Pressure
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:
cat /proc/sys/vm/vfs_cache_pressure
100
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 sysctl
:
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
Recent Comments