In Linux, if the system needs more memory resources and the physical memory is used up, the swap partitions (swap space) can be used. You can use swap space to provide temporary storage for inactive processes and data and to prevent issues that occur due to memory insufficiency when physical memory is used up. Swap space serves as an extension to physical memory and allows the system to continue running smoothly when physical memory is exhausted.
Enabling swap partitions may cause memory I/O performance degradation. If the memory of an Elastic Compute Service (ECS) instance is insufficient, we recommend that you change the instance type to increase the physical memory of the instance. If you need to enable swap partitions for your business, follow the procedure in this topic.
If you use a basic cloud disk, do not use swap partitions due to low I/O performance, which may cause performance degradation and I/O bottlenecks. Other categories of cloud disks can use swap partitions based on your business requirements. Avoid frequent swap partition operations and ensure system performance and stability.
View swap partition configurations
Connect to a Linux instance.
For more information, see Use Workbench to connect to a Linux instance over SSH.
Run the following command to view the configurations of swap partitions:
swapon --showIf the command output is empty, the system does not have swap partitions. You can configure swap partitions based on your business requirements.

If a message shown in the following figure appears, the system has swap partitions. You can disable the swap partitions based on your business requirements.

Configure swap partitions
Enable a swap partition
Run the following command to create a swap file:
sudo dd if=/dev/zero of=/var/swap bs=1M count=1024NoteTake note of the following parameters in the command:
The
/var/swapvalue of theofparameter is a variable, which indicates the ID of the swap partition. Replace the ID with the actual swap partition ID. The variable value cannot be the same as the ID of an existing partition.The values of
bsandcountindicate the size of the created swap file. Replace the values with actual values. For example, ifbsis set to 1 MB and count is set to 1024, the size of the swap file is set to 1 GB.
Run the following command to format the file as a swap partition:
sudo mkswap /var/swap
NoteIf an error message similar to
mkswap: error: swap area needs to be at least 40 KiBappears when you run themkswapcommand to create a swap partition, the specified swap partition file is less than the required minimum size of 40 KB. In this case, create a larger swap file.Run the following commands to enable the swap partition:
sudo chmod 600 /var/swap sudo swapon /var/swapRun the following command to check whether the swap partition is enabled:
swapon --showThe following command output indicates that the swap partition is enabled.

Write the information of the swap partition to the
/etc/fstabfile and configure the system to automatically mount the partition on system startup.echo '/var/swap none swap defaults 0 0' | sudo tee -a /etc/fstabView information about the swap partition.
cat /etc/fstabThe following command output is returned.

Restart the ECS instance and check whether the swap partition automatically starts on system startup.
swapon --show(Optional) Modify the
/etc/rc.localfile to check whether theswapoff -acommand exists. If the command exists, change the command toswapon -a. Make sure that the command in the/etc/rc.localfile is consistent with the command in the/etc/fstabfile to ensure that the swap partition can be used as expected.
Related operations
In Linux, you can set the swappiness parameter. Valid values: 0 to 100.
A smaller
swappinessvalue indicates that more physical memory is used. The swap partitions can be used only after physical memory is used up.A larger
swappinessvalue indicates that more swap partitions are used. In this case, data is moved from memory to the swap partitions.
An improper swap setting may cause system performance degradation or cause the virtual memory usage unable to meet expectations. We recommend that you configure the swappiness parameter based on actual business scenarios and the full understanding of the functionality of the parameter. Proceed with caution. If you are uncertain about whether to change the parameter value, we recommend that you use the default setting.
Change the value of the swappiness parameter in the
/etc/sysctl.conffile. In this example, the swap partitions are used only when the idle physical memory is less than 10%.vm.swappiness=10Save and close the file. Run the following command to make the change take effect:
sudo sysctl -pRun the following command to check whether the new
swappinessvalue takes effect:cat /proc/sys/vm/swappinessThe following command output indicates that the new
swappinessvalue takes effect.
Disable a swap partition
Run the following command to disable a swap partition:
sudo swapoff /var/swapNoteThe
/var/swapspecifies the ID of the swap partition. Replace the ID with the actual swap partition ID.Modify the
etc/fstabfile, delete the swap-related mount information, and disable the automatic swap mount feature./var/swap none swap defaults 0 0Save and close the file. Run the following command to check whether the swap partition is disabled:
swapon --showIf the command output is empty, the swap partition is disabled.
