I would like to deploy the following swapping policy:

  • By default all pages in memory should also be in swap space.
  • When a page in memory is changed (i.e. dirty), the page should be written out as soon as possible, but with lower priority than other processes.
  • if a certain configurable watermark is reached, (let's say 80% of pages are dirty), the priority will be equal as other processes.

Is this kind of swapping policy possible with the linux kernel? If so, how do I set the kernel settings to achieve this?


Obviously the reason for this is to reduce the number of pages that need to be swapped out. Only dirty pages need to be written to disk, and this happens in the background over time. Therefore when page misses occur (i.e. the page is not in memory), there is no need to write any pages from memory to disk, but only from disk to memory. Therefore it reduced the probability of i/o bottlenecks because both swapping in and swapping out try to access the disk simultaneously.

  • let me see if I understand this.. you want stuff to be in both memory and swap? I don't think that's possible... Commented Aug 14, 2010 at 2:24
  • Not possible with the Linux kernel, or not possible at all? The latter is certainly not true, since that is how HP-UX swapping used to work. You could never run an HP-UX kernel without any swap.
    – txwikinger
    Commented Aug 14, 2010 at 5:04
  • 1
    See at my update to the question. Keeping all pages in swap reduces swap times because only dirty pages need to be swapped out, hence there is less competing for io between swapping in and swapping out. This was the only option of swapping that a lot of older versions of Unix had, and it seemed to work far better than the Linux swapping I experience.
    – txwikinger
    Commented Aug 16, 2010 at 18:33
  • 5
    I find your logic faulty. Total swap-out means all dirty pages have to be swapped out, soonest. I could see doing it for some kind of obscure data backup or unexpected reboot recovery reason, but speed? No, I don't buy it. Commented Aug 16, 2010 at 22:01
  • 1
    I second the faulty logic claim here. The excessive book-keeping over-head of having to write all new pages to swap space when they may never be recalled or used is what will cause this to be slower.
    – polynomial
    Commented Aug 27, 2011 at 19:53

3 Answers 3


You can set the value of /proc/sys/vm/swappiness to control the ratio of segments of data swapped to the segments of data kept in memory. A value of 0 completely avoids swapping at all costs.

This can be done using either:

  • echo 0 > /proc/sys/vm/swappiness
  • sysctl -w vm.swappiness=0
  • Storing that setting in /etc/sysctl.conf

Generally, using just a little swap is not a bad thing. Free memory can be used for caching data read from disk, and the system can plan ahead for a sudden need of lots of memory by an application.

When too many programs are swapped however, there is a lot of disk related activity during every program switch which really makes everything slow down. Before something can be used, it needs to be loaded back into memory.

Disks reads are horribly slow compared to memory access, as it takes significantly longer for the data to arrive. The system has to schedule the read between the other read/write requests, the drive starts making attempts to find the right cylinder, and finally starts slowly delivering data.

Hence, I think your logic is flawed. Generally, you want to keep programs running in memory, while still keeping enough room for sudden growth. Do not use the swap too often to "write things to disk", because it is neither a backup nor a performance improvement.

Older computers contained less memory and suffered from swapping problems as a result. When many programs were open at once, the system would slow down and you could hear the disk reading and writing in order to the swap file.


Just because your system is swapping, does not mean you have a problem. There are applications that are finely tuned to take great advantage of swap without hindering performance of the system. Most relational database systems are tuned this way: IE: Oracle and Cache, probably being the biggest two.

If you use hibernation, it uses swap space for the storage of RAM. When booting the system back up, everything in swap is added back to RAM. This way, you can power down your system without chewing through the battery like standby, and still get back to where you left off before power down. As a result, your battery will last much longer.

Swapping can be a great thing, because it frees up more of your active RAM, to keep the performance of your system high. When your active RAM is filled AND your swap is filled, and you still need more room, then and only then, do you have a problem. Until that point, swap is here to help you, not hurt you.

  • Unless things have changed significantly recently, hibernation doesn't use swapping, it uses (optionally) swap space. I.e. it doesn't use the swap mechanism of the kernel, it just blindly dumps all used memory to any old expendable partition, file, or whatever other object the hibernation code knows about. By convention, that's swap (but not everywhere). VM settings have no effect on how hibernation works.
    – Alexios
    Commented Mar 19, 2012 at 1:58

Adding to vdboor's answer, I think to achieve what you want, you'd need to modify more than swappiness. There are more available kernel tunables that control how swap is used. The user can access those at runtime via /proc/sys/vm/* files or set permanently in /etc/sysctl.conf (this may need adding sysctl init script to your boot sequence to have an effect)

The settings are nicely covered in the Linux kernel documentation. You can find it under <your_kernel_sources_path>/Documentation/sysctl/vm.txt. You can also have a look at some online documentation such as this one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .