SWAP memory is used to help RAM, once it can not store any more data. Here is how to modify and increase SWAP memory on Linux.
SWAP memory is used to help RAM, once it can not store any more data. The data which can not be stored in RAM is then stored to SWAP memory in the hard disk. Here is how to modify and increase SWAP memory on Linux.
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 this case, I will assign 6 GB of SWAP to the server.
Allocating SWAP memory
First thing you should do is to check if there is not 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 is the exact amount of RAM and SWAP memory on the server. If SWAP is not yet configured the “Swap” line should only contain zeros.
With the following command we will allocate 1 GB more of disk space for our SWAP memory:
fallocate -l 1G /swapfile
fallocate -change the allocated disk space for a file, either to deallocate or preallocate it ( e.g increase it by 1GB from 11G to 12G).
Suppose fallocate fails or is not available, you can also use the dd command:
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 a permission issue and you might be seeing this message:
-rw-r--r-- 1 root root 12.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, compared to the previous above:
-rw------- 1 root root 12.0G Dec 5 14:36 /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 12 GB.
Make SWAP file permanent
In general, your SWAP memory allocation may stop working after you reboot the 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 can make the changes to the actual file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Additional Swap options
There are few extra option for SWAP memory, which we would like to address as well.
option – swappiness, is used to describe when the system will move data to SWAP file. The option may have a value from 0 to 100. Closer to 0, means that your data will be move 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 file system is updated. By default it should be 100, but we would recommend using low, for example, value 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 lines at the bottom of the file:
My name is Stefan. I have been creating websites since 2009 and have been working on WordPress, cPanel, and Linux on a near-daily basis since 2012.
Throughout my career, I've worked with companies large and small to meet their business goals and I've been fortunate enough to contribute to such global brands as Michelin and Raiffeisen Bank.
If you want to work together, have an idea or simply want to say hello, then send me an email.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.