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 --show
If 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=1024
NoteTake note of the following parameters in the command:
The
/var/swap
value of theof
parameter 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
bs
andcount
indicate the size of the created swap file. Replace the values with actual values. For example, ifbs
is 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 KiB
appears when you run themkswap
command 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/swap
Run the following command to check whether the swap partition is enabled:
swapon --show
The following command output indicates that the swap partition is enabled.
Run the following command to query the UUID and file system type of the swap partition:
sudo blkid /var/swap
Modify the
/etc/fstab
file and add the following mount information to automatically mount the swap partition on system startup:UUID=1197b17e-270c-41fd-afcd-b252829d**** / swap defaults 1 1
(Optional) Modify the
/etc/rc.local
file to check whether theswapoff -a
command exists. If the command exists, change the command toswapon -a
. Make sure that the command in the/etc/rc.local
file is consistent with the command in the/etc/fstab
file 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
swappiness
value indicates that more physical memory is used. The swap partitions can be used only after physical memory is used up.A larger
swappiness
value 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.conf
file. In this example, the swap partitions are used only when the idle physical memory is less than 10%.vm.swappiness=10
Save and close the file. Run the following command to make the change take effect:
sudo sysctl -p
Run the following command to check whether the new
swappiness
value takes effect:cat /proc/sys/vm/swappiness
The following command output indicates that the new
swappiness
value takes effect.
Disable a swap partition
Run the following command to disable a swap partition:
sudo swapoff /var/swap
NoteThe
/var/swap
specifies the ID of the swap partition. Replace the ID with the actual swap partition ID.Modify the
etc/fstab
file, delete the swap-related mount information, and disable the automatic swap mount feature.UUID=1197b17e-270c-41fd-afcd-b252829d**** / swap defaults 1 1
Save and close the file. Run the following command to check whether the swap partition is disabled:
swapon --show
If the command output is empty, the swap partition is disabled.