Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

At times the kernel seems to be intent on keeping e.g. firefox in swap, even though there is enough physical memory available and I'm using firefox:

robert@rm:~> free -m
             total       used       free     shared    buffers     cached
Mem:          3009       1904       1104          0        109        679
-/+ buffers/cache:       1116       1892
Swap:         4102        885       3216

Is there a command to instruct the kernel to drop swap space and use the available free memory?


Update: although in the short term I've used swapon/swapoff, I now set

sudo sysctl vm.swappiness=30

as suggested in another response, and get very good results.

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

You could do a swapoff -- will need root privileges,
but, I guess that is not a problem for you.

share|improve this answer
add comment

You could do what nik suggested and use swapoff. However, there is another, more elegant way to tweak "swappiness", or how aggressively the kernel swaps programs out to disk in systems running the 2.6 kernels.

There have been heated debates on the linux kernel mailing lists about the policy the kernel should follow regarding swapping behaviour. The upshot is that we now have a patch in 2.6 kernels that lets us tweak this behaviour to a large extent.

Note that you need root privileges to do this, as you would to run the swapoff/swapon commands.

The current value of "swappiness" can be inspected in the /proc/sys/vm/swappiness file, or by running this sysctl command:

sudo sysctl vm.swappiness

The "swappiness" values can range from 0 (no swapping) to 100 (swap to disk as much as possible). Ubuntu ships with the default swappiness set to 60.

To change this for a session, call sysctl again and pass it a swappiness value to use:

sudo sysctl vm.swappiness=30

Besides free, you can of course monitor the effects of doing this via the excellent htop or iotop utilities.

If you like what you see, and want to retain this value across reboots, just place "vm.swappiness=30" in the /etc/sysctl.conf file.

$ sudo sysctl vm.swappiness
vm.swappiness = 30
$ sudo sysctl vm.swappiness=40
vm.swappiness = 40
$ sudo sysctl vm.swappiness
vm.swappiness = 40
$ sudo tail /etc/sysctl.conf 
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#
# The contents of /proc/<pid>/maps and smaps files are only visible to 
# readers that are allowed to ptrace() the process
# kernel.maps_protect = 1
vm.swappiness=30

You can play with different values till you find one that entails an acceptable level of swapping on your machine.

share|improve this answer
    
Thanks for the sysctl tip - I'll experiment. –  Robert Munteanu Aug 11 '09 at 8:33
    
Very informative, thanks! –  Jonik Aug 11 '09 at 12:02
    
+1 for sysctl. When experimenting with different values of swappiness, you can write directly to the proc file (echo "30" > /proc/sys/vm/swappiness). This changes is temporary and will not write into /etc/sysctl.conf. Once you're happy with the settings, then use sysctl to make a 'permanent' change. –  Shawn Chin Aug 17 '09 at 9:41
add comment

Remember that the stuff in swap was put there at a busier time than now.

You might well find that the data is also in RAM - having been loaded back for use but not wiped from swap. The kernel does this to save time in the next busy period - if the data is already copied there it can just release the RAM it is using instead of having to do disk writes first. So unless you know the machine is not going to need to swap again in the near future, forcing the freeing of swap space may achieve nothing except to slow things down a little more next time swapping is needed.

This is managed on a delete-on-write basis, so if the copy in RAM is updated the blocks in swap will be released.

If the swap space is needed for something else, which it won't be at the moment as you have plenty free, it will of course be released for that use.

You can't see how much data is in this state (of being both in disk-based pages in swap areas, and in RAM) in the output from free. You can see it via the /proc special filesystem though. Look at the output of cat /proc/meminfo for the SwapCached count.

Another two reasons for there to be swap in use when there is apparently spare memory are

  • when under heavy I/O load the kernel has decided that pages that haven't been used for ages is better swapped out so it can use the RAM for cache/buffers, though looking at your free output that is probably not the case here as you have a chunk of truly unallocated RAM as well as that used by IO cache/buffers.
  • the pages were swapped out for some reason earlier, and just hasn't been needed again since - perhaps it is memory in use by a process that has been inactive for some time. In this case releasing the swap (so loading the pages back into RAM) may improve the response time of that process next time it is needed to do something other than sleep, but if it has been inactive for some time already it might not be needed at any time in the near future anyway.
share|improve this answer
1  
+1 for an excellent explanation. FYI, vmstat lets you see /proc/meminfo data through not all of it. –  nagul Aug 11 '09 at 11:41
add comment

Remember that free is just a snapshot of memory usage. The result you are seeing may imply that, at some point in the past, the system was getting short of RAM so swapped-out RAM-resident material. Since then, RAM has become free but the material on swap has not been required so is still held on swap rather than just cluttering up RAM. If that is the case, turning off swap would be a bad idea. Are there any processes that could demand such a large amount of RAM since the last reload?

Additionally, you may want to check that you don't have a restriction on maximum resident size set via ulimit (usually in /etc/profile but maybe it varies with distributions and it can be set per-process (e.g. in a launch script)).

Finally, on Microsoft Windows there are reports of specific issues with Firefox being swapped out to disk, e.g. when minimised (e.g. http://joeabiraad.com/mozilla-firefox/control-your-firefox-ram-usage/113). I've not heard of that on GNU/Linux systems but it may be worth exploring.

share|improve this answer
    
Thanks for the answer. My problem is not that swap is being used, but that it's now used for active programs, e.g. Firefox and Thunderbird, which feel sluggish. A swapoff/swapon made them feel snappy again. –  Robert Munteanu Aug 11 '09 at 8:34
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.