Swap

From LQWiki
(Redirected from Virtual Memory)
Jump to navigation Jump to 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. swap is said to be 1000 times slower than having "real", physical memory.

By using swap space, 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.

How to do it

To create a 1GB swap file and use it, issue

dd if=/dev/zero of=/tmp/swap bs=1024 count=1000000
mkswap /tmp/swap  # write a signature so the system knows this is swap
swapon /tmp/swap
cat >> /etc/fstab << EOF
/tmp/swap none swap pri=100,defaults 0 0
EOF

To find out if your swap is in use, issue the command

swapon -s

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