User Tools

How to Add Swap Space On Linux systems

First, create a file that will be used as swap:

fallocate -l 10G /swapfile

If the fallocate utility is not present on your system, or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:

dd if=/dev/zero of=/swapfile bs=1024 count=10485760

Set the file permissions to 600 to prevent regular users to write and read the file:

chmod 600 /swapfile

Create a Linux swap area on the file:

mkswap /swapfile

Activate the swap file by running the following command:

swapon /swapfile

To make the change permanent open the /etc/fstab file:

nano /etc/fstab

/swapfile swap swap defaults 0 0

Verify that the swap is active by using either the swapon or the free command, as shown below:

swapon --show

Tuning your Swap Settings

There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.

Adjusting the Swappiness Property The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.

With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Remember, interactions with the swap file are “expensive” in that they take a lot longer than interactions with RAM and they can cause a significant reduction in performance. Telling the system not to rely on the swap 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 RAM space free. Depending on your applications’ memory profile or what you are using your server for, this might be better in some cases.

We can see the current swappiness value by typing:

cat /proc/sys/vm/swappiness

For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might 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:

sysctl vm.swappiness=10

This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:

nano /etc/sysctl.conf
At the bottom, you can add:
vm.swappiness=10

Save and close the file when you are finished.

Adjusting the Cache Pressure Setting Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will choose to cache inode and dentry information over other data.

Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache. You can see the current value by querying the proc filesystem again:

cat /proc/sys/vm/vfs_cache_pressure

As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:

sysctl 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:

nano /etc/sysctl.conf
At the bottom, add the line that specifies your new value:
vm.vfs_cache_pressure=50
Save and close the file when you are finished.

Source: How to Add Swap Space