From Mage
Jump to navigation Jump to search

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), a.k.a "enable swap", and also for suspend-to-disk support.

If it is beneficial to enable swap depends on the amount of installed physical memory, and the amount of memory required to run all the desired programs. If the amount of physical memory is less than the required amount, then it is beneficial to enable swap. This avoids out of memory conditions, where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference as swap space. For example, if your programs require 7.5 GB of memory to run, and there are 4 GB of physical memory installed, add the difference of 3.5 GB in swap space. Add more swap space to account for future requirements. It is a matter of personal preference if you prefer programs to be killed over enabling swap. The biggest drawback to enabling swap is its lower performance, as disk drives are orders of a magnitude slower than RAM (yes, even SSDs).

Swap partition

If you followed the installation guide you'll still have some space left for partitions via LVM. Create a new partition for your swap data and format it as such, then enable it.

lvcreate -L 2048M -n lv_swap vg_sys      # 2GB swap
mkswap /dev/vg_sys/lv_swap
swapon /dev/vg_sys/lv_swap

The UUID of the swap partition is displayed from the mkswap command upon creation. Insert the UUID into /etc/fstab:

# Swap Partition
UUID=6811af79-1bf7-44aa-b5a0-b3eb13101a53 none swap defaults 0 0

To deactivate/remove the swap partition, issue the following commands:

swapoff /dev/vg_sys/lv_swap
lvremove /dev/vg_sys/lv_swap
# Don't forget to remove the /etc/fstab entry.

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Some filesystems do not handle swap files well. I do not recommend using a swap file on ZFS or Btrfs. Stick to something simple like ext4 or xfs.

Issue the following commands to create a 2GB swap file under /root/swapfile and enable it permanently:

dd if=/dev/zero of=/root/swapfile bs=1M count=2048
chmod 600 /root/swapfile
mkswap /root/swapfile
swapon /root/swapfile

Add the file to your /etc/fstab file so it is mounted at boot:

# Swap File
/root/swapfile none swap defaults 0 0

To deactivate/remove the swap file, issue the following commands:

swapoff /root/swapfile
rm -f /root/swapfile
# Don't forget to remove the /etc/fstab entry.


Swap operations are usually significantly slower than directly accessing data in RAM. Disabling swap entirely to improve performance can sometimes lead to a degradation, since it decreases the memory available for VFS caches, causing more frequent and costly disk I/O.

Swap values can be adjusted to help performance:


The swappiness sysctl parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100, the default value is 60. A low value causes the kernel to avoid swapping, a higher value causes the kernel to try to use swap space. Using a low value on sufficient memory is known to improve responsiveness on many systems.

To check the current swappiness value:

cat /sys/fs/cgroup/memory/memory.swappiness

To temporarily set the swappiness value:

sysctl vm.swappiness=10

To set the swappiness value permanently, edit a sysctl configuration file .

vi /etc/sysctl.d/99-sysctl.conf


If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk (/dev/sda) and a slower disk (/dev/sdb), assign a higher priority to the swap area located on the fastest device. Priorities can be assigned in /etc/fstab via the pri parameter:

/dev/sda1 none swap defaults,pri=100 0 0
/dev/sdb2 none swap defaults,pri=10  0 0

Or via the --priority parameter of swapon:

swapon --priority 100 /dev/sda1

If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.