Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know of a linux utility which will prevent all memory in a forked process from being swapped out to disk? I've seen the 'mlockall' call, but hacking the app sounds like overkill.

My reason for needing this is that I'm running Windows XP under VirtualBox on my linux netbook, and I'm concerned there are basically two levels of swapping going on, which on a single dinky netbook hard disk isn't good...

share|improve this question

migrated from Sep 2 '11 at 21:20

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

For a single process, mlock and its relatives are the only way, I believe.

System-wide, you might find you get better performance by fiddling with the "swappiness". Try sysctl vm.swappiness to read the value and sysctl -w vm.swappiness=N to set it to N, where is some smaller number than it is currently. (Try 0 if you want to be extreme, telling the kernel that it should always evict cached pages in preference to swapping.)

share|improve this answer
This is excellent- appears to have the exact effect I want it to. Many thanks. I guess giving a VM > 40% of the memory means swapping is kicked off globally whenever the machine is started... – Rick Sep 1 '11 at 8:59

If it actually "swaps" (ie, gets really busy on the disk) you system needs more memory.

If RAM is enough the OSes will not swap, other than paging out some 'dormient' processes to free more physical ram.

It does not directly answers you question, but maybe you can gain some benefit from installing a zram/cache enabled kernel: that'll provide you with some amount of RAM-backed, compressed page cache and swap.

This way your system will still page out ("swap") but it'll do most of it to ram, and thanks to compression the amount of memory pages kept in physical ram (either in active memory or in zcache) will be more than what you get from normal uncompressed ram.

share|improve this answer
I'm happy for a single level of swapping; i.e. WinXP swaps to disk or Linux swaps. What I don't want is linux to swap out a portion of memory XP thinks is in memory, then have XP swap out that same memory (= disk thrashing). Maybe I'm just paranoid about this happening, but my disk sounds much less noisy when only one of the swaps is enabled... Basically, I'm happy for XP to handle its swapping and Linux to handle all-but that. – Rick Aug 30 '11 at 16:28
If XP swaps to disk linux won't be able to "double swap" the same page since it's no more in memory (not even in the VM one). If linux swaps too much it's just short of memory and will swap out the VM and anything else it wants to swap. Reduce the amount of RAM allocated to the VM so linux will swap less and all the swapping for the VM workload will be managed by the VM OS itself. – Luke404 Aug 30 '11 at 16:40
@Luke404: Linux caches disk pages in RAM (quite aggressively, actually). So it is simply not true that "the same page is no more in memory" when the VM "swaps" it to "disk". – Nemo Aug 30 '11 at 20:32
@Nemo: I'll try to clarify my sentence. If the XP o.s. swaps a page, that page won't be in XP's physical memory anymore. XP's physical memory is actually VirtualBox process memory on the linux host, so pages already swapped out by XP will not be "memory" at all as far as the linux host is concerned. – Luke404 Aug 31 '11 at 7:19
@Luke404: Double swapping occurs when Linux swaps out the page first. Consider the following: Linux swaps out a page used by the guest. The guest now decides to also swap out the page. The page must be first read in from disk by Linux so that it can be written out to disk by the guest. – davidg Apr 13 '12 at 2:03

You can also control at which point the system starts to use swap memory. This will tell you what is the current value in your computer:

cat /proc/sys/vm/swappiness

Linux default used to be 60. Roughly speaking, this means that the computer will start using the swap partitions when as little 40% of RAM is being used. This was a safe default for old machines. However, if you have plenty of RAM, you can reduce the swappiness.

On the fly:

sudo sysctl vm.swappiness=10

sudo swapoff -a

sudo swapon -a

Or permanently by adding vm.swappiness = 10 to /etc/sysctl.conf (for Debian).

share|improve this answer
My standard installation of Fedora 15 has swappiness at 60. This is a new machine with 3.5 GB of ram. – wallyk Aug 30 '11 at 19:33

You must log in to answer this question.