Linux: Configuring Disk Swapping

Overview

Disk swapping allows your system to move less important, less active pages in your RAM onto disk to free space for more active processes. This is ideal when your system has a very small amount of RAM installed to improve the user experience, or when the user has multiple applications running.

For most desktops, pro-active swapping is preferred, as we want to ensure that our most active applications, be it Firefox or the latest game, have a monopoly over our physical memory and always have access to unused memory.

However, with most servers the the opposite is true. The last thing we want to do is swap to disk, unless absolutely necessary.  You are typically running a service that isn’t always being utilized by your user base. Seeing that the service isn’t active 100% of the time, Linux will being to swap the service’s inactive memory pages to disk. This becomes a problem when you get a sudden surge in user activity and pages need to be brought back into active memory.

Thankfully, Linux allows you to configure how often pages should be swapped to disk. You set the value based on the percentage of free memory available. The default value is 60, which means when your system drops to 60% available memory the operating system starts to swap.

For servers, you want to tone the number down to between 0 and 5. Absolutely zero isn’t always recommended. This may cause your server to crash if  it suddenly receives a large amount of requests, which causes the system to run out of physical memory. Ideally, I recommend setting the value 5. It gives you some breathing room when sudden, unexpected surges occur.

Set the Swappiness Value

Swappiness is the value used by Linux’s system control to set when memory pages are swapped to disk.

  1. Open the system control configuration file into a text editor, like Nano.
    nano /etc/sysctl.conf
  2. Add the the following line to the file. You may modify the ‘0’ value to whatever suits your needs.
    vm.swappiness = 0
  3. Save your changes and exit the text editor.

Be careful when modifying the sysctl.conf file, as your changes will have an immediate affect. Always review your changes to ensure your options are set correctly.

Here are some more commonly used values.

Value Effect
60 Default value. This is usually recommended for desktop deployments, though you may adjust it higher or lower depending on your needs.
5 Ideal for servers. This instructs Linux to not swap unless there is 5% or less available physical memory. Make sure your swap valume’s disk isn’t too heavily burdened with other I/O requests to ensure memory pages can be written.
0 Ideal for some servers. This instructs Linux to not swap unless an out of memory error were to occur. You should not use this value unless the disks holding your swap volume are capable of quickly responding to swap requests. If the volume is on a disk that is already heavily accessed, your system may not have enough time to swap pages from memory to disk, causing the server to hang.