The value of swap space

Discussion in 'all things UNIX' started by Gullible Jones, Jan 27, 2013.

Thread Status:
Not open for further replies.
  1. I'm posting this from an HP ze5375us laptop with 512 MB of RAM - of which 128 are locked up by the GPU, so the OS has only 384 available. Currently the installed OS, Salix 14.0 Xfce, is using 137 MB of RAM (not including filesystem cache) and 44 MB of swap space. It is quite responsive, much more so than the original Windows XP install.

    Having done some recent and extensive Googling on the subject, I will attempt to explain this situation...

    [Disclaimer: the following is not professional advice. Do not apply it on production equipment, do not blame me for hardware failure or data loss, etc. etc.]

    - Linux doesn't actually swap, it pages. Program memory is divided up into parts of a given size (usually 4 KB), and depending on various factors, inactive pages may be removed from RAM, to be retrieved later. The main factor determining when this happens is the sysctl variable vm.swappiness.

    - There are clean and dirty pages. Dirty ones are the ones that have been modified in memory. Clean pages don't end up in swap space, since they can be reread from the disk; only dirty pages end up on your swap partition. So you don't get as much disk writes from swapping as you might think.

    - There's also the filesystem cache, which speeds up disk access drastically. Obviously it wouldn't be much use if it were on the disk, so it's never paged out... But the kernel will shrink it over time and if deemed necessary, based on the sysctl variable vm.vfs_cache_pressure.

    - On a computer with 2 GB of RAM, there's probably enough room in RAM for all the programs and the filesystem cache, so you don't really have to worry about this stuff.

    - On the other hand, if you have 512 MB of RAM, give or take, some bad things may happen:
    * You may run out of RAM, causing thrashing - i.e. writing stuff to swap and then trying to retrieve it immediately. This is manifested as your hard drive grinding while your computer hangs.
    * The filesystem cache may shrink into oblivion. This doesn't sound as bad, but it's still a problem, because you'll get more needless disk activity; loading files will take longer, and it may have to be done repeatedly.

    But you can mitigate these problems by making your computer swap preemptively. Which is why I have set swappiness to 100 and vfs_cache_pressure to 50.

    - High swappiness and low cache pressure mean that filesystem cache gets bigger and sticks around longer, while inactive program memory is paged out sooner. On a low RAM machine, this is what you want. Swapping obviously has a cost, but this way that cost is payed up front in smaller increments, as dirty pages are swapped out when the system is mostly idle. Meanwhile the filesystem cache remains large enough that your desktop doesn't feel like molasses.

    - Compare to what happens with low swappiness: you use up all your RAM, the filesystem cache disappears, and then a storm of dirty pages hits the hard drive. Not good.

    So, in summary:
    - Yes, the kernel developers know what they're doing with swappiness. The default Linux swap behavior is intelligent for most high-end systems.
    - On the other hand, VFS cache pressure in particular is not set so sensible for low-spec systems. If you're low on RAM, you want to sacrifice multitasking ability for more filesystem cache, so reducing cache pressure makes sense.

    And, of course, the caveats:
    - Don't set vfs_cache_pressure too low, lest the kernel favor filesystem cache over programs that need RAM.
    - Don't expect miracles from a Pentium II. Making extra use of swap is not so much about improving performance, as about keeping it reasonable when total RAM usage spikes.

    BTW, If anyone has comments, disagreements, etc. please do say... I'd like my (strictly nonprofessional) advice to be nonetheless up to date and accurate.
  2. NGRhodes

    NGRhodes Registered Member

    Jun 23, 2003
    West Yorkshire, UK
    Sorry for the long delay. But I remember posting a relevant link a long time and could'nt remember where, turns out it was on Ubuntuforums !

    There is a good article on Swappiness : , it is opinionated but has useful snippets of info about the reasoning around tuning swappiness.

    There is also the official documentation: and explains exactly how swappiness and cache_pressure work.

    As I understand it, you want swappiness set as high as possible as it will make more optimal use of unused memory, but not at the cost of interactivity (amount of task switching you do that causes pages to be swapped in/out of the swap space).

    This will be a factor of how frequently you task switch combined with the amount of free memory you have for your typical set of running tasks.

    "Linux does not actually swap, it pages".

    More correct is, Linux has swap space. Memory is allocated as pages in Linux and the swap space is to store unused memory pages (paging in/out of swap is a frequent term used).

    Clean pages are in swap the act of putting a page in swap makes it clean. A dirty page is a page that has been modified since being stored in swap. It must be written back to the swap space before it can be freed.

    Cheers, Nick
Thread Status:
Not open for further replies.