Tuesday, August 26, 2008

Linux and Swap Space

Few day back I heard that Linux does not use swap space and some of the critical services are getting crashed due to insufficient memory space and then my research started and found some interesting facts which is enclosed below.

Recently, some experimental improvement to the 2.6 Linux kernel have been made by Con Kolivas , published in his popular -ck patchset . The improvement, called "swap prefetch", employs a mechanism of prefetching previously swapped pages back to physical memory even before they are actually needed, as long as the system is relatively idle (so as not to impair performance) and there is available physical memory to use. This applies to a situation when a "heavy" application has been temporarily used, causing other processes to swap out. After it is closed, both freeing large areas of memory and reducing disk load, prefetch of other processes starts, reducing their initial user response time.

Within Linux®, you can adjust the swappiness of the system when using the 2.6 kernel by setting the vm.swappiness value in /etc/sysctl.conf.
Higher values lead to more pages being swapped and lower values lead to more applications being kept in memory, even if they are idle. The Linux
2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100.

In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."

There are two aspects to monitoring swap space and how it is used:
Current usage and active usage. You can find an array of different tools across the UNIX variants that report available free memory and VM usage with current usage.
$ swapon -s (Is very common tool to monitor the Swap use)

There are a number of different points of view and rules on how you should configure VM, and how much disk space you should allocate. Some of these approaches have been summarized below:

* Some administrators believe you should never have less swap space than physical RAM. This was a requirement with older versions of UNIX (particularly SunOS) because of the way in which VM was used. In theory, having more VM than physical RAM enables the operating system to swap out all current processes to disk when the system becomes idle, and it can speed up performance for systems because new processes can be loaded directly into RAM without having to swap applications beforehand.

* For development systems, many administrators agree that you should have as much VM as you have RAM. The reason for this approach is that swap space is also used during a system failure as the location where the active memory is dumped; the stored memory dump is then reloaded during boot and saved as a kernel core file. If the configured swap space is less than the physical RAM, such dumps are not possible.

* In high-performance deployment environments, you can configure as little VM as possible, on the basis that you want to keep the applications active in memory and don't need a lot of swap. Crash recovery is not required, and the ability to identify a potential lack of physical RAM (through running out of swap space) provides a warning that your application environment is not optimized, or that you need to upgrade your RAM.

* For desktop environments, having a very large VM allocation can
be to your advantage, as it enables you to run a large number of applications (many of which will probably be idle and easily swapped) making more RAM available for the active applications.

Choosing the right amount of swap space is highly dependent on the platform you are configuring -- it's intended use and how you want to cope with a lack of available VM.

For some conservative values:
* On a desktop system, allocate at least as much swap space as physical RAM.
* On a server, allocate at least 50 percent of your available RAM as swap space. Once you have a figure in mind, you need to configure the swap space.

OOM (Out-of-Memory) killer

If the Linux VM can't find memory to allocate when it's needed, it puts in-use user data pages on the swap-out queue, to be swapped out. If the VM can't allocate memory and can't swap out in-use memory, the Out-of-memory killer may begin killing current userspace processes. The rationale for OOM killing is described in the Linux-MM docs.


Managing swap space is an essential aspect of system administration. With good planning and proper use swapping can provide many benefits.Don't be afraid to experiment, and always monitor your system to ensure you are getting the results you need.

No comments: