Jump to content

How to modify SWAP memory


Recommended Posts

SWAP memory is used to help RAM, once it cannot store any more data. The data which cannot be stored in RAM is then stored to SWAP memory in the hard disk. In this article, we will provide the basic steps on how to modify and increase SWAP memory in our KVM virtualization Linux VPS packages.
 
In general, there are different opinions on how much SWAP memory your server should have. It could be half of your RAM, the same amount or even more than RAM. In our article, we will assign 6 GB of SWAP to the server.

 

Allocating SWAP Memory

First thing you should do is to check if there is no SWAP memory in use on your server with the following command:
free -h

Your results will be printed in two lines: "Mem", "Swap", which will indicate, what the exact amount of RAM and SWAP memory is on the KVM server. The "Swap" line should only contain zeros. 

 
With the following command, we will allocate 6 GB of disk space for our SWAP memory:
fallocate -l 6G /swapfile

 

You can check if your SWAP memory was assigned with this command:
ls -lh /swapfile

 

At first, your SWAP may not be allocated due to permission issue and you might be seeing this message:

 

-rw-r--r-- 1 root root 6.0G Dec 5 14:32 /swapfile

 

This would suggest we have to make additional changes, first of which should be changing the permission of the swapfile:
chmod 600 /swapfile

 

After the change you can check the file permissions again:
ls -lh /swapfile

 

The results should change as well, comparing to the previous above:
-rw------- 1 root root 6.0G Dec  5 14:36 /swapfile
 
Now set the swapfile as Linux swap area:
mkswap /swapfile
Finally, enable SWAP usage:
swapon /swapfile
We can now check if the SWAP memory was allocated correctly:
free -h

Your results will print two lines again, just this time, you will see a line "Swap" having a variable of 6 GB.

 

Additional Options

In general, your SWAP memory allocation may stop working after you reboot the KVM server, so in order to save these changes permanently, we have to edit /etc/fstab. But first, we would recommend making a backup of this file:
cp /etc/fstab /etc/fstab.old

 

Once the backup is done, we have to make the changes to the actual file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

 

There are few extra options for SWAP memory, which we would like to address as well:
 
Option - swappiness, is used to describe when the system will move data to the SWAP file. The option may have a value from 0 to 100. Closer to 0, means that your data will be moved to SWAP only when it will be necessary.  Closer to 100 means that, data will be moved to SWAP more often, therefore leaving RAM memory more free. We would recommend keeping this option closer to 0, for example, 10.
 
You can check the current value with the following command:
cat /proc/sys/vm/swappiness

 

Option - vfs_cache_pressure, this option sets how often the information about the file system is updated. By default, it should be 100, but we would recommend using a lower value, for example, 50.
 
The current value can be checked with this command:
cat /proc/sys/vm/vfs_cache_pressure

 

Both of the additional options can be edited at the file /etc/sysctl.conf
by adding the following line at the bottom of the file:
 

vm.swappiness=10
vm.vfs_cache_pressure=50
 

If any questions remain, please contact our customer support, we will be glad to help.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...