The swap feature temporarily stores inactive memory pages. If the system memory is sufficient, the system can store inactive memory pages into the swap space, which frees up memory for other running processes. This topic describes the risks of configuring the swap feature in Alibaba Cloud Linux and how to configure the swap feature. You can determine whether to enable the swap feature based on your business requirements.
Swappiness parameter
On an Alibaba Cloud Linux Elastic Compute Service (ECS) instance, the default swappiness value is 0.
Swappiness is a kernel parameter that manages the tendency of using the swap space.
0: The system uses physical memory as much as possible instead of swap partitions.
100: The system uses swap partitions as much as possible instead of physical memory.
If the swappiness value is set to an overlarge value such as close to or equal to 100, the swap space may still be used even if sufficient memory is available. This results in degraded system performance and increased memory access latency.
Risks of configuring the swap feature
Risks of configuring the swap feature on hosts:
If system memory pressure is too high, the system may frequently swap memory. The frequent use of swap memory may cause high latency because reading data from disks is slower than reading data from the memory. This affects application performance and system stability.
Risks of configuring the swap feature in containers:
Resource allocation complexity: In a containerized environment, resource management is typically performed at the host level. When a container is configured to allocate and use swap memory, resource management may become complex and the stability of other containers may be affected.
Resource competition: If multiple containers use swap memory on a host, resource competition may occur. This adversely affects the performance and stability of the containers. In particular, when multiple containers enter the swap state at the same time, the disks experience significant I/O pressure.
High latency and low performance: The use of swap memory by containers results in increased latency and performance degradation, especially for I/O-intensive applications.
Configure the swap feature
In most cases, we recommend that you do not configure the swap feature for containers because the containers rely on the kernel of the host to manage resources. You can manage and configure the swap feature for containers by using the startup parameters of the host or containers. Compared with configuring the swap feature for a container, you can use the kernel of the host to manage swap memory in a more efficient and uniform manner. Configuring the swap feature inside a container may cause unnecessary complexity and make management difficult. For example, the swap configurations within containers are difficult to monitor and modify in a centralized manner.
Configure the swap feature on a host
For more information, see How do I configure a swap partition on a Linux instance and resolve frequently asked questions?
Configure the swap feature for a container by using container startup parameters
When you start a container, you can manage the memory usage and swap memory usage by using the memory
and memory-swap
parameters.
--memory=<value>
: specifies the amount of physical memory that a container can use.--memory-swap=<value>
: specifies the total amount of memory, including the amount of physical memory and swap memory, that a container can use. If the total amount of memory is greater than the amount of physical memory that can be used, the excess memory is allocated to swap partitions.
In this example, C code is used. Set the total amount of memory to 2 GB, the amount of physical memory of the container to 1 GB, and the amount of swap memory to 1 GB.
docker run --memory=1g --memory-swap=2g my_container