{"id":28246,"date":"2024-08-30T20:34:00","date_gmt":"2024-08-30T20:34:00","guid":{"rendered":"https:\/\/goodwriterz.com\/site\/?p=28246"},"modified":"2024-08-30T20:34:00","modified_gmt":"2024-08-30T20:34:00","slug":"what-is-swappiness-on-linux-and-how-to-change-it","status":"publish","type":"post","link":"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/","title":{"rendered":"What Is Swappiness on Linux? (and How to Change It)"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_68_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >\u062c\u062f\u0648\u0644 \u0627\u0644\u0645\u062d\u062a\u0648\u064a\u0627\u062a<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#Busting_Myths_About_Swapiness\" title=\"Busting Myths About Swapiness\">Busting Myths About Swapiness<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#Your_RAM_is_Split_Into_Zones\" title=\"Your RAM is Split Into Zones\">Your RAM is Split Into Zones<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#The_PAGESIZE_Value\" title=\"The PAGESIZE Value\">The PAGESIZE Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#Zones_Are_Attached_to_Nodes\" title=\"Zones Are Attached to Nodes\">Zones Are Attached to Nodes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#File_Pages_and_Anonymous_Pages\" title=\"File Pages and Anonymous Pages\">File Pages and Anonymous Pages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#Swappiness\" title=\"Swappiness\">Swappiness<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#The_Golden_Ratio\" title=\"The Golden Ratio\">The Golden Ratio<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/#What_Should_Swapiness_Be_Set_To\" title=\"What Should Swapiness Be Set To?\">What Should Swapiness Be Set To?<\/a><\/li><\/ul><\/nav><\/div>\n<div>\n<p> The Linux swappiness value has nothing to do with how much RAM is used before swapping starts. That&#8217;s a widely reported and widely believed mistake. We explain what it really is. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":0,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":0,\"nbrPlacementsScanned\":0,\"ruleCount\":200,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":184} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":true,\"currentRuleCount\":200,\"actualCount\":184,\"hasActualCountMetThreshold\":true,\"countRemainingForInjection\":16,\"nextBlockCount\":176,\"hasCountRemainingForInjectionMetThreshold\":true,\"nextCount\":360} --><\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":0,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":0,\"nbrPlacementsScanned\":0,\"ruleCount\":200,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":true,\"actualCount\":200} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":1,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":0,\"ruleCount\":200,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":true,\"actualCount\":0} --><br \/>\n<!-- No winning ad found for zone: below first paragraph! --><br \/>\n<!-- No winning ad found for zone: native in content! --><\/p>\n<h2 id=\"busting-myths-about-swapiness\"><span class=\"ez-toc-section\" id=\"Busting_Myths_About_Swapiness\"><\/span> Busting Myths About Swapiness <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> Swapping is a technique where data in <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Random-access_memory\">Random Access Memory<\/a> (RAM) is written to a special location on your hard disk&#8212;either a swap partition or a swap file&#8212;to free up RAM. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":1,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":160} --><\/p>\n<p> Linux has a setting called the swappiness value. There&#8217;s a lot of confusion about what this setting controls. The most common incorrect description of swappiness is that it sets a threshold for RAM usage, and when the amount of used RAM hits that threshold, swapping starts. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":1,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":440} --><\/p>\n<p> This is a misconception that has been repeated so often that it is now received wisdom. If (almost) everyone else tells you that&#8217;s exactly how swappiness works, why should you believe us when we say it isn&#8217;t? <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":1,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":658} --><\/p>\n<p> Simple. We&#8217;re going to prove it. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":1,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":696} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":1,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":1,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":696} --><\/p>\n<h2 id=\"your-ram-is-split-into-zones\"><span class=\"ez-toc-section\" id=\"Your_RAM_is_Split_Into_Zones\"><\/span> Your RAM is Split Into Zones <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> Linux doesn&#8217;t think of your RAM as one big homogenous pool of memory. It considers it to be divided into a number of different regions called zones. Which zones are present on your computer depends on whether it is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/32-bit\">32-bit<\/a> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/64-bit_computing\">64-bit<\/a>. Here&#8217;s a simplified description of the possible zones on an <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/X86\">x86 architecture computer<\/a>. <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":1,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":1,\"nbrPlacementsScanned\":1,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":1025} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":2,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":1,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<ul>\n<li><strong>Direct Memory Access (DMA)<\/strong>: This is the low 16 MB of memory. The zone gets its name because, a long time ago, there were computers that could only do direct memory access into this area of physical memory. <\/li>\n<li><strong>Direct Memory Access 32<\/strong>: Despite its name, Direct Memory Access 32 (DMA32) is a zone only found in 64-bit Linux. It&#8217;s the low 4 GB of memory. Linux running on 32-bit computers can only do DMA to this amount of RAM (unless they are using the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Physical_Address_Extension#Linux\">physical address extension<\/a> (PAE) kernel), which is how the zone got its name. Although, on 32-bit computers, it is called HighMem. <\/li>\n<li><strong>Normal<\/strong>: On 64-bit computers, normal memory is all of the RAM above 4GB (roughly). On 32-bit machines, it is RAM between 16 MB and 896 MB. <\/li>\n<li><strong>HighMem<\/strong>: This only exists on 32-bit Linux computers. It is all RAM above 896 MB, including RAM above 4 GB on sufficiently large machines. <\/li>\n<\/ul>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":859} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":2,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":859} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":true,\"currentRuleCount\":900,\"actualCount\":859,\"hasActualCountMetThreshold\":true,\"countRemainingForInjection\":41,\"nextBlockCount\":210,\"hasCountRemainingForInjectionMetThreshold\":true,\"nextCount\":1069} --><\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":2,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":2,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":true,\"actualCount\":900} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":2,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":true,\"actualCount\":0} --><\/p>\n<h2 id=\"the-pagesize-value\"><span class=\"ez-toc-section\" id=\"The_PAGESIZE_Value\"><\/span> The PAGESIZE Value <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> RAM is allocated in pages, which are of a fixed size. That size is determined by the kernel at boot time by detecting the architecture of the computer. Typically the page size on a Linux computer is 4 Kbytes. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":169} --><\/p>\n<p> You can see your page size <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/getconf.1p.html\">using the <\/a><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/getconf.1p.html\"><code>getconf<\/code><\/a><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/getconf.1p.html\">command<\/a>: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":223} --><\/p>\n<pre>getconf PAGESIZE<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":241} --><\/p>\n<div class=\"body-img landscape \">\n<div class=\"responsive-img image-expandable img-article-item\">\n<figure><img data-recalc-dims=\"1\" decoding=\"async\" width=\"646\" height=\"97\" loading=\"lazy\" alt=\"getconf PAGESIZE\" data-img-url=\"https:\/\/goodwriterz.com\/site\/wp-content\/uploads\/2024\/08\/What-Is-Swappiness-on-Linux-and-How-to-Change-It.png\" src=\"https:\/\/i0.wp.com\/goodwriterz.com\/site\/wp-content\/uploads\/2024\/08\/What-Is-Swappiness-on-Linux-and-How-to-Change-It.png?resize=646%2C97&#038;ssl=1\" style=\"height:auto;max-width:100%\" title=\"\">  <\/figure>\n<\/p><\/div>\n<\/p><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":481} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":481} --><\/p>\n<h2 id=\"zones-are-attached-to-nodes\"><span class=\"ez-toc-section\" id=\"Zones_Are_Attached_to_Nodes\"><\/span> Zones Are Attached to Nodes <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> Zones are attached to nodes. Nodes are associated with a Central Processing Unit (CPU). The kernel will try to allocate memory for a process running on a CPU from the node associated with that CPU. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":680} --><\/p>\n<p> The concept of nodes being tied to CPUs allows mixed memory types to be installed in specialist multi-CPU computers, using the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Non-uniform_memory_access\">Non-Uniform Memory Access<\/a> architecture. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":848} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":true,\"currentRuleCount\":900,\"actualCount\":848,\"hasActualCountMetThreshold\":true,\"countRemainingForInjection\":52,\"nextBlockCount\":249,\"hasCountRemainingForInjectionMetThreshold\":true,\"nextCount\":1097} --><\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":3,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":3,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":true,\"actualCount\":900} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":3,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":true,\"actualCount\":0} --><\/p>\n<p> That&#8217;s all very high-end. The average Linux computer will have a single node, called node zero. All zones will belong to that node. To see the nodes and zones in your computer, look inside the <code>\/proc\/buddyinfo<\/code> file. We&#8217;ll use <code>less<\/code> to do so: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":197} --><\/p>\n<pre>less \/proc\/buddyinfo<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":219} --><\/p>\n<div class=\"body-img landscape \">\n<div class=\"responsive-img image-expandable img-article-item\">\n<figure><img data-recalc-dims=\"1\" decoding=\"async\" width=\"646\" height=\"57\" loading=\"lazy\" alt=\"less \/proc\/buddyinfo in a terminal window\" data-img-url=\"https:\/\/goodwriterz.com\/site\/wp-content\/uploads\/2024\/08\/1725050035_431_What-Is-Swappiness-on-Linux-and-How-to-Change-It.png\" src=\"https:\/\/i0.wp.com\/goodwriterz.com\/site\/wp-content\/uploads\/2024\/08\/1725050035_431_What-Is-Swappiness-on-Linux-and-How-to-Change-It.png?resize=646%2C57&#038;ssl=1\" style=\"height:auto;max-width:100%\" title=\"\">  <\/figure>\n<\/p><\/div>\n<\/p><\/div>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":459} --><\/p>\n<p> This is the output from the 64-bit computer this article was researched on: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":536} --><\/p>\n<pre>Node 0, zone DMA 1 1 1 0 2 1 1 0 1 1 3 \n<!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":576} -->\n<p> Node 0, zone DMA32 2 67 58 19 8 3 3 1 1 1 17<\/p><\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":622} --><\/p>\n<p> There is a single node, node zero. This computer only has 2 GB of RAM, so there is no &#8220;Normal&#8221; zone. There are only two zones, DMA and DMA32. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":775} --><\/p>\n<p> Each column represents the number of available pages of a certain size. For example, for the DMA32 zone, reading from the left: <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":4,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":4,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":904} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":5,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":4,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<ul>\n<li><strong>2<\/strong>: There are 2 of 2^(<strong>0<\/strong>*PAGESIZE) chunks of memory. <\/li>\n<li><strong>67<\/strong>: There are 67 of 2^(<strong>1<\/strong>*PAGE_SIZE) chunks of memory. <\/li>\n<li><strong>58<\/strong>: There are 58 of 2^(<strong>2<\/strong>*PAGESIZE) chunks of memory available. <\/li>\n<li> And so on, all the way up to&#8230; <\/li>\n<li><strong>17<\/strong>: There are 17 of 2^(<strong>512<\/strong>*PAGESIZE) chunks. <\/li>\n<\/ul>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":247} --><\/p>\n<p> But really, the only reason we&#8217;re looking at this information is to see the relationship between nodes and zones. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":366} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":366} --><\/p>\n<h2 id=\"file-pages-and-anonymous-pages\"><span class=\"ez-toc-section\" id=\"File_Pages_and_Anonymous_Pages\"><\/span> File Pages and Anonymous Pages <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> Memory mapping uses sets of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Page_table\">page table entries<\/a> to record which memory pages are used, and for what. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":467} --><\/p>\n<p> Memory mappings can be: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":5,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":492} --><\/p>\n<ul>\n<li><strong>File backed<\/strong>: File backed mappings contain data that has been read from a file. It can be any kind of file. The important thing to note is that if the system freed this memory and needed to obtain that data again, it can be read from the file once more. But, if the data has been changed in memory, those changes will need to be written to the file on the hard drive before the memory can be freed. If that didn&#8217;t happen, the changes would be lost. <\/li>\n<li><strong>Anonymous<\/strong>: Anonymous memory is a memory mapping with no file or device backing it. These pages may contain memory requested on-the-fly by programs to hold data, or for such things as the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Call_stack\">stack<\/a> and the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Memory_management#HEAP\">heap<\/a>. Because there is no file behind this type of data, a special place must be set aside for the storage of anonymous data. That place is the swap partition or swap file. Anonymous data is written to swap before anonymous pages are freed. <\/li>\n<li><strong>Device backed<\/strong>: Devices are addressed through block device files that can be treated as though they were files. Data can be read from them and written to them. A device backed memory mapping has data from a device stored in it. <\/li>\n<li><strong>Shared<\/strong>: Multiple <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Page_table\">page table entries<\/a> can map to the same page of RAM. Accessing the memory locations through any of the mappings will show the same data. Different processes can communicate with one another in a very efficient way by changing the data in these jointly-watched memory locations. Shared writable mappings are a common means of achieving high-performance inter-process communications. <\/li>\n<li><strong>Copy on write<\/strong>: Copy on write is a lazy allocation technique. If a copy of a resource already in memory is requested, the request is satisfied by returning a mapping to the original resource. If one of the processes &#8220;sharing&#8221; the resource tries to write to it, the resource must be truly replicated in memory to allow the changes to be made to the new copy. So the memory allocation only takes place on the first write command. <\/li>\n<\/ul>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":5,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":5,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":2449} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":6,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":5,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<p> For swappiness, we need only concern ourselves with the first two in the list: file pages and anonymous pages. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":112} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":112} --><\/p>\n<h2 id=\"swappiness\"><span class=\"ez-toc-section\" id=\"Swappiness\"><\/span> Swappiness <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> Here&#8217;s the description of swappiness <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/torvalds\/linux\/blob\/v5.0\/Documentation\/sysctl\/vm.txt#L809\">from the Linux documentation<\/a> on <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/\">GitHub<\/a>: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":194} --><\/p>\n<p> <em>&#8220;<\/em>This control is used to define how aggressive (sic) the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone. The default value is 60.<em>&#8220;<\/em> <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":551} --><\/p>\n<p> That sounds like swappiness turns swap up or down in intensity. Interestingly, it states that setting swappiness to zero doesn&#8217;t turn off swap. It instructs the kernel not to swap until certain conditions are met. But swapping can still occur. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":6,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":800} --><\/p>\n<p> Let&#8217;s dig deeper. Here&#8217;s the definition and default value of <code>vm_swappiness<\/code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/torvalds\/linux\/blob\/master\/mm\/vmscan.c#L176\">in the kernel source code file vmscan.c<\/a>: <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":6,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":6,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":924} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":7,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":6,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<pre><code class=\"hljs java\">\/*<br \/>* From 0 .. 100. Higher means more swappy.<br \/>*\/<br \/><span class=\"hljs-keyword\">int<\/span> vm_swappiness = <span class=\"hljs-number\">60<\/span>;<\/code> <\/pre>\n<p> The swappiness value can range from 0 to 100. Again, the comment certainly sounds like the swappiness value has a bearing on how much swapping takes place, with a higher figure leading to more swapping. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":204} --><\/p>\n<p> Further on in the source code file, we can see that a new variable called <code>swappiness<\/code> is assigned a value that is returned by the function <code>mem_cgroup_swappiness()<\/code>. Some more tracing through the source code will show that the value returned by this function is <code>vm_swappiness<\/code>. So now, the variable <code>swappiness<\/code> is set to equal whatever value <code>vm_swappiness<\/code> was set to. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":568} --><\/p>\n<p> int swappiness = mem_cgroup_swappiness(memcg); <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":616} --><\/p>\n<p> And <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/torvalds\/linux\/blob\/master\/mm\/vmscan.c#L2306\">a little further down in the same source code file<\/a>, we see this: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":7,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":686} --><\/p>\n<pre><code class=\"hljs javascript\">\/*<br \/>* With swappiness at 100, anonymous and file have the same priority.<br \/>* This scanning priority <span class=\"hljs-keyword\">is<\/span> essentially the inverse of IO cost. <br \/>*\/ anon_prio = swappiness; <br \/>file_prio = 200 - anon_prio;<\/code> <\/pre>\n<p> That&#8217;s interesting. Two distinct values are derived from <code>swappiness<\/code>. The <code>anon_prio<\/code> and <code>file_prio<\/code> variables hold these values. As one increases, the other decreases, and vice versa. The Linux swappiness value actually sets the ratio between two values. <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":7,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":7,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":943} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":8,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":7,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":8,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":0} --><\/p>\n<h2 id=\"the-golden-ratio\"><span class=\"ez-toc-section\" id=\"The_Golden_Ratio\"><\/span> The Golden Ratio <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> File pages hold data that can be easily retrieved if that memory is freed. Linux can just read the file again. As we&#8217;ve seen, if the file data has been changed in RAM, those changes must be written to the file before the file page can be freed. But, either way, the file page in RAM can be repopulated by reading data from the file. So why bother adding these pages to the swap partition or swap file? If you need that data again, you might as well read it back from the original file instead of a redundant copy in the swap space. So file pages are not stored in swap. They&#8217;re &#8220;stored&#8221; back in the original file. With anonymous pages, there is no underlying file associated with the values in memory. The values in those pages have been dynamically arrived at. You can&#8217;t simply read them back in from a file. The only way anonymous page memory values can be recovered is to store the data somewhere before freeing the memory. And that&#8217;s what swap holds. Anonymous pages that you are going to need to reference again. But note that for both file pages and for anonymous pages, freeing up the memory may require a hard drive write. If the file page data or the anonymous page data has changed since it was last written to the file or to swap, a file system write is required. To retrieve the data will require a file system read. Both types of page reclaim are costly. Trying to reduce hard drive input and output by minimizing the swapping of anonymous pages only increases the amount of hard drive input and output that is required to deal with file pages being written to, and read from, files. As you can see from the last code snippet, there are two variables. One called <code>file_prio<\/code> for &#8220;file priority&#8221;, and one called <code>anon_prio<\/code> for &#8220;anonymous priority&#8221;. <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":8,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":8,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":1805} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":9,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":8,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<ul>\n<li> The <code>anon_prio<\/code> variable is set to the Linux swappiness value. <\/li>\n<li> The <code>file_prio<\/code> value is set to 200 minus the <code>anon_prio<\/code> value. <\/li>\n<\/ul>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":9,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":125} --><\/p>\n<p> These variables hold values that work in tandem. If they are both set to 100, they are equal. For any other values, <code>anon_prio<\/code> will decrease from 100 towards 0, and <code>file_prio<\/code> will increase from 100 towards 200. The two values feed into a complicated algorithm that determines whether the Linux kernel runs with a preference for reclaiming (freeing up) file pages or anonymous pages. You can think of <code>file_prio<\/code> as the system&#8217;s willingness to free up file pages and <code>anon_prio<\/code> as the system&#8217;s willingness to free anonymous pages. What these values don&#8217;t do is set any kind of trigger or threshold for when swap is going to be used. That&#8217;s decided elsewhere. But, when memory needs to be freed, these two variables&#8212;and the ratio between them&#8212;are taken into consideration by the reclamation and swap algorithms to determine which page types are preferentially considered for freeing up. And that dictates whether the associated hard drive activity will be processing files for file pages or swap space for anonymous pages. When Does Swap Actually Cut In? We&#8217;ve established that the Linux swappiness value sets a preference for the type of memory pages that will be scanned for potential reclamation. That&#8217;s fine, but something must decide when swap is going to cut in. Each memory zone has a high water mark and a low water mark. These are system derived values. They are percentages of the RAM in each zone. It is these values that are used as the swap trigger thresholds. To check what your high and low water marks are, look inside the <code>\/proc\/zoneinfo<\/code> file with this command: less \/proc\/zoneinfo Each of the zones will have a set of memory values measured in pages. Here are the values for the DMA32 zone on the test machine. The low water mark is 13966 pages, and the high water mark is 16759 pages: <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":9,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":9,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":1951} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":10,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":9,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<ul>\n<li> In normal running conditions, when free memory in a zone drops below the zone&#8217;s low water mark, the swap algorithm starts scanning memory pages looking for memory that it can reclaim, taking into account the relative values of <code>anon_prio<\/code> and <code>file_prio<\/code>. <\/li>\n<li> If the Linux swappiness value is set to zero, swap occurs when the combined value of file pages and free pages are less than the high water mark. <\/li>\n<\/ul>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":405} --><\/p>\n<p> So you can see that you cannot use the Linux swappiness value to influence swap&#8217;s behavior with respect to RAM usage. It just doesn&#8217;t work like that. <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":564} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":10,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":564} --><\/p>\n<h2 id=\"what-should-swapiness-be-set-to\"><span class=\"ez-toc-section\" id=\"What_Should_Swapiness_Be_Set_To\"><\/span> What Should Swapiness Be Set To? <span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p> This depends on hardware, workload, hard drive type, and whether your computer is a desktop or a server. Obviously, this isn&#8217;t going to be a one size fits all type of setting. And you have to bear in mind that swap isn&#8217;t just used as a mechanism to free up RAM when you&#8217;re running out of memory space. Swap is an important part of a well functioning system, and without it, sane memory management becomes very difficult for Linux to achieve. Changing the Linux swappiness value has an instant effect; you don&#8217;t need to reboot. So you can make small adjustments and monitor the effects. Ideally, you&#8217;d do this over a period of days, with different types of activity on your computer, to try to find the closest to an ideal setting that you can. These are some points to consider: <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":10,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":10,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":1364} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":11,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":10,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<ul>\n<li> Trying to &#8220;disable swap&#8221; by setting the Linux swappiness value to zero simply shifts the swap-associated hard drive activity to file-associated hard drive activity. <\/li>\n<li> If you have aging, mechanical hard drives, you might try reducing the Linux swappiness value to bias away from anonymous page reclamation and reduce swap partition churn. Of course, as you turn down one setting, the other setting increases. Reducing swap churn is likely to increase the file system churn. But your computer might be happier favoring one method over the other. Really, the only way to know for sure is to try and see. <\/li>\n<li> For single-purpose servers, such as database servers, you may get guidance from the suppliers of the database software. Very often, these applications have their own purpose-designed file cache and memory management routines that you&#8217;d be better to rely on. The software providers may suggest a Linux swappiness value according to machine specification and workload. <\/li>\n<li> For the average desktop user with reasonably recent hardware? Leave it as it is. <\/li>\n<\/ul>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":11,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":11,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":1066} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":12,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":11,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<p> How to Set the Linux Swappiness Value Before you change your swappiness value, you need to know what its current value is. If you want to reduce it a little bit, the question is a little bit less than what? You can find out with this command: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":244} --><\/p>\n<pre>cat \/proc\/sys\/vm\/swappiness<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":273} --><\/p>\n<p> To configure the swappiness value, use the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man8\/sysctl.8.html\"><code>sysctl<\/code> command<\/a>: <\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":333} --><\/p>\n<pre>sudo sysctl vm.swappiness=45<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":12,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":363} --><\/p>\n<p> The new value is used straight away, no reboot is required. In fact, if you do reboot, the swappiness value will return to its default value of 60. When you have finished experimenting and have decided on the new value you wish to use, you can make it persistent across reboots by adding it to the <code>\/etc\/sysctl.conf<\/code> file. You can use whichever editor you prefer. Use the following command to edit the file with the <code>nano<\/code> editor: sudo nano \/etc\/sysctl.conf When <code>nano<\/code> opens, scroll to the bottom of the file and add this line. We&#8217;re using 35 as the permanent swappiness value. You should substitute the value you wish to use. <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":12,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":12,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":990} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":13,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":12,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<pre>vm.swappiness=35<\/pre>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":13,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":18} --><\/p>\n<p> To save your changes and exit from <code>nano<\/code>, press &#8220;Ctrl+O&#8221;, press &#8220;Enter&#8221;, and press &#8220;Ctrl+Z.&#8221; Memory Management is Complex Memory management is complicated. And that&#8217;s why, for the average user, it is usually better to leave it up to the kernel. It&#8217;s easy to think you&#8217;re using more RAM than you are. Utilities like <code>top<\/code> and <code>free<\/code> can give the wrong impression. Linux will use free RAM for a variety of its own purposes, such as disk caching. This artificially elevates the &#8220;used&#8221; memory figure and reduces the &#8220;free&#8221; memory figure. In actual fact, the RAM used as disk cache is flagged as both &#8220;used&#8221; and &#8220;available&#8221; because it can be reclaimed at any time, very quickly. To the uninitiated that might look like swap isn&#8217;t working, or that the swappiness value needs changing. As always, the devil is in the detail. Or, in this case, the daemon. The kernel swap daemon. <\/p>\n<p><!-- Repeatable debug data: {\"injection\":\"before\",\"adPosition\":13,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":13,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":972} --><!-- Zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"after\",\"adPosition\":14,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":14,\"nbrPlacementsScanned\":13,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"isEarlyInjection\":false,\"actualCount\":0} --><br \/>\n<!-- Repeatable debug data: {\"isEarlyInjection\":false,\"currentRuleCount\":900,\"actualCount\":0,\"hasActualCountMetThreshold\":null,\"countRemainingForInjection\":null,\"nextBlockCount\":null,\"hasCountRemainingForInjectionMetThreshold\":null} --><\/p>\n<p><!-- No repeatable ad for zone: character count repeatable. --><!-- Repeatable debug data: {\"injection\":\"none\",\"adPosition\":14,\"startingPoint\":0,\"skipEvery\":null,\"nbrPlacementFilledEachSkip\":14,\"nbrPlacementsScanned\":14,\"ruleCount\":900,\"degradationStartingPoint\":1,\"stopAds\":null,\"actualCount\":320} --><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux swappiness value has nothing to do with how much RAM is used before swapping starts. That&#8217;s a widely reported and widely believed mistake. We explain what it really is. Busting Myths About Swapiness Swapping is a technique where data in Random Access Memory (RAM) is written to a special location on your hard &#8230; <a title=\"What Is Swappiness on Linux? (and How to Change It)\" class=\"read-more\" href=\"https:\/\/goodwriterz.com\/site\/what-is-swappiness-on-linux-and-how-to-change-it\/\" aria-label=\"Read more about What Is Swappiness on Linux? (and How to Change It)\">\u0625\u0642\u0631\u0623 \u0627\u0644\u0645\u0632\u064a\u062f<\/a><\/p>\n","protected":false},"author":1,"featured_media":28247,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[42,43],"class_list":["post-28246","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-linux","tag-linux-macos-terminal"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/goodwriterz.com\/site\/wp-content\/uploads\/2024\/08\/What-Is-Swappiness-on-Linux-and-How-to-Change-It.jpg?fit=2100%2C1400&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/posts\/28246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/comments?post=28246"}],"version-history":[{"count":0,"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/posts\/28246\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/media\/28247"}],"wp:attachment":[{"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/media?parent=28246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/categories?post=28246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/goodwriterz.com\/site\/wp-json\/wp\/v2\/tags?post=28246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}