2 Implementing Swap Spaces

Swap spaces are a way by which the operating system manages resources in the system to ensure efficient performance.

Oracle Linux uses swap space if your system does not have enough physical memory for ongoing processes. When available memory is low, the operating system writes inactive pages to swap space on the disk, and thus free up physical memory.

However, swap space is not an effective solution to memory shortage. Swap space is located on disk drives, which have much slower access times than physical memory. Writing to swap space effectively degrades system performance. If your system often resorts to swapping, you should add more physical memory, not more swap space.

Swap space can be either in a swap file or on a separate swap partition. A dedicated swap partition is faster, but changing the size of a swap file is easier. If you know how much swap space your system requires, configure a swap partition. Otherwise, start with a swap file and create a swap partition later when you know what your system requires.

Creating a Swap File

  1. Use the dd command to create a file of the required size, for example, one million 1KB blocks.

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1000000
    1000000+0 records in
    1000000+0 records out
    1024000000 bytes (1.0 GB, 977 MiB) copied, 6.10298 s, 168 MB/s
  2. Initialize the file as a swap file.

    sudo mkswap /swapfile
    mkswap: /swapfile: insecure permissions 0644, 0600 suggested.
    Setting up swapspace version 1, size = 976.6 MiB (1023995904 bytes)
    no label, UUID=43964855-e81f-414c-a61c-370408085ba4
  3. Change the permissions on the file so that it is not world readable.

    sudo chmod 0600 /swapfile
  4. Add an entry to the /etc/fstab file so that the system uses the swap file at system reboots, for example:

    /swapfile       swap       swap       defaults       0 0
  5. Regenerate the mount units and register the new configuration in /etc/fstab.

    sudo systemctl daemon-reload
  6. Activate the swap file.

    sudo swapon /swapfile
  7. (Optional) Test whether the new swap file was successfully created by inspecting the active swap space:

    cat /proc/swaps
    sudo free -h

Creating a Swap Partition

  1. Create the swap partition by using either fdisk or parted.

    • If using fdisk, create the partition as discussed in Creating Partitions. Then use t to change the partition type from the default to 82 Linux swap / So.

    • If using parted, specify linux-swap at the File system type? prompt as shown in Partitioning Disks by Using parted.

  2. Initialize the partition as a swap partition.

    For example, if the partition is /dev/sda2, use the following command:

    sudo mkswap /dev/sda2
  3. Enable swapping to the swap partition.

    sudo swapon /dev/sda2
  4. Add an entry to /etc/fstab for the swap partition so that the system uses it following the next reboot, for example:

    /dev/sda2       swap       swap       defaults       0 0

Viewing Swap Space Usage

To view a system's usage of swap space, examine the contents of /proc/swaps:

cat /proc/swaps
Filename                Type        Size      Used   Priority
/dev/sda2               partition   4128760   388    -1
/swapfile               file        999992    0      -2

In this example, the system is using both a 4GB swap partition on /dev/sda2 and a 1GB swap file, /swapfile. The Priority column shows that the operating system to write to the swap partition rather than to the swap file.

You can also view /proc/meminfo or use utilities such as free, top, and vmstat to view swap space usage, for example:

grep Swap /proc/meminfo
SwapCached:          248 kB
SwapTotal:       5128752 kB
SwapFree:        5128364 kB
sudo free | grep Swap
Swap:      5128752        388    5128364

Removing a Swap File or Swap Partition

To remove a swap file or swap partition from use:

  1. Disable swapping to the swap file or swap partition, for example:

    sudo swapoff /swapfile
  2. Remove the entry for the swap file or swap partition from /etc/fstab.

  3. Optionally, remove the swap file or swap partition if you no longer need it.