{"id":458,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:38","modified_gmt":"2020-08-22T20:26:38","slug":"this-is-the-page-title-toplevel-291","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=458","title":{"rendered":"Swapping Out and Discarding Pages"},"content":{"rendered":"\n<title>Swapping Out and Discarding Pages<\/title>\n<p>\nWhen physical memory becomes scarce the Linux memory management subsystem\nmust attempt to free physical pages.\nThis task falls to the kernel swap daemon (<i>kswapd<\/i>).\n<p>\nThe kernel swap daemon is a special type of process, a kernel thread.\nKernel threads are processes that have no virtual memory, instead they run in kernel mode in the physical address space.\nThe kernel swap daemon is slightly misnamed in that it does more than merely swap pages out to the system&#8217;s\nswap files.\nIts role is make sure that there are enough free pages in the system  to keep the\nmemory management system operating efficiently.\n<p>\nThe Kernel swap daemon (<i>kswapd<\/i>) is started by the\nkernel init process at startup time and sits waiting for the kernel swap timer to periodically expire.\n<p>\nEvery time the timer expires, the swap daemon looks to see if the number of\nfree pages in the system is getting too low.\nIt uses two variables, <i>free_pages_high<\/i> and <i>free_pages_low<\/i> to decide if it should free some pages.\nSo long as the number of free pages in the system remains above <i>free_pages_high<\/i>, the kernel\nswap daemon does nothing; it sleeps again until its timer next expires.\nFor the purposes of this check the kernel swap daemon takes into account the number of pages currently\nbeing written out to the swap file.\nIt keeps a count of these in <i>nr_async_pages<\/i>, which is incremented each time a page is queued waiting to\nbe written out to the swap file and decremented when the write to the swap device has completed.\n<i>free_pages_low<\/i> and <i>free_pages_high<\/i> are set at\nsystem startup time and are related to the number of physical pages in the\nsystem.\nIf the number of free pages in the system has fallen below <i>free_pages_high<\/i> or worse still\n<i>free_pages_low<\/i>, the kernel swap daemon will try three ways to reduce the number of physical pages being\nused by the system:\n<dl compact>\n<p>\n\t<dt><\/dt><dd> Reducing the size of the buffer and page caches,\n\t<dt><\/dt><dd> Swapping out System V shared memory pages,\n\t<dt><\/dt><dd> Swapping out and discarding pages.\n<\/dl>\n<p>\nIf the number of free pages in the system has fallen below <i>free_pages_low<\/i>, the kernel swap daemon\nwill try to free 6 pages before it next runs.\nOtherwise it will try to free 3 pages.\nEach of the above methods are tried in turn until enough pages have been\nfreed.\nThe kernel swap daemon remembers which method it used the last time that it attempted to free\nphysical pages.\nEach time it runs it will start trying to free pages using this last successful method.\n<p>\nAfter it has freed sufficient pages, the swap daemon sleeps again until its timer expires.\nIf the reason that the kernel swap daemon freed pages was that the number of free pages in the\nsystem had fallen below <i>free_pages_low<\/i>, it only sleeps for half its usual time.\nOnce the number of free pages is more than <i>free_pages_low<\/i> the kernel swap daemon goes\nback to sleeping longer between checks.\n<p>\n<h3>Swapping Out and Discarding Pages<\/h3>\n<p>\nThe swap daemon looks at each process in the system\nin turn to see if it is a good candidate for swapping.\n<p>\nGood candidates are processes that can be swapped (some cannot) and that have\none or more pages which can be swapped or discarded from memory.\nPages are swapped out of physical memory into the system&#8217;s swap files only if\nthe data in them cannot be retrieved another way.\n<p>\nA lot of the contents of an executable image come from the image&#8217;s file and\ncan easily be re-read from that file.\nFor example, the executable instructions of an image will never be modified\nby the image and so will never be written to the swap file.\nThese pages can simply be discarded; when they are again referenced\nby the process, they will be brought back into memory from the\nexecutable image.\n<p>\nOnce the process to swap has been located, the swap daemon looks through\nall of its virtual memory regions looking for areas which\nare not shared or locked.\n<p>\nLinux does not swap out all of the swappable pages of the process that\nit has selected. Instead it removes only a small number of pages.\n<p>\nPages cannot be swapped or discarded if they are locked in memory.\n<p>\nThe Linux swap algorithm uses page aging.\nEach page has a counter (held in the <tt>mem_map_t<\/tt> data structure) that\ngives the Kernel swap daemon some idea whether or not a page is worth swapping.\nPages age when they are unused and rejuvinate on access;\nthe swap daemon only swaps out old pages.\nThe default action when a page is first allocated, is to give it an initial age of 3.\nEach time it is touched, it&#8217;s age is increased by 3 to a maximum of 20.\nEvery time the Kernel swap daemon runs it ages pages, decrementing their\nage by 1.\nThese default actions can be changed and for this reason they (and other\nswap related information) are stored in the <tt>swap_control<\/tt> data structure.\nIf the page is old (age = 0), the swap daemon will  process it further.\n<i>Dirty<\/i> pages are pages which can be swapped out.\nLinux uses an architecture specific bit in the PTE to describe pages this way.\nHowever, not all <i>dirty<\/i> pages are necessarily written to the swap file.\nEvery virtual memory region of a process may have its own swap operation\n(pointed at by the <tt>vm_ops<\/tt> pointer in the <tt>vm_area_struct<\/tt>) and that method is used.\nOtherwise, the swap daemon will allocate a page in the swap file and write the\npage out to that device.\n<p>\nThe page&#8217;s page table entry is replaced by one which\nis marked as invalid but which contains information about where the page is in\nthe swap file.\nThis is an offset into the swap file\nwhere the page is held and an indication of which swap file is being used.\nWhatever the swap method used, the original physical page\nis made free by putting it back into the <tt>free_area<\/tt>.\nClean (or rather not <i>dirty<\/i>) pages can be discarded and put back into the <tt>free_area<\/tt>\nfor re-use.\n<p>\nIf enough of the swappable process&#8217; pages have been swapped out or discarded, the swap daemon\nwill again sleep.\nThe next time it wakes it will consider the next process in the system.\nIn this way, the swap daemon nibbles away at each process&#8217; physical pages until the system\nis again in balance.   This is much fairer than swapping out whole processes.\n<p>\n","protected":false},"excerpt":{"rendered":"<p>Swapping Out and Discarding Pages When physical memory becomes scarce the Linux memory management subsystem must attempt to free physical pages. This task falls to the kernel swap daemon (kswapd). The kernel swap daemon is a special type of process, &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=458\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-458","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=458"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/458\/revisions"}],"predecessor-version":[{"id":716,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/458\/revisions\/716"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}