Swap

From Mage
Revision as of 02:19, 3 July 2018 by Bret (talk | contribs) (Created page with "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 p...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, 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:

/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.