Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

Edit:

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.

share|improve this question
    
let me see if I understand this.. you want stuff to be in both memory and swap? I don't think that's possible... –  xenoterracide Aug 14 '10 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 Aug 14 '10 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 Aug 16 '10 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. –  Warren Young Aug 16 '10 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 Aug 27 '11 at 19:53
show 2 more comments

3 Answers

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.

share|improve this answer
    
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 Mar 19 '12 at 1:58
add comment

You can set the value of /proc/sys/vm/swappiness to control the ratio of swapping vs keeping things 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 much 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 slower compared to memory access. Like waiting for an hour before the data arrives. 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 delivering data.. slowly.

Hence, I think your logic is flawed. Generally, you want to keep programs running in memory, and keeping enough room for sudden growth. Not using the swap overly to "write things to disk". It's neither a backup nor a performance improvement.

Old computers with less memory suffered from swapping problems. With a lot of programs open everything was slow, and you could hear the disk reading and writing all the time. To the swap file.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.