{"id":52,"date":"2025-01-19T23:22:38","date_gmt":"2025-01-19T15:22:38","guid":{"rendered":"https:\/\/zhang.bo.de.com\/page\/?p=52"},"modified":"2025-01-19T23:22:38","modified_gmt":"2025-01-19T15:22:38","slug":"how-to-add-swap-space-on-debian-11","status":"publish","type":"post","link":"https:\/\/zhang.bo.de.com\/page\/2025\/01\/19\/52.html","title":{"rendered":"How To Add Swap Space on Debian 11"},"content":{"rendered":"<h3 id=\"introduction\" class=\"wp-block-heading\">Introduction<\/h3>\n<p>One way to guard against out-of-memory errors in applications is to add some swap space to your server. In this guide, we will cover how to add a swap file to an Debian 11 server.<\/p>\n<h2 id=\"what-is-swap\" class=\"wp-block-heading\">What is Swap?<\/h2>\n<p><em>Swap<\/em>\u00a0is a portion of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in RAM. This lets you increase the amount of information that your server can keep in its working memory, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.<\/p>\n<p>The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system\u2019s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.<\/p>\n<h2 id=\"step-1-checking-the-system-for-swap-information\" class=\"wp-block-heading\">Step 1 \u2013 Checking the System for Swap Information<\/h2>\n<p>Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.<\/p>\n<p>We can see if the system has any configured swap by typing:<\/p>\n<pre class=\"wp-block-code\"><code>sudo swapon --show\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>If you don\u2019t get back any output, this means your system does not have swap space available currently.<\/p>\n<p>You can verify that there is no active swap using the\u00a0<code>free<\/code>\u00a0utility:<\/p>\n<pre class=\"wp-block-code\"><code>free -h\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>Output               total        used        free      shared  buff\/cache   available\r\nMem:           976Mi        75Mi       623Mi       0.0Ki       276Mi       765Mi\r\n<mark>Swap:             0B          0B          0B<\/mark>\r\n<\/code><\/pre>\n<p>As you can see in the\u00a0<strong>Swap<\/strong>\u00a0row of the output, no swap is active on the system.<\/p>\n<h2 id=\"step-2-checking-available-space-on-the-hard-drive-partition\" class=\"wp-block-heading\">Step 2 \u2013 Checking Available Space on the Hard Drive Partition<\/h2>\n<p>Before we create our swap file, we\u2019ll check our current disk usage to make sure we have enough space. Do this by entering:<\/p>\n<pre class=\"wp-block-code\"><code>df -h\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>OutputFilesystem      Size  Used Avail Use% Mounted on\r\nudev            472M     0  472M   0% \/dev\r\ntmpfs            98M  500K   98M   1% \/run\r\n<mark>\/dev\/vda1        25G  1.1G   23G   5% \/<\/mark>\r\ntmpfs           489M     0  489M   0% \/dev\/shm\r\ntmpfs           5.0M     0  5.0M   0% \/run\/lock\r\n\/dev\/vda15      124M  5.9M  118M   5% \/boot\/efi\r\ntmpfs            98M     0   98M   0% \/run\/user\/0\r\n<\/code><\/pre>\n<p>The device with\u00a0<code>\/<\/code>\u00a0in the\u00a0<code>Mounted on<\/code>\u00a0column is our disk in this case. We have plenty of space available in this example (only 1.4G used). Your usage will probably be different.<\/p>\n<p>Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.<\/p>\n<h2 id=\"step-3-creating-a-swap-file\" class=\"wp-block-heading\">Step 3 \u2013 Creating a Swap File<\/h2>\n<p>Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the size that we want called\u00a0<code>swapfile<\/code>\u00a0in our root (<code>\/<\/code>) directory.<\/p>\n<p>The best way of creating a swap file is with the\u00a0<code>fallocate<\/code>\u00a0program. This command instantly creates a file of the specified size.<\/p>\n<p>Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:<\/p>\n<pre class=\"wp-block-code\"><code>sudo fallocate -l <mark>1G<\/mark> \/swapfile\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>We can verify that the correct amount of space was reserved by typing:<\/p>\n<pre class=\"wp-block-code\"><code>ls -lh \/swapfile\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>-rw-r--r-- 1 root root 1.0G Aug 23 11:14 \/swapfile\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>Our file has been created with the correct amount of space set aside.<\/p>\n<h2 id=\"step-4-enabling-the-swap-file\" class=\"wp-block-heading\">Step 4 \u2013 Enabling the Swap File<\/h2>\n<p>Now that we have a file of the correct size available, we need to actually turn this into swap space.<\/p>\n<p>First, we need to lock down the permissions of the file so that only users with\u00a0<strong>root<\/strong>\u00a0privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.<\/p>\n<p>Make the file only accessible to\u00a0<strong>root<\/strong>\u00a0by typing:<\/p>\n<pre class=\"wp-block-code\"><code>sudo chmod 600 \/swapfile\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>Verify the permissions change by typing:<\/p>\n<pre class=\"wp-block-code\"><code>ls -lh \/swapfile\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>Output<mark>-rw-------<\/mark> 1 root root 1.0G Aug 23 11:14 \/swapfile\r\n<\/code><\/pre>\n<p>As you can see, only the\u00a0<strong>root<\/strong>\u00a0user has the read and write flags enabled.<\/p>\n<p>We can now mark the file as swap space by typing:<\/p>\n<pre class=\"wp-block-code\"><code>sudo mkswap \/swapfile\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>OutputSetting up swapspace version 1, size = 1024 MiB (1073737728 bytes)\r\nno label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf\r\n<\/code><\/pre>\n<p>After marking the file, we can enable the swap file, allowing our system to start using it:<\/p>\n<pre class=\"wp-block-code\"><code>sudo swapon \/swapfile\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>Verify that the swap is available by typing:<\/p>\n<pre class=\"wp-block-code\"><code>sudo swapon --show\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>OutputNAME      TYPE  SIZE USED PRIO\r\n\/swapfile file 1024M   0B   -2\r\n<\/code><\/pre>\n<p>We can check the output of the\u00a0<code>free<\/code>\u00a0utility again to corroborate our findings:<\/p>\n<pre class=\"wp-block-code\"><code>free -h\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>Output               total        used        free      shared  buff\/cache   available\r\nMem:           976Mi        85Mi       612Mi       0.0Ki       279Mi       756Mi\r\n<mark>Swap:          1.0Gi          0B       1.0Gi<\/mark>\r\n<\/code><\/pre>\n<p>Our swap has been set up successfully and our operating system will begin to use it as necessary.<\/p>\n<h2 id=\"step-5-making-the-swap-file-permanent\" class=\"wp-block-heading\">Step 5 \u2013 Making the Swap File Permanent<\/h2>\n<p>Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our\u00a0<code>\/etc\/fstab<\/code>\u00a0file.<\/p>\n<p>Back up the\u00a0<code>\/etc\/fstab<\/code>\u00a0file in case anything goes wrong:<\/p>\n<pre class=\"wp-block-code\"><code>sudo cp \/etc\/fstab \/etc\/fstab.bak\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>Add the swap file information to the end of your\u00a0<code>\/etc\/fstab<\/code>\u00a0file by typing:<\/p>\n<pre class=\"wp-block-code\"><code>echo '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>Next we\u2019ll review some settings we can update to tune our swap space.<\/p>\n<h2 id=\"step-6-tuning-your-swap-settings\" class=\"wp-block-heading\">Step 6 \u2013 Tuning your Swap Settings<\/h2>\n<p>There are a few options that you can configure that will have an impact on your system\u2019s performance when dealing with swap.<\/p>\n<h3 id=\"adjusting-the-swappiness-property\" class=\"wp-block-heading\">Adjusting the Swappiness Property<\/h3>\n<p>The\u00a0<code>swappiness<\/code>\u00a0parameter 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.<\/p>\n<p>With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Remember, interactions with the swap file are \u201cexpensive\u201d 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.<\/p>\n<p>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\u2019 memory profile or what you are using your server for, this might be better in some cases.<\/p>\n<p>We can see the current swappiness value by typing:<\/p>\n<pre class=\"wp-block-code\"><code>cat \/proc\/sys\/vm\/swappiness\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>Output60\r\n<\/code><\/pre>\n<p>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.<\/p>\n<p>We can set the swappiness to a different value by using the\u00a0<code>sysctl<\/code>\u00a0command.<\/p>\n<p>For instance, to set the swappiness to 10, we could type:<\/p>\n<pre class=\"wp-block-code\"><code>sudo sysctl vm.swappiness=10\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>Outputvm.swappiness = 10\r\n<\/code><\/pre>\n<p>This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our\u00a0<code>\/etc\/sysctl.conf<\/code>\u00a0file:<\/p>\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/sysctl.conf\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>At the bottom, you can add:<\/p>\n<p>\/etc\/sysctl.conf<\/p>\n<pre class=\"wp-block-code\"><code>vm.swappiness=10\r\n<\/code><\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<h3 id=\"adjusting-the-cache-pressure-setting\" class=\"wp-block-heading\">Adjusting the Cache Pressure Setting<\/h3>\n<p>Another related value that you might want to modify is the\u00a0<code>vfs_cache_pressure<\/code>. This setting configures how much the system will choose to cache\u00a0<em>inode<\/em>\u00a0and\u00a0<em>dentry<\/em>\u00a0information over other data.<\/p>\n<p>Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it\u2019s an excellent thing for your system to cache. You can see the current value by querying the\u00a0<code>proc<\/code>\u00a0filesystem again:<\/p>\n<pre class=\"wp-block-code\"><code>cat \/proc\/sys\/vm\/vfs_cache_pressure\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>Output100\r\n<\/code><\/pre>\n<p>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:<\/p>\n<pre class=\"wp-block-code\"><code>sudo sysctl vm.vfs_cache_pressure=50\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<pre class=\"wp-block-code\"><code>Outputvm.vfs_cache_pressure = 50\r\n<\/code><\/pre>\n<p>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:<\/p>\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/sysctl.conf\r\n<\/code><\/pre>\n<p>Copy<\/p>\n<p>At the bottom, add the line that specifies your new value:<\/p>\n<p>\/etc\/sysctl.conf<\/p>\n<pre class=\"wp-block-code\"><code>vm.vfs_cache_pressure=50\r\n<\/code><\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<h2 id=\"conclusion\" class=\"wp-block-heading\">Conclusion<\/h2>\n<p>Following the steps in this guide will give you some breathing room in cases that would otherwise lead to out-of-memory exceptions. Swap space can be incredibly useful in avoiding some of these common problems.<\/p>\n<p>If you are running into out-of-memory errors, or if you find that your system is unable to use the applications you need, the best solution is to optimize your application configurations or upgrade your server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction One way to guard against out-of-memory errors in applications is to add some swap space to your server. In this guide, we will cover how &hellip;<\/p>\n<p> <a class=\"more-link\" href=\"https:\/\/zhang.bo.de.com\/page\/2025\/01\/19\/52.html\">\u7ee7\u7eed\u9605\u8bfb<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-52","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-article"},"_links":{"self":[{"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":1,"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":53,"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/posts\/52\/revisions\/53"}],"wp:attachment":[{"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.bo.de.com\/page\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}