All Products
Search
Document Center

:How do I configure a swap partition on a Linux instance and resolve frequently asked questions?

Last Updated:Aug 03, 2021

Disclaimer: This topic may contain information about third-party products. The information is for reference only. Alibaba Cloud does not make guarantees or warranties, express or implied, with respect to the performance and reliability of third-party products, and potential impacts of operations on the products.

Overview

This topic describes how to configure a swap partition on a Linux instance and resolve frequently asked questions (FAQ).

Description

Introduce swap partitions

Swap partitions on Linux systems are similar to virtual memory on Windows systems. Linux systems virtualize part of hard disk space to count as virtual memory and move memory that is infrequently used to swap partitions to increase available memory.

Note: If you use basic disks, we recommend that you do not use swap partitions. If you use ultra disks or SSDs, you can use swap partitions based your needs. For more information, see ECS instance use notes.

Configure a swap partition

  1. Log on to the Linux instance as the root user and run the following command to create a swap file.
    dd if=/dev/zero of=/mnt/swap bs=block_size count=number_of_block
    Note: The block_size and number_of_block parameters can be specified. For example, you can set bs to 1M and count to 1024, which indicates a swap partition of 1 GB in size.
  2. Run the following command to set the swap file:
    mkswap /mnt/swap
  3. Run the following command to use the swap file:
    swapon /mnt/swap


    Note: If the /etc/rc.local file contains swapoff - a, change swapoff - a to swapon -a.
  4. Edit the swap line in the /etc/fstab file and add the following command to enable the swap partition on system startup:
    /mnt/swap swap swap defaults 0 0
    Note: The path of /mnt/swap can be modified. You can configure the path based on the actual path of the created swap partition.
  5. Run the following command to temporarily modify the value of the swappiness parameter. In this example, the swap partition is used only when the available physical memory is less than 10%.
    Note: On a Linux system, the rule of system usage of a swap partition can be determined by viewing the value of the content of /proc/sys/vm/swappiness. When the value of the swappiness parameter is set to 0, only physical memory is used. The swap partition is used only after physical memory is unavailable. When the value of the swappiness parameter is set to 100, the swap partition is used, and data in memory is moved to the swap partition in a timely manner. Before you modify the swappiness parameter, the value of the swappiness parameter is 0, which indicates that the swap partition is used only after physical memory is unavailable.
    echo 10 >/proc/sys/vm/swappiness
  6. If you want to permanently modify the configuration and make the configuration take effect after the instance restarts, run the vim command to edit the /etc/sysctl.conf file and add the following command:
    vm.swappiness = 10
  7. Run the following command to check whether the previous command is added:
    sysctl -p

Disable the swap partition

If the system has insufficient memory to use, memory pages may be frequently changed when the swap partition is enabled. This deteriorates I/O performance. Perform the following operations to disable the swap partition:

  1. Run the following command to query the settings of the swap partition:
    free -m
    A command output similar to the following one is returned.
  2. Run the following command to disable the swap partition:
    swapoff [$SWAP_File]
    Note: [$SWAP_File] indicates the identifier of the swap partition.
    A command output similar to the following one is returned.
  3. Run the vim command to modify the /etc/fstab file and delete or comment out relevant configurations to cancel the automatic mount of the swap partition. A command output similar to the following one is returned.
  4. Run the following command to check whether the swap partition is disabled.
    free -m
    A command output similar to the following one is returned.
  5. Run the following command to temporarily modify the value of the swappiness parameter. In this example, the available physical memory is 0%.
    echo 0 >/proc/sys/vm/swappiness    
  6. If you want to permanently modify the configuration and make the configuration take effect after the instance restarts, run the vim command to edit the /etc/sysctl.conf file and add the following command:
    vm.swappiness = 0
  7. Run the following command to check whether the previous command is added:
    sysctl -p

FAQ

Description

An error message similar to the following one appears when you create a swap partition by running the mkswap command:

mkswap: error: swap area needs to be at least 40 KiB

Cause

The specified swap file is small in size. Swap files must be at least 40 KB in size.

Solution

Regenerate a swap file that has a large size.

Applicable scope

  • ECS