{"id":21925,"date":"2026-02-23T11:07:31","date_gmt":"2026-02-23T08:07:31","guid":{"rendered":"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/"},"modified":"2026-11-12T18:13:01","modified_gmt":"2026-11-12T15:13:01","slug":"what-is-swap-space","status":"publish","type":"post","link":"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/","title":{"rendered":"What Is Swap Space? Creating Swap on a Linux Server and Tuning Swappiness"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#How_Does_Swap_Space_Work\" >How Does Swap Space Work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#Why_Swap_Matters_on_a_Server\" >Why Swap Matters on a Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#Swap_Partition_vs_Swap_File\" >Swap Partition vs Swap File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#Creating_a_Swap_File_on_a_Linux_Server\" >Creating a Swap File on a Linux Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#What_Is_Swappiness_and_How_Do_You_Set_It\" >What Is Swappiness and How Do You Set It?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#How_Big_Should_Swap_Be\" >How Big Should Swap Be?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#Is_high_swap_usage_a_problem\" >Is high swap usage a problem?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#How_do_I_empty_swap_temporarily\" >How do I empty swap temporarily?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#Does_using_swap_on_an_SSD_wear_it_out\" >Does using swap on an SSD wear it out?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/sunucun.com.tr\/blog\/what-is-swap-space\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> Swap space is disk space where the operating system temporarily moves rarely used memory pages when the server&#8217;s physical memory (RAM) fills up. It doesn&#8217;t make RAM bigger, but it&#8217;s a safety net that stops services from being killed when memory suddenly runs out. On Linux it&#8217;s configured as a separate partition (swap partition) or a file (swap file).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2026\/02\/text-swap-alani-nedir-sunucularda-sanal-bellek-yapilandirmasi.png\" alt=\"Data moving from RAM to swap space on disk when memory is full\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_Does_Swap_Space_Work\"><\/span>How Does Swap Space Work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">All applications, services and the operating system on a server share the same <a href=\"https:\/\/sunucun.com.tr\/blog\/what-is-ram\/\">RAM<\/a>. When total memory demand approaches physical RAM, the Linux <a href=\"https:\/\/sunucun.com.tr\/blog\/what-is-a-kernel\/\">kernel<\/a> writes memory pages that haven&#8217;t been used for a while to disk, into swap space (swap out). When those pages are needed again, they&#8217;re read back from disk into RAM (swap in). This lets the system behave as if it had more memory than physical RAM; the concept is called virtual memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Swap_Matters_on_a_Server\"><\/span>Why Swap Matters on a Server<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>A buffer against the OOM Killer:<\/strong> when RAM is completely full and there&#8217;s no swap, the kernel&#8217;s OOM (Out of Memory) Killer terminates memory-using processes to keep the system alive. Often that&#8217;s the biggest consumer, such as the database or web server.<\/li>\n<li><strong>Efficient use of RAM:<\/strong> memory pages loaded at startup and never used again are moved to disk, leaving RAM for active applications and the file cache.<\/li>\n<li><strong>Sudden load spikes:<\/strong> it prevents a crash when memory is briefly exceeded during short traffic peaks or jobs such as backups.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Disk is much slower than RAM. If the system is constantly writing to and reading from swap (thrashing), everything slows down noticeably. That means RAM, not swap, is insufficient; the fix is adding RAM or reducing memory use, not enlarging swap.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Swap_Partition_vs_Swap_File\"><\/span>Swap Partition vs Swap File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"snc-table-wrap\"><table>\n<thead><tr><th>Feature<\/th><th>Swap partition<\/th><th>Swap file<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>When it&#8217;s created<\/td><td>Usually during OS installation, as a separate disk partition<\/td><td>Any time while the system is running<\/td><\/tr>\n<tr><td>Resizing<\/td><td>Hard; requires changing disk partitions (or LVM)<\/td><td>Easy; delete the file and create a new one at the new size<\/td><\/tr>\n<tr><td>Performance<\/td><td>No file system layer; historically slightly faster<\/td><td>On modern kernels and SSDs the difference is negligible in practice<\/td><\/tr>\n<tr><td>Best for<\/td><td>Initial setup of physical servers<\/td><td><a href=\"https:\/\/sunucun.com.tr\/vds-sunucu\" data-internallinksmanager029f6b8e52c=\"133\" title=\"Tahsisli kaynakl\u0131 sanal sunucu \u00e7\u00f6z\u00fcmleri\">VDS<\/a>\/<a href=\"https:\/\/sunucun.com.tr\/vps-sunucu\" data-internallinksmanager029f6b8e52c=\"132\" title=\"Sanal sunucu se\u00e7eneklerini inceleyin\">VPS<\/a> and servers where swap is added later<\/td><\/tr>\n<\/tbody><\/table><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Many VDS\/VPS images come without swap. In that case the most practical approach is to add a swap file as shown below. If you&#8217;re wondering what a VDS is, see our <a href=\"https:\/\/sunucun.com.tr\/blog\/what-is-vds\/\">what is a VDS<\/a> article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_a_Swap_File_on_a_Linux_Server\"><\/span>Creating a Swap File on a Linux Server<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The steps below work on Ubuntu, Debian, AlmaLinux and Rocky Linux (on ext4 and XFS file systems). Systems using Btrfs need different steps for a swap file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Check the current swap status.<\/strong> If <code>swapon --show<\/code> prints nothing, or the Swap line in <code>free -h<\/code> shows 0, there&#8217;s no swap:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\nfree -h<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Create the swap file<\/strong> (example: 4 GB) and make it readable and writable only by root. <code>fallocate<\/code> is fast; on file systems that don&#8217;t support it, use <code>dd<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fallocate -l 4G \/swapfile\n# if fallocate doesn't work:\n# sudo dd if=\/dev\/zero of=\/swapfile bs=1M count=4096\nsudo chmod 600 \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Format the file as swap and enable it:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkswap \/swapfile\nsudo swapon \/swapfile\nswapon --show<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. Make it persistent across reboots.<\/strong> Add this line to the end of <code>\/etc\/fstab<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A typo in fstab can stop the server from booting; it&#8217;s a good habit to check the file with <code>sudo findmnt --verify<\/code> after any change.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Is_Swappiness_and_How_Do_You_Set_It\"><\/span>What Is Swappiness and How Do You Set It?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>vm.swappiness<\/strong> controls how willing the kernel is to move application memory to swap rather than drop file cache when memory gets tight. The default on most distributions is 60. Since Linux 5.8 the range is 0\u2013200 (0\u2013100 on older kernels).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Low value (e.g. 10):<\/strong> swap is used as late as possible. Often chosen for latency-sensitive servers such as database servers.<\/li>\n<li><strong>Default (60):<\/strong> a balanced setting for general-purpose systems.<\/li>\n<li><strong>0:<\/strong> doesn&#8217;t disable swap, only postpones it until the last moment; if memory fills up, the OOM Killer may step in sooner. Use with care.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># View the current value\ncat \/proc\/sys\/vm\/swappiness\n\n# Change it temporarily\nsudo sysctl vm.swappiness=10\n\n# Make it persistent\necho 'vm.swappiness=10' | sudo tee \/etc\/sysctl.d\/99-swappiness.conf\nsudo sysctl --system<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_Big_Should_Swap_Be\"><\/span>How Big Should Swap Be?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s no strict rule. The values Red Hat recommends in its RHEL documentation for systems that don&#8217;t use hibernation are a good starting point:<\/p>\n\n\n\n<div class=\"snc-table-wrap\"><table>\n<thead><tr><th>Physical RAM<\/th><th>Recommended swap<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>2 GB or less<\/td><td>2\u00d7 RAM<\/td><\/tr>\n<tr><td>2 GB \u2013 8 GB<\/td><td>Equal to RAM<\/td><\/tr>\n<tr><td>8 GB \u2013 64 GB<\/td><td>At least 4 GB<\/td><\/tr>\n<tr><td>More than 64 GB<\/td><td>At least 4 GB<\/td><\/tr>\n<\/tbody><\/table><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">On servers with plenty of RAM, swap is a buffer; rather than allocating a huge swap, monitor memory use (<code>free -h<\/code>, <code>vmstat 1<\/code>) and add RAM if swap usage stays high.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Is_high_swap_usage_a_problem\"><\/span>Is high swap usage a problem?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not on its own. It&#8217;s normal for pages that haven&#8217;t been used for a long time to sit in swap. The real warning sign is the si\/so (swap in\/out) columns in <code>vmstat 1<\/code> staying above zero, which means the system is actively writing to and reading from disk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_do_I_empty_swap_temporarily\"><\/span>How do I empty swap temporarily?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sudo swapoff -a &amp;&amp; sudo swapon -a<\/code> moves the pages in swap back into RAM. Only do this when free RAM is larger than used swap; otherwise the OOM Killer may step in.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Does_using_swap_on_an_SSD_wear_it_out\"><\/span>Does using swap on an SSD wear it out?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Occasional swap writes aren&#8217;t a problem for the endurance of today&#8217;s SSDs. Constant heavy swap use hurts both performance and the disk; in that case you should add RAM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Swap space is a safety net that stops services from being killed when RAM fills up; it exists for stability, not performance. On VDS\/VPS servers you can add a swap file with a few commands and tune its use for your workload with swappiness. If swap is constantly in use, moving to a <a href=\"https:\/\/sunucun.com.tr\/en\/vds-server\">VDS server<\/a> plan with more RAM is the lasting fix.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Swap space is disk space where the operating system temporarily moves rarely used memory pages when the server&#8217;s physical memory (RAM) fills up. It doesn&#8217;t make RAM bigger, but it&#8217;s a safety net that stops services from being killed when memory suddenly runs out. On Linux it&#8217;s configured as a separate partition (swap&hellip;<\/p>\n","protected":false},"author":1,"featured_media":20342,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1520],"tags":[],"class_list":["post-21925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/21925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=21925"}],"version-history":[{"count":0,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/21925\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media\/20342"}],"wp:attachment":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=21925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=21925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=21925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}