{"id":111,"date":"2021-05-18T22:57:20","date_gmt":"2021-05-18T20:57:20","guid":{"rendered":"https:\/\/www.speedhub.eu\/blog\/?p=111"},"modified":"2021-12-04T22:51:42","modified_gmt":"2021-12-04T20:51:42","slug":"modify-swap-memory-on-your-linux-vps","status":"publish","type":"post","link":"https:\/\/www.speedhub.eu\/blog\/2021\/05\/18\/modify-swap-memory-on-your-linux-vps\/","title":{"rendered":"Modify SWAP memory on your Linux VPS"},"content":{"rendered":"\n<figure class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" src=\"https:\/\/www.speedhub.eu\/blog\/wp-content\/uploads\/2021\/05\/Swap_memory-1024x608.jpg\" alt=\"\" data-id=\"113\" data-full-url=\"https:\/\/www.speedhub.eu\/blog\/wp-content\/uploads\/2021\/05\/Swap_memory.jpg\" data-link=\"https:\/\/www.speedhub.eu\/blog\/?attachment_id=113#main\" class=\"wp-image-113\" srcset=\"https:\/\/www.speedhub.eu\/blog\/wp-content\/uploads\/2021\/05\/Swap_memory-1024x608.jpg 1024w, https:\/\/www.speedhub.eu\/blog\/wp-content\/uploads\/2021\/05\/Swap_memory-300x178.jpg 300w, https:\/\/www.speedhub.eu\/blog\/wp-content\/uploads\/2021\/05\/Swap_memory-768x456.jpg 768w, https:\/\/www.speedhub.eu\/blog\/wp-content\/uploads\/2021\/05\/Swap_memory.jpg 1048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li><\/ul><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Allocating SWAP Memory<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First thing you should do is to check if there is no SWAP memory in use on your server with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><kbd>free -h<\/kbd><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your results will be printed in two lines: &#8220;Mem&#8221;, &#8220;Swap&#8221;, which will indicate, what the exact amount of RAM and SWAP memory is on the KVM server. The &#8220;Swap&#8221; line should only contain zeros.&nbsp;<br><br>With the following command, we will allocate 6 GB of disk space for our SWAP memory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fallocate -l 6G \/swapfile<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can check if your SWAP memory was assigned with this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ls -lh \/swapfile<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">At first, your SWAP may not be allocated due to permission issue and you might be seeing this message:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-rw-r--r-- 1 root root 6.0G Dec 5 14:32 \/swapfile<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This would suggest we have to make additional changes, first of which should be changing the permission of the swapfile:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">chmod 600 \/swapfile<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After the change you can check the file permissions again:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ls -lh \/swapfile<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The results should change as well, comparing to the previous above:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-rw------- 1 root root 6.0G Dec&nbsp; 5 14:36 \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now set the swapfile as Linux swap area:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, enable SWAP usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>swapon \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can now check if the SWAP memory was allocated correctly:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">free -h<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your results will print two lines again, just this time, you will see a line &#8220;Swap&#8221; having a variable of 6 GB.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Additional Options<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cp \/etc\/fstab \/etc\/fstab.old<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once the backup is done, we have to make the changes to the actual file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There are few extra options for SWAP memory, which we would like to address as well:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Option &#8211; 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.&nbsp; 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can check the current value with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cat \/proc\/sys\/vm\/swappiness<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Option &#8211; 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The current value can be checked with this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cat \/proc\/sys\/vm\/vfs_cache_pressure<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both of the additional options can be edited at the file \/etc\/sysctl.conf<br>by adding the following line at the bottom of the file:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>vm.swappiness=10<\/code><br><code>vm.vfs_cache_pressure=50<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: &#8220;Mem&#8221;, &#8220;Swap&#8221;, which will&hellip; <a href=\"https:\/\/www.speedhub.eu\/blog\/2021\/05\/18\/modify-swap-memory-on-your-linux-vps\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":113,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[30,29,27,41],"class_list":["post-111","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps-servers","tag-linux","tag-memory","tag-swap","tag-vps"],"_links":{"self":[{"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":2,"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions\/114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/media\/113"}],"wp:attachment":[{"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speedhub.eu\/blog\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}