Swap

From Wiki - Manjaro-Arm
Revision as of 06:49, 15 April 2018 by GrayJack (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Overview

Linux divides its physical Random Access Memory (RAM) into chunks of memory called pages. The swap space is used to receive copies of pages that are no longer needed by running processes to free up the RAM when needed.

There are two possible to set up a swap space:

  • Swap file
  • Swap partition
Note: There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.

Swap File

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.

Tip: It is recommended to use swap file since most boards only supports booting from storage devices that usually comes with small sizes, like eMMC, microSD, USB storage.

Swap Partition

A swap partition is a partition used solely to use as swap space, it partition type is typically designated as type 82, although it is possible to use any partition type as swap space.

Note: Systemd can automatically detect a partition of type 82 and mount it if properly set.

Swappiness

The swappiness 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:

sudo sysctl vm.swappiness=[value]

To set the swappiness value permanently, edit a sysctl configuration file /etc/sysctl.d/99-sysctl.conf Open the file with your favourite editor, don't forget that you require privileges to open this file, that means, use sudo.

Detecting Existing Swap Space

To find if a swap file exist use this command:

find /swapfile

The easiest way to detect existing swap partition is using this command:

sudo blkid | grep swap

If nothing is printed in the terminal, that means there is no swap partition

Configuring a Swap Space

Swap File

  1. Create a swap file the size of your choosing:
    • sudo fallocate -l [size][M/G] /swapfile
    • Note: The 'M' means Mebibytes while 'G', Gibibytes.
  2. Set permissions, because a world-readable swap file is a huge local vulnerability:
    • sudo chmod 600 /swapfile
  3. Now format it to swap:
    • sudo mkswap /swapfile
  4. Activate the swap file:
    • sudo swapon /swapfile
  5. Use your favourite text editor to edit /etc/fstab to add an entry for the swap file, this will enable this swap file on boot:
    • /swapfile none swap defaults 0 0

Swap Partition

  1. Create a partition of type 82 with the size of your choosing.
  2. Set up the created partition as Linux swap area:
    • sudo mkswap /dev/[device][partiton]
    • Example:
      sudo mkswap /dev/mmcblk0p3
    • Tip: To know the right partition to use, use the command:
      sudo blkid | grep swap
    • Warning: All data on the specified partition will be lost if exist.
  3. Now, enable the device for paging:
    • sudo swapon /dev/[device][partition]
  4. Use your favourite text editor to edit /etc/fstab to add an entry for the swap partition, this will enable this swap partition on boot:
    • PARTUUID=[PARTUUID] none swap defaults 0 0
    • Tip: To know the PARTUUID to use, use the command:
      sudo blkid | grep swap


See also