16.2 About Swap Space

16.2.1 Viewing Swap Space Usage
16.2.2 Creating and Using a Swap File
16.2.3 Creating and Using a Swap Partition
16.2.4 Removing a Swap File or Swap Partition

Oracle Linux uses swap space when your system does not have enough physical memory to store the text (code) and data pages that the processes are currently using. When your system needs more memory, it writes inactive pages to swap space on disk, freeing up physical memory. However, writing to swap space has a negative impact on system performance, so increasing swap space is not an effective solution to shortage of memory. Swap space is located on disk drives, which have much slower access times than physical memory. If your system often resorts to swapping, you should add more physical memory, not more swap space.

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

16.2.1 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 4-gigabyte swap partition on /dev/sda2 and a 1-gigabyte swap file, /swapfile. The Priority column shows that the system preferentially swaps 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
# free | grep Swap
Swap:      5128752        388    5128364

16.2.2 Creating and Using a Swap File

To create and use a swap file:

  1. Use the dd command to create a file of the required size (for example, one million 1-kilobyte blocks):

    # dd if=/dev/zero of=/swapfile bs=1024 count=1000000
  2. Initialize the file as a swap file:

    # mkswap /swapfile
  3. Enable swapping to the swap file:

    # swapon /swapfile
  4. Add an entry to /etc/fstab for the swap file so that the system uses it following the next reboot:

    /swapfile       swap       swap       defaults       0 0

16.2.3 Creating and Using a Swap Partition

To create and use a swap partition:

  1. Use fdisk to create a disk partition of type 82 (Linux swap) or parted to create a disk partition of type linux-swap of the size that you require.

  2. Initialize the partition (for example, /dev/sda2) as a swap partition:

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

    # swapon /swapfile
  4. Add an entry to /etc/fstab for the swap partition so that the system uses it following the next reboot:

    /dev/sda2       swap       swap       defaults       0 0

16.2.4 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:

    # 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 do not want to use it in future.