View the Most Wanted LQ Wiki articles.
LinuxQuestions.org > Linux Wiki > Swap

From LQWiki

Jump to: navigation, search

Swap or virtual memory is a technique used to write some memory content, not used at the moment, to the hard disk to make room for a process which needs more memory now.

In this way programs can be started even when the memory is used to its maximum without having to shut down processes first. This also make a good buffer for when peaks of memory usage occur.

Linux can add swap space in two ways, either as a swap file within the file system or as a separate partition. Microsoft Windows uses the file approach (pagefile) while the standard for Linux is to use a partition for swap space.

Benefits of using a swap partition include

  • Storage adapted to be suitable for memory content rather than files.
  • The swap content won't fragment your file system.
  • Can easily be spread on several physical disks for performance.

While swap can give the illusion of more memory then actually exists, it also comes at a price: slowdowns due to the swapping to/from the hard drive, a device that is significantly slower then actual memory. While the kernel tries to swap in a smart way, its not always possible, which can lead to slowdowns when the kernel must actively swap things around. These slowdown can be easily seen on desktops, where the kernel cant predict when a unused program left running will be used again by the user, so the kernel only swaps it back into memory when the user tries to use it again, causing a slight delay in the initial response time of that program.

Swap Parameters in /proc

  • /proc/sys/vm/swappiness holds a single number between 0 and 100 that is the ratio of how much to favor swapping out program data to increase cache space. A value of 100 means to favor cache space the most, meaning lots of active data could get swapped out to disk. A value of zero means program data will remain in memory an long as possible. The default is 60, seen as a good compromise between servers (that would prefer a high value to increase throughput), and desktops (that would prefer a lower value to increase system responsiveness). No matter what value you choose, swapping will still take place when there is no choice. Desktop systems should think about setting this to 0 if they have enough RAM to hold all active programs (anything about 512MB, for example), this will increase the chance of program data remaining in memory, increase the responsiveness of the program after a long away time.

See also


Personal tools