All Products
Search
Document Center

Alibaba Cloud Linux:How do I configure the swap feature and what are the risks that I need to consider?

Last Updated:Jun 04, 2026

The swap feature extends usable memory by temporarily moving inactive memory pages to disk. This topic describes the risks of enabling swap on Alibaba Cloud Linux and how to configure it at the host and container levels.

Swappiness parameter

On an Alibaba Cloud Linux Elastic Compute Service (ECS) instance, the default swappiness value is 0.

Swappiness is a kernel parameter that controls how aggressively the kernel moves anonymous memory pages to swap space relative to reclaiming page cache.

  • 0: Turns off anonymous page swapping. The kernel avoids using swap space unless no other memory reclaim option remains.

  • 100: Sets all anonymous pages as swappable. The kernel aggressively moves pages to swap even when free memory is available.

Setting swappiness too high — close to or at 100 — causes the kernel to swap pages even when free memory exists, increasing memory access latency and degrading application performance.

Risks of enabling swap

  • Risks on hosts:

Under sustained memory pressure, the kernel may swap pages in and out frequently. Because disk I/O is orders of magnitude slower than RAM access, frequent swapping causes high latency that degrades application performance and system stability.

  • Risks in containers:

    • Resource allocation complexity: Container runtimes manage resources at the host level. Adding per-container swap memory introduces an extra layer of resource management that can conflict with host-level controls and destabilize other containers on the same host.

    • Resource competition: When multiple containers on the same host use swap simultaneously, they compete for disk I/O. In a multitenant cloud environment, this IOPS contention can affect other workloads on the same physical host, creating a noisy neighbor effect.

    • High latency: Swap use in containers increases access latency and reduces throughput, with the most severe impact on I/O-intensive workloads.

Configure the swap feature

Avoid configuring swap inside containers. Containers rely on the host kernel for resource management, and the host kernel manages swap memory more efficiently and uniformly than per-container configuration. Swap configured inside a container is also harder to monitor and tune centrally.

Use host startup parameters or container startup parameters to manage swap instead. For latency-critical containers, disable swap entirely. For containers with high transient memory demands, enabling swap at the container level can reduce the risk of out-of-memory (OOM) errors under load.

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 swap for a container using container startup parameters

When starting a container, use the memory and memory-swap parameters to control physical memory and swap memory limits.

  • --memory=<value>: specifies the maximum amount of physical memory the container can use.

  • --memory-swap=<value>: specifies the total memory limit (physical memory + swap). Any allocation beyond the --memory limit is drawn from swap. If set to the same value as --memory, the container uses no swap.

The following example limits the container to 1 GB of physical memory and 1 GB of swap (2 GB total).

docker run --memory=1g --memory-swap=2g my_container